Beispiel #1
0
        private void submitJoinRunnable(CloseableExecutorService service,
                                        CountdownEvent startLatch,
                                        CountdownEvent latch)
        {
            CancellationTokenSource token = new CancellationTokenSource();

            service.submit(CallableUtils.FromFunc <object>(() =>
            {
                try
                {
                    startLatch.Signal();
                    Console.WriteLine(startLatch.CurrentCount);
                    int sleepTime = 100000;
                    while (!token.Token.IsCancellationRequested && sleepTime >= 0)
                    {
                        sleepTime -= 100;
                        if (Thread.CurrentThread.Join(100))
                        {
                            break;
                        }
                    }
                    if (token.Token.IsCancellationRequested)
                    {
                        Console.WriteLine("Stopped by cancel request");
                    }
                }
                finally
                {
                    latch.Signal();
                }
                return(null);
            }), token);
        }
Beispiel #2
0
        private IFuture <object> submitJoinRunnable(CloseableExecutorService service,
                                                    CountdownEvent startLatch)
        {
            CancellationTokenSource token = new CancellationTokenSource();

            return(service.submit(CallableUtils.FromFunc <object>(() =>
            {
                startLatch.Signal();
                Console.WriteLine(startLatch.CurrentCount);
                Thread.CurrentThread.Join();
                return null;
            }), token));
        }
 public void testBasicCallable()
 {
     CloseableExecutorService service = new CloseableExecutorService(executorService);
     CountdownEvent startLatch = new CountdownEvent(QTY);
     CountdownEvent latch = new CountdownEvent(QTY);
     for (int i = 0; i < QTY; i++)
     {
         submitJoinRunnable(service, startLatch, latch);
     }
     Assert.True(startLatch.Wait(TimeSpan.FromSeconds(15)));
     service.Dispose();
     Assert.True(latch.Wait(TimeSpan.FromSeconds(15)));
 }
Beispiel #4
0
        public void testBasicCallable()
        {
            CloseableExecutorService service    = new CloseableExecutorService(executorService);
            CountdownEvent           startLatch = new CountdownEvent(QTY);
            CountdownEvent           latch      = new CountdownEvent(QTY);

            for (int i = 0; i < QTY; i++)
            {
                submitJoinRunnable(service, startLatch, latch);
            }
            Assert.True(startLatch.Wait(TimeSpan.FromSeconds(15)));
            service.Dispose();
            Assert.True(latch.Wait(TimeSpan.FromSeconds(15)));
        }
        public void testListeningCallable()
        {
            CloseableExecutorService service = new CloseableExecutorService(executorService);
            CountdownEvent startLatch = new CountdownEvent(QTY);
            List<IFuture<object>> futures = new List<IFuture<object>>();
            for ( int i = 0; i<QTY; ++i )
            {
                IFuture<object> future = submitJoinRunnable(service, startLatch);
                futures.Add(future);
            }

            Assert.True(startLatch.Wait(TimeSpan.FromSeconds(15)));
            foreach ( IFuture<object> future in futures )
            {
                future.cancel();
            }
            Thread.Sleep(TimeSpan.FromSeconds(5));
            Assert.AreEqual(0, service.size());
        }
Beispiel #6
0
        public void testListeningCallable()
        {
            CloseableExecutorService service    = new CloseableExecutorService(executorService);
            CountdownEvent           startLatch = new CountdownEvent(QTY);
            List <IFuture <object> > futures    = new List <IFuture <object> >();

            for (int i = 0; i < QTY; ++i)
            {
                IFuture <object> future = submitJoinRunnable(service, startLatch);
                futures.Add(future);
            }

            Assert.True(startLatch.Wait(TimeSpan.FromSeconds(15)));
            foreach (IFuture <object> future in futures)
            {
                future.cancel();
            }
            Thread.Sleep(TimeSpan.FromSeconds(5));
            Assert.AreEqual(0, service.size());
        }
Beispiel #7
0
        public void testPartialRunnable()
        {
            CountdownEvent outsideLatch = new CountdownEvent(1);

            executorService.submit
            (
                new FutureTask <object>(CallableUtils.FromFunc <object>(() =>
            {
                try
                {
                    Thread.CurrentThread.Join();
                }
                finally
                {
                    outsideLatch.Signal();
                }
                return(null);
            }))
            );

            CloseableExecutorService service    = new CloseableExecutorService(executorService);
            CountdownEvent           startLatch = new CountdownEvent(QTY);
            CountdownEvent           latch      = new CountdownEvent(QTY);

            for (int i = 0; i < QTY; ++i)
            {
                submitRunnable(service, startLatch, latch);
            }

            while (service.size() < QTY)
            {
                Thread.Sleep(100);
            }

            Assert.True(startLatch.Wait(TimeSpan.FromSeconds(15)));
            service.Dispose();
            Assert.True(latch.Wait(TimeSpan.FromSeconds(15)));
            Assert.AreEqual(1, outsideLatch.CurrentCount);
        }
Beispiel #8
0
 public void testBasicRunnable()
 {
     try
     {
         CloseableExecutorService service    = new CloseableExecutorService(executorService);
         CountdownEvent           startLatch = new CountdownEvent(QTY);
         CountdownEvent           latch      = new CountdownEvent(QTY);
         for (int i = 0; i < QTY; i++)
         {
             submitRunnable(service, startLatch, latch);
         }
         Assert.True(startLatch.Wait(TimeSpan.FromSeconds(15)));
         service.Dispose();
         Assert.True(latch.Wait(TimeSpan.FromSeconds(15)));
     }
     catch (AssertionException e)
     {
         throw e;
     }
     catch (Exception e)
     {
         Console.WriteLine(e.ToString());
     }
 }
 public void testBasicRunnable()
 {
     try
     {
         CloseableExecutorService service = new CloseableExecutorService(executorService);
         CountdownEvent startLatch = new CountdownEvent(QTY);
         CountdownEvent latch = new CountdownEvent(QTY);
         for (int i = 0; i < QTY; i++)
         {
             submitRunnable(service, startLatch, latch);
         }
         Assert.True(startLatch.Wait(TimeSpan.FromSeconds(15)));
         service.Dispose();
         Assert.True(latch.Wait(TimeSpan.FromSeconds(15)));
     }
     catch ( AssertionException e )
     {
         throw e;
     }
     catch ( Exception e )
     {
         Console.WriteLine(e.ToString());
     }
 }
 private void submitRunnable(CloseableExecutorService service, 
                                 CountdownEvent startLatch, 
                                 CountdownEvent latch)
 {
     CancellationTokenSource token = new CancellationTokenSource();
     service.submit(CallableUtils.FromFunc<object>(() =>
     {
         try
         {
             startLatch.Signal();
             Console.WriteLine(startLatch.CurrentCount);
             int sleepTime = 100000;
             while (!token.Token.IsCancellationRequested && sleepTime >= 0)
             {
                 sleepTime -= 100;
                 Thread.Sleep(100);
             }
             if (token.Token.IsCancellationRequested)
             {
                 Console.WriteLine("Stopped by cancel request");
             }
         }
         finally
         {
             latch.Signal();
         }
         return null;
     }), token);
 }
 private IFuture<object> submitJoinRunnable(CloseableExecutorService service,
                 CountdownEvent startLatch)
 {
     CancellationTokenSource token = new CancellationTokenSource();
     return service.submit(CallableUtils.FromFunc<object>(() =>
     {
         startLatch.Signal();
         Console.WriteLine(startLatch.CurrentCount);
         Thread.CurrentThread.Join();
         return null;
     }), token);
 }
        public void testPartialRunnable()
        {
            CountdownEvent outsideLatch = new CountdownEvent(1);
            executorService.submit
            (
                new FutureTask<object>(CallableUtils.FromFunc<object>(() =>
                {
                    try
                    {
                        Thread.CurrentThread.Join();
                    }
                    finally
                    {
                        outsideLatch.Signal();
                    }
                    return null;
                }))
            );

            CloseableExecutorService service = new CloseableExecutorService(executorService);
            CountdownEvent startLatch = new CountdownEvent(QTY);
            CountdownEvent latch = new CountdownEvent(QTY);
            for ( int i = 0; i<QTY; ++i )
            {
                submitRunnable(service, startLatch, latch);
            }

            while ( service.size() < QTY )
            {
                Thread.Sleep(100);
            }

            Assert.True(startLatch.Wait(TimeSpan.FromSeconds(15)));
            service.Dispose();
            Assert.True(latch.Wait(TimeSpan.FromSeconds(15)));
            Assert.AreEqual(1, outsideLatch.CurrentCount);
        }