@JepPythonTestContext(jepPythonBasedTest = true)
@Test
public void testSelectWorkerForCurrentCall() throws Exception
{

  Set<String> overloadedWorkers = new HashSet<>();
  List<String> busyCommands = new ArrayList<>();
  busyCommands.add("import time");
  busyCommands.add("time.sleep(5)");
  PythonInterpreterRequest<Void> busyCommandRequest = buildRequestObjectForVoidGenericCommand(busyCommands,
      2,TimeUnit.MILLISECONDS);
  for ( int i = 0; i < jepPythonEngine.getNumWorkerThreads() - 1; i++) {
    InterpreterWrapper aWrapper = jepPythonEngine.getWorkers().get(i);
    aWrapper.runCommands(1L,i,busyCommandRequest);
    assertTrue(aWrapper.isCurrentlyBusy());
    overloadedWorkers.add(aWrapper.getInterpreterId());
  }
  InterpreterWrapper candidateWrapperForExecution = null;
  InterpreterWrapper validCandidateWrapperForExecution = null;
  int counterForNullWorkers = 0;
  int counterForValidWorkers = 0;
  for ( int i = 0; i < jepPythonEngine.getNumWorkerThreads(); i++) {
    candidateWrapperForExecution = jepPythonEngine.selectWorkerForCurrentCall(i);
    if ( candidateWrapperForExecution == null) {
      counterForNullWorkers += 1;
    } else {
      counterForValidWorkers += 1;
      validCandidateWrapperForExecution = candidateWrapperForExecution;
    }
  }
  // numWorker threads  because the select worker calls iterates over all workers to
  // get any of the free workers. We did not give any worker for the 5th worker and hence should pass for all calls
  assertEquals(jepPythonEngine.getNumWorkerThreads(), counterForValidWorkers);
  assertEquals( 0, counterForNullWorkers); // None of the attempts should fail to get a worker
  // Also we can only get that worker which has not been assigned a sleep instruction
  assertFalse(overloadedWorkers.contains(validCandidateWrapperForExecution.getInterpreterId()));
  Thread.sleep(5000); // all the python workers must be free after this line
  // we now test for all workers being busy
  for ( int i = 0; i < jepPythonEngine.getNumWorkerThreads(); i++) {
    InterpreterWrapper aWrapper = jepPythonEngine.getWorkers().get(i);
    aWrapper.runCommands(1L,i,busyCommandRequest);
  }
  for (int i = 0; i < jepPythonEngine.getNumWorkerThreads(); i++) {
    candidateWrapperForExecution = jepPythonEngine.selectWorkerForCurrentCall(i);
    assertNull(candidateWrapperForExecution);
  }
  Thread.sleep(5000); // ensures other tests in this class can use this engine after this test
}