WorkerAuthenticator { public void checkHITValid(String hitId, String workerId, Session existingRecord) throws SessionCompletedException, SessionOverlapException { if( existingRecord == null ) { existingRecord = tracker.getStoredSessionInfo(hitId); } if( existingRecord == null ) { tracker.saveHITId(hitId); } else { if( workerId.equals(existingRecord.getWorkerId()) ) { if( SessionRecord.status(existingRecord) == SessionStatus.COMPLETED ) { throw new SessionCompletedException(); } else { logger.info("Session {} by worker {} is reconnecting", hitId, workerId); } } else if( !workerId.equals(existingRecord.getWorkerId()) && ( SessionRecord.status(existingRecord) == SessionStatus.COMPLETED || SessionRecord.status(existingRecord) == SessionStatus.EXPERIMENT ) ) { logger.info(String.format("Session %s was in experiment or completed with worker %s," + "but new worker %s tried to accept", hitId, existingRecord.getWorkerId(), workerId)); throw new SessionOverlapException(); } } } @Inject  WorkerAuthenticator(
			ExperimentDataTracker tracker,
			@Nullable QuizFactory quizFactory,
			@Nullable QuizPolicy quizPolicy,
			Configuration conf
			); @Inject(optional=true) void setSpecialWorkers(
			@Named(TSConfig.EXP_SPECIAL_WORKERS)
			List<String> specialWorkers ); void checkHITValid(String hitId, String workerId, Session existingRecord); void checkWorkerLimits(String hitId, String workerId, Session existingRecord); boolean workerRequiresQuiz(String workerId); boolean tooManyFails(String workerId); boolean quizPasses(Quiz qr); QuizMaterials getQuiz(); int getConcurrentLimit(); int getSetLimit();  }
@Test public void testHITValidEmpty() throws Exception { String hitId = "testHITId"; workerAuth.checkHITValid(hitId, "testWorkerId", tracker.getStoredSessionInfo(hitId)); }
@Test public void testHITValidExisting() throws Exception { String hitId = "testHITId"; tracker.saveHITId(hitId); workerAuth.checkHITValid(hitId, "testWorkerId", tracker.getStoredSessionInfo(hitId)); }
@Test(expected=SessionOverlapException.class) public void testHITValidExperimentOverlap() throws Exception { String hitId = "testHITId"; Session s = new Session(); s.setHitId(hitId); s.setWorkerId("someOtherWorker"); s.setExperimentId("random experiment"); assertEquals(SessionStatus.EXPERIMENT, SessionRecord.status(s)); tracker.saveSession(s); workerAuth.checkHITValid(hitId, "thisWorker", tracker.getStoredSessionInfo(hitId)); }
@Test(expected=SessionOverlapException.class) public void testHITValidCompletedOverlap() throws Exception { String hitId = "testHITId"; Session s = new Session(); s.setHitId(hitId); s.setWorkerId("someOtherWorker"); s.setInactivePercent(0.00); assertEquals(SessionStatus.COMPLETED, SessionRecord.status(s)); tracker.saveSession(s); workerAuth.checkHITValid(hitId, "thisWorker", tracker.getStoredSessionInfo(hitId)); }
@Test(expected=SessionCompletedException.class) public void testHITValidCompleted() throws Exception { String hitId = "testHITId"; String workerId = "workerId"; Session s = new Session(); s.setHitId(hitId); s.setWorkerId(workerId); s.setInactivePercent(0.00); tracker.saveSession(s); workerAuth.checkHITValid(hitId, workerId, tracker.getStoredSessionInfo(hitId)); }