Skip to content

Instantly share code, notes, and snippets.

@kalda341
Created October 1, 2015 02:04
Show Gist options
  • Save kalda341/fde6ec2fd514548ee916 to your computer and use it in GitHub Desktop.
Save kalda341/fde6ec2fd514548ee916 to your computer and use it in GitHub Desktop.
@Test
public void testAlbumResourceLongPoll() throws InterruptedException, ExecutionException, TimeoutException {
Client client = ClientBuilder.newClient();
Client asyncClient = ClientBuilder.newClient();
Client getAlbumClient = ClientBuilder.newClient();
try {
final String albumTitle = "My Squelchy Life";
AlbumDTO album = new AlbumDTO();
album.setName(albumTitle);
album.setYearReleased(1975);
album.addArtist(_testArtist);
Future<Response> asyncFuture = asyncClient.target(BASE_URL + "albums").queryParam("artist_id", _testArtist.getId())
.request().async().get();
//Prevent race conditions
Thread.sleep(4000);
Response response = client.target(BASE_URL + "albums")
.request().post(Entity.xml(album));
if (response.getStatus() != 201){
_logger.error("Failed to create album with error: " + response.getStatus());
fail();
} else {
synchronized (asyncFuture) {
_logger.error("Failed to create album with error: " + response.getStatus());
Response asyncAlbumResponse = asyncFuture.get(3, TimeUnit.SECONDS);
AlbumDTO asyncAlbum = getAlbumClient.target(asyncAlbumResponse.getLocation()).request().get(AlbumDTO.class);
if (!asyncAlbum.getName().equals(albumTitle)){
_logger.error("Async long poll failed - album names didn't match");
fail();
}
}
}
} finally {
client.close();
asyncClient.close();
getAlbumClient.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment