package rltoys.experiments.scheduling.tests;

import static rltoys.experiments.scheduling.tests.SchedulerTestsUtils.testScheduler;

import java.io.IOException;

import org.junit.BeforeClass;
import org.junit.Test;

import rltoys.experiments.scheduling.network.NetworkClientScheduler;
import rltoys.experiments.scheduling.network.ServerScheduler;
import rltoys.experiments.scheduling.network.internal.Messages;
import rltoys.experiments.scheduling.network.internal.NetworkClassLoader;
import rltoys.experiments.scheduling.schedulers.LocalScheduler;

public class SchedulerTest {
  private static final String Localhost = "localhost";
  private static final int Port = 5000;
  public static final int Timeout = 1000000;

  @BeforeClass
  static public void junitMode() {
    NetworkClassLoader.enableForceNetworkClassResolution();
    Messages.disableVerbose();
    // Messages.enableDebug();
  }

  @Test(timeout = Timeout)
  public void testJobScheduler() {
    LocalScheduler scheduler = new LocalScheduler(10);
    SchedulerTestsUtils.testScheduler(scheduler);
    scheduler.dispose();
  }

  @Test(timeout = Timeout)
  public void testServerScheduler() throws IOException {
    ServerScheduler scheduler = new ServerScheduler(Port, 10);
    testScheduler(scheduler);
    scheduler.dispose();
  }

  @Test(timeout = Timeout)
  public void testServerSchedulerWithUniqueClient() throws IOException {
    ServerScheduler scheduler = new ServerScheduler(Port, 0);
    NetworkClientScheduler client01 = new NetworkClientScheduler(1, Localhost, Port);
    client01.start();
    testScheduler(scheduler);
    client01.dispose();
    scheduler.dispose();
  }

  @Test(timeout = Timeout)
  public void testServerSchedulerWithUniqueClientMultipleThreads() throws IOException {
    ServerScheduler scheduler = new ServerScheduler(Port, 0);
    NetworkClientScheduler client01 = new NetworkClientScheduler(2, Localhost, Port);
    client01.start();
    testScheduler(scheduler);
    scheduler.dispose();
    client01.dispose();
  }

  @Test(timeout = Timeout)
  public void testServerSchedulerWithMultipleClients() throws IOException {
    ServerScheduler scheduler = new ServerScheduler(Port, 0);
    NetworkClientScheduler client01 = new NetworkClientScheduler(10, Localhost, Port);
    NetworkClientScheduler client02 = new NetworkClientScheduler(10, Localhost, Port);
    client01.start();
    client02.start();
    testScheduler(scheduler);
    scheduler.dispose();
    client01.dispose();
    client02.dispose();
  }
}

@@ -152,16 +152,99 @@
 ception;
+%0Aimport java.util.ArrayList;%0Aimport java.util.List;%0A%0Aimport junit.framework.Assert;
 %0A%0Aimport
@@ -414,32 +414,98 @@
 erverScheduler;%0A
+import rltoys.experiments.scheduling.network.internal.LocalQueue;%0A
 import rltoys.ex
@@ -689,16 +689,164 @@
 heduler;
+%0Aimport rltoys.experiments.scheduling.tests.SchedulerTestsUtils.Job;%0Aimport rltoys.experiments.scheduling.tests.SchedulerTestsUtils.JobDoneListener;
 %0A%0Apublic
@@ -1299,24 +1299,24 @@
 eduler(10);%0A
-
     Schedule
@@ -1375,32 +1375,810 @@
 dispose();%0A  %7D%0A%0A
+  @Test(timeout = Timeout)%0A  public void testJobSchedulerWithManyIterators() %7B%0A    LocalScheduler scheduler = new LocalScheduler(10);%0A    final int NbJobs = 100;%0A    for (int i = 0; i %3C 2; i++) %7B%0A      List%3CJob%3E jobs = SchedulerTestsUtils.createJobs(NbJobs);%0A      SchedulerTestsUtils.assertAreDone(jobs, false);%0A      JobDoneListener listener = SchedulerTestsUtils.createListener();%0A      for (Job job : jobs) %7B%0A        List%3CRunnable%3E oneElement = new ArrayList%3CRunnable%3E();%0A        oneElement.add(job);%0A        ((LocalQueue) scheduler.queue()).add(oneElement.iterator(), listener);%0A      %7D%0A      scheduler.runAll();%0A      Assert.assertEquals(NbJobs, listener.nbJobDone());%0A      SchedulerTestsUtils.assertAreDone(listener.jobDone(), true);%0A    %7D%0A    scheduler.dispose();%0A  %7D%0A%0A
   @Test(timeout 

Test with many iterators