Skip to content

Commit

Permalink
try to make some multithreaded tests a little more robust
Browse files Browse the repository at this point in the history
  • Loading branch information
RayPlante committed Aug 22, 2024
1 parent 5774089 commit ea69349
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -436,8 +436,9 @@ public void testExpires() throws StorageVolumeException, ResourceNotFoundExcepti
long expectedExpires = System.currentTimeMillis() + TWO_WEEKS_MILLIS;
long actualExpires = found.get(0).getMetadatumLong("expires", 0);
// Check that the absolute difference between the expected and actual expires values is less than 1000ms
assertTrue("expires field should be set to 2 weeks from the current time",
Math.abs(expectedExpires - actualExpires) < 1000);
assertTrue("expires field should be set to 2 weeks from the current time (diff="+
Long.toString(Math.abs(expectedExpires - actualExpires)) + ")",
Math.abs(expectedExpires - actualExpires) < 5000);

found = cache.getInventoryDB().findObject("mds1491/trial2.json", VolumeStatus.VOL_FOR_INFO);
assertEquals(1, found.size());
Expand All @@ -446,7 +447,7 @@ public void testExpires() throws StorageVolumeException, ResourceNotFoundExcepti
expectedExpires= System.currentTimeMillis() + TWO_WEEKS_MILLIS;
actualExpires = found.get(0).getMetadatumLong("expires", 0);
assertTrue("expires field should be set to 2 weeks from the current time",
Math.abs(expectedExpires - actualExpires) < 1000);
Math.abs(expectedExpires - actualExpires) < 5000);

found = cache.getInventoryDB().findObject("mds1491/README.txt", VolumeStatus.VOL_FOR_INFO);
assertEquals(1, found.size());
Expand All @@ -455,7 +456,7 @@ public void testExpires() throws StorageVolumeException, ResourceNotFoundExcepti
expectedExpires = System.currentTimeMillis() + TWO_WEEKS_MILLIS;
actualExpires = found.get(0).getMetadatumLong("expires", 0);
assertTrue("expires field should be set to 2 weeks from the current time",
Math.abs(expectedExpires - actualExpires) < 1000);
Math.abs(expectedExpires - actualExpires) < 5000);

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,11 +284,14 @@ public void testCacheDataset() {
resp = websvc.exchange(getBaseURL() + "/cache/objects/67C783D4BA814C8EE05324570681708A1899/:cached",
HttpMethod.PUT, req, String.class);
assertEquals(HttpStatus.ACCEPTED, resp.getStatusCode());
try { Thread.sleep(1000); } catch (InterruptedException ex) { }

resp = websvc.exchange(getBaseURL() +
"/cache/objects/67C783D4BA814C8EE05324570681708A1899/NMRRVocab20171102.rdf",
HttpMethod.GET, req, String.class);
for(int i=0; i < 10; i++) {
try { Thread.sleep(200); } catch (InterruptedException ex) { }
resp = websvc.exchange(getBaseURL() +
"/cache/objects/67C783D4BA814C8EE05324570681708A1899/NMRRVocab20171102.rdf",
HttpMethod.GET, req, String.class);
if (! HttpStatus.NOT_FOUND.equals(resp.getStatusCode())) break;
}
assertEquals(HttpStatus.OK, resp.getStatusCode());
JSONObject file = new JSONObject(new JSONTokener(resp.getBody()));
assertEquals("67C783D4BA814C8EE05324570681708A1899/NMRRVocab20171102.rdf", file.getString("name"));
Expand Down Expand Up @@ -316,14 +319,21 @@ public void testCacheDataset() {
resp = websvc.exchange(getBaseURL() + "/cache/objects/mds1491/:cached?recache=true",
HttpMethod.PUT, req, String.class);
assertEquals(HttpStatus.ACCEPTED, resp.getStatusCode());
try { Thread.sleep(200); } catch (InterruptedException ex) { }

resp = websvc.exchange(getBaseURL() + "/cache/objects/mds1491/trial1.json",
HttpMethod.GET, req, String.class);
for(int i=0; i < 10; i++) {
try { Thread.sleep(200); } catch (InterruptedException ex) { }
resp = websvc.exchange(getBaseURL() + "/cache/objects/mds1491/trial1.json",
HttpMethod.GET, req, String.class);
if (! HttpStatus.OK.equals(resp.getStatusCode())) break;
file = new JSONObject(new JSONTokener(resp.getBody()));
if (since < file.optLong("since", 0L)) break;
}
assertEquals(HttpStatus.OK, resp.getStatusCode());
file = new JSONObject(new JSONTokener(resp.getBody()));
assertEquals("mds1491/trial1.json", file.getString("name"));
assertTrue(since < file.optLong("since", 0L));
assertTrue("Cache object's since date is too old: "+Long.toString(file.optLong("since", 0L))+" <= "+
Long.toString(since),
since < file.optLong("since", 0L));
}

@Test
Expand Down

0 comments on commit ea69349

Please sign in to comment.