Exemplo n.º 1
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);
        }
Exemplo n.º 2
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();
        }
Exemplo n.º 3
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);
        }
Exemplo n.º 4
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);
        }
Exemplo n.º 5
0
 /// <summary>
 /// Gets an awaiter for this awaitable.
 /// </summary>
 public TaskAwaiter <T> GetAwaiter()
 {
     return(ControlledTask <T> .GetAwaiter(this.Tcs.Task));
 }