You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Fix flaky tests: use nanoseconds instead of milliseconds
Operations may take less than 1 millisecond at powerful machines, so rounding to milliseconds
results to 0 time duration. Usage of nanoseconds allows to make tests more stable
Copy file name to clipboardExpand all lines: browsermob-core/src/test/java/net/lightbody/bmp/proxy/dns/AdvancedHostResolverCacheTest.java
+17-17Lines changed: 17 additions & 17 deletions
Original file line number
Diff line number
Diff line change
@@ -70,7 +70,7 @@ public void testCanClearDNSCache() {
70
70
resolver.resolve("www.msn.com");
71
71
longfinish = System.nanoTime();
72
72
73
-
assertNotEquals("Expected non-zero DNS lookup time for www.msn.com after clearing DNS cache", 0, TimeUnit.MILLISECONDS.convert(finish - start, TimeUnit.NANOSECONDS));
73
+
assertNotEquals("Expected non-zero DNS lookup time for www.msn.com after clearing DNS cache", 0, finish - start);
74
74
}
75
75
76
76
@Test
@@ -80,17 +80,17 @@ public void testCachedPositiveLookup() {
assertTrue("Expected extremely fast DNS lookup time for news.bing.com on second (cached) lookup. Uncached: " + uncachedLookupMs + "ms; cached: " + cachedLookupMs + "ms.", cachedLookupMs <= uncachedLookupMs / 2);
93
+
assertTrue("Expected extremely fast DNS lookup time for news.bing.com on second (cached) lookup. Uncached: " + uncachedLookupNs + "ns; cached: " + cachedLookupNs + "ns.", cachedLookupNs <= uncachedLookupNs / 2);
94
94
}
95
95
96
96
@Test
@@ -99,17 +99,17 @@ public void testCachedNegativeLookup() {
assertTrue("Expected extremely fast DNS lookup time for fake.notarealaddress on second (cached) lookup. Uncached: " + uncachedLookupMs + "ms; cached: " + cachedLookupMs + "ms.", cachedLookupMs <= uncachedLookupMs / 2);
112
+
assertTrue("Expected extremely fast DNS lookup time for fake.notarealaddress on second (cached) lookup. Uncached: " + uncachedLookupNs + "ns; cached: " + cachedLookupNs + "ns.", cachedLookupNs <= uncachedLookupNs / 2);
113
113
}
114
114
115
115
@Test
@@ -134,7 +134,7 @@ public void testSetPositiveCacheTtl() throws InterruptedException {
134
134
assertNotNull("Collection of resolved addresses should never be null", addresses);
135
135
assertNotEquals("Expected to find addresses for www.msn.com", 0, addresses.size());
136
136
137
-
assertNotEquals("Expected non-zero DNS lookup time for www.msn.com after setting positive cache TTL", 0, TimeUnit.MILLISECONDS.convert(finish - start, TimeUnit.NANOSECONDS));
137
+
assertNotEquals("Expected non-zero DNS lookup time for www.msn.com after setting positive cache TTL", 0, finish - start);
138
138
}
139
139
140
140
@Test
@@ -162,7 +162,7 @@ public void testSetNegativeCacheTtl() throws InterruptedException {
162
162
assertNotNull("Collection of resolved addresses should never be null", addresses);
163
163
assertEquals("Expected to find no addresses for " + fakeAddress, 0, addresses.size());
164
164
165
-
assertNotEquals("Expected non-zero DNS lookup time for " + fakeAddress + " after setting negative cache TTL", 0, TimeUnit.MILLISECONDS.convert(finish - start, TimeUnit.NANOSECONDS));
165
+
assertNotEquals("Expected non-zero DNS lookup time for " + fakeAddress + " after setting negative cache TTL", 0, finish - start);
166
166
}
167
167
168
168
@Test
@@ -184,12 +184,12 @@ public void testSetEternalNegativeCacheTtl() {
log.info("Time to resolve address with cache: {}ms", TimeUnit.MILLISECONDS.convert(finish - start, TimeUnit.NANOSECONDS));
218
+
log.info("Time to resolve address with cache: {}ns", cachedLookupNs);
219
219
220
220
assertNotNull("Collection of resolved addresses should never be null", addresses);
221
221
assertNotEquals("Expected to find addresses for www.msn.com", 0, addresses.size());
222
222
223
-
assertTrue("Expected extremely fast DNS lookup time for www.msn.com after setting eternal negative cache TTL. Cached lookup time: " + cachedLookupMs + "ms.", cachedLookupMs <= 10);
223
+
assertTrue("Expected extremely fast DNS lookup time for www.msn.com after setting eternal negative cache TTL. Cached lookup time: " + cachedLookupNs + "ns.", cachedLookupNs <= TimeUnit.NANOSECONDS.convert(10, TimeUnit.MILLISECONDS));
0 commit comments