Ejemplo n.º 1
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void shouldExecuteTasksInParallel()
        public virtual void ShouldExecuteTasksInParallel()
        {
            // GIVEN
            TaskExecutor <Void> executor = new DynamicTaskExecutor <Void>(2, 0, 5, _park, this.GetType().Name);
            ControlledTask      task1    = new ControlledTask();
            TestTask            task2    = new TestTask();

            // WHEN
            executor.Submit(task1);
            task1.Latch.waitForAllToStart();
            executor.Submit(task2);
            //noinspection StatementWithEmptyBody
            while (task2.Executed == 0)
            {               // Busy loop
            }
            task1.Latch.finish();
            //noinspection StatementWithEmptyBody
            while (task1.Executed == 0)
            {               // Busy loop
            }
            executor.Close();

            // THEN
            assertEquals(1, task1.Executed);
            assertEquals(1, task2.Executed);
        }
Ejemplo n.º 2
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void shouldIncrementNumberOfProcessorsWhenRunning()
        public virtual void ShouldIncrementNumberOfProcessorsWhenRunning()
        {
            // GIVEN
            TaskExecutor <Void> executor = new DynamicTaskExecutor <Void>(1, 0, 5, _park, this.GetType().Name);
            ControlledTask      task1    = new ControlledTask();
            TestTask            task2    = new TestTask();

            // WHEN
            executor.Submit(task1);
            task1.Latch.waitForAllToStart();
            executor.Submit(task2);
            executor.Processors(1);                 // now at 2
            //noinspection StatementWithEmptyBody
            while (task2.Executed == 0)
            {               // With one additional worker, the second task can execute even if task one is still executing
            }
            task1.Latch.finish();
            //noinspection StatementWithEmptyBody
            while (task1.Executed == 0)
            {               // Busy loop
            }
            executor.Close();

            // THEN
            assertEquals(1, task1.Executed);
            assertEquals(1, task2.Executed);
        }
Ejemplo n.º 3
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void shouldSurfaceTaskErrorInAssertHealthy() throws Exception
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void ShouldSurfaceTaskErrorInAssertHealthy()
        {
            // GIVEN
            TaskExecutor <Void> executor  = new DynamicTaskExecutor <Void>(2, 0, 10, _park, this.GetType().Name);
            IOException         exception = new IOException("Failure");

            // WHEN
            FailingTask failingTask = new FailingTask(exception);

            executor.Submit(failingTask);
            failingTask.Latch.await();
            failingTask.Latch.release();

            // WHEN
            for (int i = 0; i < 5; i++)
            {
                try
                {
                    executor.AssertHealthy();
                    // OK, so the executor hasn't executed the finally block after task was done yet
                    Thread.Sleep(100);
                }
                catch (Exception e)
                {
                    assertTrue(Exceptions.contains(e, exception.Message, exception.GetType()));
                    return;
                }
            }
            fail("Should not be considered healthy after failing task");
        }
Ejemplo n.º 4
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void shouldNoticeBadHealthBeforeBeingClosed()
        public virtual void ShouldNoticeBadHealthBeforeBeingClosed()
        {
            // GIVEN
            TaskExecutor <Void> executor = new DynamicTaskExecutor <Void>(1, 2, 2, _park, "test");
            Exception           panic    = new Exception("My failure");

            // WHEN
            executor.ReceivePanic(panic);

            try
            {
                // THEN
                executor.AssertHealthy();
                fail("Should have failed");
            }
            catch (TaskExecutionPanicException e)
            {
                assertSame(panic, e.InnerException);
            }

            // and WHEN
            executor.Close();

            try
            {
                // THEN
                executor.AssertHealthy();
                fail("Should have failed");
            }
            catch (TaskExecutionPanicException e)
            {
                assertSame(panic, e.InnerException);
            }
        }
Ejemplo n.º 5
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void shouldShutDownOnTaskFailureEvenIfOtherTasksArePending() throws Exception
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void ShouldShutDownOnTaskFailureEvenIfOtherTasksArePending()
        {
            // GIVEN
            TaskExecutor <Void> executor           = new DynamicTaskExecutor <Void>(2, 0, 10, _park, this.GetType().Name);
            IOException         exception          = new IOException("Test message");
            ControlledTask      firstBlockingTask  = new ControlledTask();
            ControlledTask      secondBlockingTask = new ControlledTask();

            executor.Submit(firstBlockingTask);
            executor.Submit(secondBlockingTask);
            firstBlockingTask.Latch.waitForAllToStart();
            secondBlockingTask.Latch.waitForAllToStart();

            FailingTask failingTask = new FailingTask(exception);

            executor.Submit(failingTask);

            ControlledTask thirdBlockingTask = new ControlledTask();

            executor.Submit(thirdBlockingTask);

            // WHEN
            firstBlockingTask.Latch.finish();
            failingTask.Latch.await();
            failingTask.Latch.release();

            // THEN
            AssertExceptionOnSubmit(executor, exception);
            executor.Close();               // call would block if the shutdown as part of failure doesn't complete properly

            secondBlockingTask.Latch.finish();
        }
Ejemplo n.º 6
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void shouldDecrementNumberOfProcessorsWhenRunning() throws Exception
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void ShouldDecrementNumberOfProcessorsWhenRunning()
        {
            // GIVEN
            TaskExecutor <Void> executor = new DynamicTaskExecutor <Void>(2, 0, 5, _park, this.GetType().Name);
            ControlledTask      task1    = new ControlledTask();
            ControlledTask      task2    = new ControlledTask();
            ControlledTask      task3    = new ControlledTask();
            TestTask            task4    = new TestTask();

            // WHEN
            executor.Submit(task1);
            executor.Submit(task2);
            task1.Latch.waitForAllToStart();
            task2.Latch.waitForAllToStart();
            executor.Submit(task3);
            executor.Submit(task4);
            executor.Processors(-1);                 // it started at 2 ^^^
            task1.Latch.finish();
            task2.Latch.finish();
            task3.Latch.waitForAllToStart();
            Thread.Sleep(200);                 // gosh, a Thread.sleep...
            assertEquals(0, task4.Executed);
            task3.Latch.finish();
            executor.Close();

            // THEN
            assertEquals(1, task1.Executed);
            assertEquals(1, task2.Executed);
            assertEquals(1, task3.Executed);
            assertEquals(1, task4.Executed);
        }
Ejemplo n.º 7
0
        public virtual void ShouldCopeWithConcurrentIncrementOfProcessorsAndShutdown()
        {
            // GIVEN
            TaskExecutor <Void> executor = new DynamicTaskExecutor <Void>(1, 2, 2, _park, "test");
            Race race = (new Race()).withRandomStartDelays();

            race.AddContestant(executor.close);
            race.AddContestant(() => executor.Processors(1));

            // WHEN
            race.Go(10, SECONDS);

            // THEN we should be able to do so, there was a recent fix here and before that fix
            // shutdown() would hang, that's why we wait for 10 seconds here to cap it if there's an issue.
        }
Ejemplo n.º 8
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void shouldShutDownOnTaskFailure() throws Exception
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void ShouldShutDownOnTaskFailure()
        {
            // GIVEN
            TaskExecutor <Void> executor = new DynamicTaskExecutor <Void>(30, 0, 5, _park, this.GetType().Name);

            // WHEN
            IOException exception = new IOException("Test message");
            FailingTask task      = new FailingTask(exception);

            executor.Submit(task);
            task.Latch.await();
            task.Latch.release();

            // THEN
            AssertExceptionOnSubmit(executor, exception);
        }
Ejemplo n.º 9
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void shouldRespectMaxProcessors()
        public virtual void ShouldRespectMaxProcessors()
        {
            // GIVEN
            int maxProcessors = 4;
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final TaskExecutor<Void> executor = new DynamicTaskExecutor<>(1, maxProcessors, 10, PARK, getClass().getSimpleName());
            TaskExecutor <Void> executor = new DynamicTaskExecutor <Void>(1, maxProcessors, 10, _park, this.GetType().Name);

            // WHEN/THEN
            assertEquals(1, executor.Processors(0));
            assertEquals(2, executor.Processors(1));
            assertEquals(4, executor.Processors(3));
            assertEquals(4, executor.Processors(0));
            assertEquals(4, executor.Processors(1));
            assertEquals(3, executor.Processors(-1));
            assertEquals(1, executor.Processors(-2));
            assertEquals(1, executor.Processors(-2));
            assertEquals(1, executor.Processors(0));
            executor.Close();
        }
Ejemplo n.º 10
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void shouldExecuteMultipleTasks()
        public virtual void ShouldExecuteMultipleTasks()
        {
            // GIVEN
            TaskExecutor <Void> executor = new DynamicTaskExecutor <Void>(30, 0, 5, _park, this.GetType().Name);

            ExpensiveTask[] tasks = new ExpensiveTask[1000];

            // WHEN
            for (int i = 0; i < tasks.Length; i++)
            {
                executor.Submit(tasks[i] = new ExpensiveTask(10));
            }
            executor.Close();

            // THEN
            foreach (ExpensiveTask task in tasks)
            {
                assertEquals(1, task.Executed);
            }
        }
Ejemplo n.º 11
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void shouldLetShutdownCompleteInEventOfPanic() throws Exception
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void ShouldLetShutdownCompleteInEventOfPanic()
        {
            // GIVEN
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final TaskExecutor<Void> executor = new DynamicTaskExecutor<>(2, 0, 10, PARK, getClass().getSimpleName());
            TaskExecutor <Void> executor  = new DynamicTaskExecutor <Void>(2, 0, 10, _park, this.GetType().Name);
            IOException         exception = new IOException("Failure");

            // WHEN
            FailingTask failingTask = new FailingTask(exception);

            executor.Submit(failingTask);
            failingTask.Latch.await();

            // WHEN
            using (OtherThreadExecutor <Void> closer = new OtherThreadExecutor <Void>("closer", null))
            {
                Future <Void> shutdown = closer.ExecuteDontWait(state =>
                {
                    executor.Close();
                    return(null);
                });
                while (!closer.WaitUntilWaiting().isAt(typeof(DynamicTaskExecutor), "close"))
                {
                    Thread.Sleep(10);
                }

                // Here we've got a shutdown call stuck awaiting queue to be empty (since true was passed in)
                // at the same time we've got a FailingTask ready to throw its exception and another task
                // sitting in the queue after it. Now make the task throw that exception.
                failingTask.Latch.release();

                // Some time after throwing this, the shutdown request should have been completed.
                shutdown.get();
            }
        }
Ejemplo n.º 12
0
 internal Processor(DynamicTaskExecutor <LOCAL> outerInstance, string name) : base(name)
 {
     this._outerInstance      = outerInstance;
     UncaughtExceptionHandler = SILENT_UNCAUGHT_EXCEPTION_HANDLER;
     start();
 }