Esempio n. 1
0
        public void UsingLikeAMutex()
        {
            Semaphore semaphore = new Semaphore(1);
            Latch     latch     = new Latch();
            Helper    helper    = new Helper(semaphore, latch);
            Thread    thread    = new Thread(new ThreadStart(helper.Go));

            semaphore.Acquire();
            thread.Start();
            latch.Acquire();
            Assert.IsFalse(helper.gone);
            semaphore.Release();
            thread.Join();
            Assert.IsTrue(helper.gone, "not gone");
        }
Esempio n. 2
0
 public void UsingLikeAMutex ()
 {
     
     Semaphore semaphore = new Semaphore (1);
     Latch latch = new Latch ();
     Helper helper = new Helper (semaphore, latch);
     Thread thread = new Thread (new ThreadStart (helper.Go));
     semaphore.Acquire ();
     thread.Start ();
     latch.Acquire ();
     Assert.IsFalse (helper.gone);
     semaphore.Release ();
     thread.Join ();
     Assert.IsTrue (helper.gone, "not gone");
     
 }
 public void CanBeInterruptedAgainAfterInterruption()
 {
     Latch stop = new Latch ();
     Latch go = new Latch ();
     InterruptAgainStrategy strategy = new InterruptAgainStrategy (go, stop);
     Runner runner = new Runner (strategy);
     Thread t = StartedThread (runner);
     Log ("Thread #" + t.GetHashCode () + " started: interrupting it");
     t.Interrupt ();
     Log ("Thread #" + t.GetHashCode () + " interrupted");
     runner.Interrupted ();
     Log ("artificially signalled interruption");
     go.Acquire ();
     t.Interrupt ();
     stop.Release ();
     Log ("Joining thread #" + t.GetHashCode ());
     t.Join ();
     Assert.IsTrue (strategy.interrupted, "thread could not take any action after interruption");
     Assert.IsTrue (strategy.reInterrupted, "thread could not interrupt itself after interruption");
 }
Esempio n. 4
0
        public void CanBeInterruptedAgainAfterInterruption()
        {
            Latch stop = new Latch();
            Latch go   = new Latch();
            InterruptAgainStrategy strategy = new InterruptAgainStrategy(go, stop);
            Runner runner = new Runner(strategy);
            Thread t      = StartedThread(runner);

            Log("Thread #" + t.GetHashCode() + " started: interrupting it");
            t.Interrupt();
            Log("Thread #" + t.GetHashCode() + " interrupted");
            runner.Interrupted();
            Log("artificially signalled interruption");
            go.Acquire();
            t.Interrupt();
            stop.Release();
            Log("Joining thread #" + t.GetHashCode());
            t.Join();
            Assert.IsTrue(strategy.interrupted, "thread could not take any action after interruption");
            Assert.IsTrue(strategy.reInterrupted, "thread could not interrupt itself after interruption");
        }
Esempio n. 5
0
 public void WaitOnBorrowWhenExausted()
 {
     int n = 100;
     object[] objects = new object[n];
     pool = new SimplePool(new MyFactory(), n);
     for (int i = 0; i < n; i++)
     {
         objects[i] = pool.BorrowObject();
     }
     Latch latch = new Latch();
     ISync sync = new Latch();
     Helper helper = new Helper(latch, sync, pool);
     Thread thread = new Thread(new ThreadStart(helper.UsePool));
     thread.Start();
     Assert.AreEqual(n, pool.NumActive);
     Assert.AreEqual(0, pool.NumIdle);
     object released = objects[n - 1];
     pool.ReturnObject(released);
     latch.Acquire();
     Assert.AreEqual(n, pool.NumActive);
     Assert.AreEqual(0, pool.NumIdle);
     Assert.IsNotNull(helper.gotFromPool, "helper did not get from pool");
     Assert.AreSame(released, helper.gotFromPool, "got unexpected object");
 }
Esempio n. 6
0
 public void WaitForStartedBeforeStopping()
 {
     MyService serviceable = new MyService(sync1, sync2);
     ISync starting = new Latch();
     ISync started = new Latch();
     BlockWhileStartingExecutor executor = 
         new BlockWhileStartingExecutor(starting, started);
     MyServiceSupport support = new MyServiceSupport(executor, serviceable);
     executor.ServiceSupport = support;
     Thread startThread = new Thread(new ThreadStart(executor.Start));
     startThread.Name = "start";
     startThread.Start();
     Log ("start thread started");
     Latch stopping = new Latch();
     Latch stopped = new Latch();
     StoppingHelper helper = new StoppingHelper(support, stopping, stopped);
     Thread stopThread = new Thread(new ThreadStart(helper.Stop));
     stopThread.Name = "stop";
     stopThread.Start();
     Log ("stop thread started: waiting for stopping ...");
     stopping.Acquire();
     Log ("stopping in progress ...");
     Assert.IsFalse(executor.wasStarted);
     Assert.IsFalse(helper.wasStopped, "helper could stop before expected");
     Log ("allow to start ...");
     starting.Release();
     Log ("waiting for started ...");
     started.Acquire();
     Assert.IsTrue(executor.wasStarted);
     stopped.Acquire();
     Log ("waiting for stop ...");
     Assert.IsTrue(helper.wasStopped);
     Log ("stopped ...");
 }