WorkerAuthenticator { public void checkWorkerLimits(String hitId, String workerId, Session existingRecord) throws SimultaneousSessionsException, TooManySessionsException { if (existingRecord == null) existingRecord = tracker.getStoredSessionInfo(hitId); String prevWorkerId = existingRecord == null ? null : existingRecord.getWorkerId(); Collection<Session> allSessions = tracker.getSetSessionInfoForWorker(workerId); if( allSessions == null || allSessions.size() == 0 ) return; int currentlyAssigned = 0; for( Session otherRec : allSessions ) { if ( SessionRecord.status(otherRec) != SessionStatus.COMPLETED ) currentlyAssigned++; } logger.info( String.format("Worker %s already has %d other sessions (%d currently assigned)", workerId, allSessions.size(), currentlyAssigned)); if( specialWorkers.contains(workerId) ) { logger.info("Ignored limits for special worker " + workerId); } else if( !workerId.equals(prevWorkerId) ) { if( currentlyAssigned >= simultaneousSessionLimit ) throw new SimultaneousSessionsException(); if( allSessions.size() >= totalSetLimit ) throw new TooManySessionsException(); } } @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 testLimitNone() throws Exception { String hitId = "testHITId"; String workerId = "testWorkerId"; workerAuth.checkWorkerLimits(hitId, workerId, tracker.getStoredSessionInfo(hitId)); }
@Test(expected=SimultaneousSessionsException.class) public void testLimitConcurrent() throws Exception { String hitId = "testHITId"; String hitId2 = "testHITId2"; String workerId = "testWorkerId"; String assignmentId = "testAssignmentId"; Session takenSession = new Session(); takenSession.setHitId(hitId); takenSession.setWorkerId(workerId); takenSession.setAssignmentId(assignmentId); assertEquals(SessionStatus.ASSIGNED, SessionRecord.status(takenSession)); tracker.saveSession(takenSession); workerAuth.checkWorkerLimits(hitId2, workerId, tracker.getStoredSessionInfo(hitId2)); }
@Test(expected=TooManySessionsException.class) public void testLimitSets() throws Exception { String hitId = "testHITId"; String hitId2 = "testHITId2"; String workerId = "testWorkerId"; String assignmentId = "testAssignmentId"; Session s = new Session(); s.setHitId(hitId); s.setWorkerId(workerId); s.setAssignmentId(assignmentId); s.setInactivePercent(0.0); assertEquals(SessionStatus.COMPLETED, SessionRecord.status(s)); tracker.saveSession(s); workerAuth.checkWorkerLimits(hitId2, workerId, tracker.getStoredSessionInfo(hitId2)); }