MinOccupancyFirstSchedulingPolicy implements SchedulingPolicy { @Override public ExecutorRepresenter selectExecutor(final Collection<ExecutorRepresenter> executors, final Task task) { final OptionalInt minOccupancy = executors.stream() .map(ExecutorRepresenter::getNumOfRunningTasks) .mapToInt(i -> i).min(); if (!minOccupancy.isPresent()) { throw new RuntimeException("Cannot find min occupancy"); } return executors.stream() .filter(executor -> executor.getNumOfRunningTasks() == minOccupancy.getAsInt()) .findFirst() .orElseThrow(() -> new RuntimeException("No such executor")); } @Inject private  MinOccupancyFirstSchedulingPolicy(); @Override ExecutorRepresenter selectExecutor(final Collection<ExecutorRepresenter> executors, final Task task);  }
@Test public void test() throws InjectionException { final SchedulingPolicy schedulingPolicy = Tang.Factory.getTang().newInjector() .getInstance(MinOccupancyFirstSchedulingPolicy.class); final ExecutorRepresenter a0 = mockExecutorRepresenter(1); final ExecutorRepresenter a1 = mockExecutorRepresenter(2); final ExecutorRepresenter a2 = mockExecutorRepresenter(2); final Task task = mock(Task.class); final List<ExecutorRepresenter> executorRepresenterList = Arrays.asList(a0, a1, a2); assertEquals(a0, schedulingPolicy.selectExecutor(executorRepresenterList, task)); }