public void testCreateHITAsync() { AsyncReply reply = service.createHITAsync( null, // HITTypeId "Async_" + defaultHITTitle + unique, "Async_" + defaultHITDescription, null, // keywords RequesterService.getBasicFreeTextQuestion(defaultQuestion), defaultReward, defaultAssignmentDurationInSeconds, defaultAutoApprovalDelayInSeconds, defaultLifetimeInSeconds, defaultMaxAssignments, null, // requesterAnnotation null, null, // responseGroup null, // uniqueRequestToken null, // assignmentReviewPolicy null, // hitReviewPolicy null // callback ); assertNotNull(reply); assertNotNull(reply.getFuture()); assertFalse(reply.getFuture().isDone()); // wait for result HIT hit = ((HIT[]) reply.getResult())[0]; assertNotNull(hit); assertNotNull(hit.getHITId()); assertTrue(reply.getFuture().isDone()); }
public void testErrorAsync() { AsyncReply reply = service.createHITAsync( null, // HITTypeId "Async_" + defaultHITTitle + unique, "Async_" + defaultHITDescription, null, // keywords "____________________INVALID_QUESTION____________________", defaultReward, defaultAssignmentDurationInSeconds, defaultAutoApprovalDelayInSeconds, defaultLifetimeInSeconds, defaultMaxAssignments, null, // requesterAnnotation null, null, // responseGroup null, // uniqueRequestToken null, // assignmentReviewPolicy null, // hitReviewPolicy null // callback ); assertNotNull(reply); assertNotNull(reply.getFuture()); assertFalse(reply.getFuture().isDone()); // wait for result try { reply.getResult(); fail("Expected ParseErrorException"); } catch (ParseErrorException ex) { // expected exception } }
public void testCreateHITsAsync() { if (service.getConfig().getServiceURL().equals(ClientConfig.SANDBOX_SERVICE_URL)) { // since we test that async performs better, let's wait a 20 secs // so that we can execute the requests without throttling skewing the times try { Thread.sleep(5000); } catch (InterruptedException ie) { //do nothing } } long t1 = System.currentTimeMillis(); AsyncReply[] replies = new AsyncReply[10]; // submit to work queue for (int i = 0; i < replies.length; i++) { replies[i] = service.createHITAsync( null, // HITTypeId "Async_" + String.valueOf(i) + "_" + defaultHITTitle + unique, "Async_" + String.valueOf(i) + "_" + defaultHITDescription, null, // keywords RequesterService.getBasicFreeTextQuestion(defaultQuestion), defaultReward, defaultAssignmentDurationInSeconds, defaultAutoApprovalDelayInSeconds, defaultLifetimeInSeconds, defaultMaxAssignments, null, // requesterAnnotation null, null, // responseGroup null, // uniqueRequestToken null, // assignmentReviewPolicy null, // hitReviewPolicy null // callback ); assertNotNull(replies[i]); assertNotNull(replies[i].getFuture()); assertFalse(replies[i].getFuture().isDone()); } // get results for (AsyncReply reply : replies) { HIT hit = ((HIT[]) reply.getResult())[0]; assertNotNull(hit); assertNotNull(hit.getHITId()); assertTrue(reply.getFuture().isDone()); } long t2 = System.currentTimeMillis(); // create same amount of HITs synchronously for (int i = 0; i < replies.length; i++) { createHIT(); } long t3 = System.currentTimeMillis(); long timeAsync = t2 - t1; long timeSync = t3 - t2; assertTrue(timeAsync < timeSync); }
public void testCreateHITAsyncWithCallback() { SimpleCallback simpleCallback = new SimpleCallback(); AsyncReply reply = service.createHITAsync( null, // HITTypeId "AsyncCB_" + defaultHITTitle + unique, "AsyncCB_" + defaultHITDescription, null, // keywords RequesterService.getBasicFreeTextQuestion(defaultQuestion), defaultReward, defaultAssignmentDurationInSeconds, defaultAutoApprovalDelayInSeconds, defaultLifetimeInSeconds, defaultMaxAssignments, null, // requesterAnnotation null, null, // responseGroup null, // uniqueRequestToken null, // assignmentReviewPolicy null, // hitReviewPolicy simpleCallback // callback ); assertNotNull(reply); assertNotNull(reply.getFuture()); assertFalse(reply.getFuture().isDone()); // wait for result reply.getResult(); // create three failing requests to check failure callbacks AsyncReply[] replies = new AsyncReply[3]; for (int i = 0; i < replies.length; i++) { replies[i] = service.createHITAsync( null, // HITTypeId "Async_" + String.valueOf(i) + "_" + defaultHITTitle + unique, "Async_" + String.valueOf(i) + "_" + defaultHITDescription, null, // keywords "____________________INVALID_QUESTION____________________", defaultReward, defaultAssignmentDurationInSeconds, defaultAutoApprovalDelayInSeconds, defaultLifetimeInSeconds, defaultMaxAssignments, null, // requesterAnnotation null, null, // responseGroup null, // uniqueRequestToken null, // assignmentReviewPolicy null, // hitReviewPolicy simpleCallback); assertNotNull(replies[i]); assertNotNull(replies[i].getFuture()); assertFalse(replies[i].getFuture().isDone()); } // wait for (failing) results and ignore them for (AsyncReply errReply : replies) { try { errReply.getResult(); } catch (ParseErrorException ex) { // ignore expected exception } } // make sure we got right number of callbacks // for successful and failed requests simpleCallback.assertResultCounts(1,replies.length); }