Ejemplo n.º 1
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test void awaitingShutdownMustBlockUntilAllMessagesHaveBeenProcessed() throws Exception
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        internal virtual void AwaitingShutdownMustBlockUntilAllMessagesHaveBeenProcessed()
        {
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final Event specialShutdownObservedEvent = new Event();
            Event specialShutdownObservedEvent = new Event();

//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final java.util.concurrent.CountDownLatch awaitStartLatch = new java.util.concurrent.CountDownLatch(1);
            System.Threading.CountdownEvent awaitStartLatch = new System.Threading.CountdownEvent(1);
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final EventConsumer consumer = new EventConsumer();
            EventConsumer consumer = new EventConsumer();
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final AsyncEvents<Event> asyncEvents = new AsyncEvents<>(consumer, AsyncEvents.Monitor_Fields.NONE);
            AsyncEvents <Event> asyncEvents = new AsyncEvents <Event>(consumer, AsyncEvents.Monitor_Fields.None);

            _executor.submit(asyncEvents);

            // Wait for the background thread to start processing events
            do
            {
                asyncEvents.Send(new Event());
            } while (consumer.EventsProcessed.take().processedBy == Thread.CurrentThread);

            // Start a thread that awaits the termination
//JAVA TO C# CONVERTER WARNING: Java wildcard generics have no direct equivalent in .NET:
//ORIGINAL LINE: java.util.concurrent.Future<?> awaitShutdownFuture = executor.submit(() ->
            Future <object> awaitShutdownFuture = _executor.submit(() =>
            {
                awaitStartLatch.Signal();
                asyncEvents.AwaitTermination();
                consumer.EventsProcessed.offer(specialShutdownObservedEvent);
            });

            awaitStartLatch.await();

            // Send 5 events
            asyncEvents.Send(new Event());
            asyncEvents.Send(new Event());
            asyncEvents.Send(new Event());
            asyncEvents.Send(new Event());
            asyncEvents.Send(new Event());

            // Observe 5 events processed
            assertThat(consumer.EventsProcessed.take(), @is(notNullValue()));
            assertThat(consumer.EventsProcessed.take(), @is(notNullValue()));
            assertThat(consumer.EventsProcessed.take(), @is(notNullValue()));
            assertThat(consumer.EventsProcessed.take(), @is(notNullValue()));
            assertThat(consumer.EventsProcessed.take(), @is(notNullValue()));

            // Observe no events left
            assertThat(consumer.EventsProcessed.poll(20, TimeUnit.MILLISECONDS), @is(nullValue()));

            // Shutdown and await termination
            asyncEvents.Shutdown();
            awaitShutdownFuture.get();

            // Observe termination
            assertThat(consumer.EventsProcessed.take(), sameInstance(specialShutdownObservedEvent));
        }
Ejemplo n.º 2
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test void concurrentlyPublishedEventsMustAllBeProcessed()
        internal virtual void ConcurrentlyPublishedEventsMustAllBeProcessed()
        {
            assertTimeout(ofSeconds(10), () =>
            {
                EventConsumer consumer = new EventConsumer();
                System.Threading.CountdownEvent startLatch = new System.Threading.CountdownEvent(1);
                const int threads               = 10;
                const int iterations            = 2_000;
                AsyncEvents <Event> asyncEvents = new AsyncEvents <Event>(consumer, AsyncEvents.Monitor_Fields.None);
                _executor.submit(asyncEvents);

                ExecutorService threadPool = Executors.newFixedThreadPool(threads);
                ThreadStart runner         = () =>
                {
                    try
                    {
                        startLatch.await();
                    }
                    catch (InterruptedException e)
                    {
                        throw new Exception(e);
                    }

                    for (int i = 0; i < iterations; i++)
                    {
                        asyncEvents.Send(new Event());
                    }
                };
                for (int i = 0; i < threads; i++)
                {
                    threadPool.submit(runner);
                }
                startLatch.countDown();

                Thread thisThread = Thread.CurrentThread;
                int eventCount    = threads * iterations;
                try
                {
                    for (int i = 0; i < eventCount; i++)
                    {
                        Event @event = consumer.Poll(1, TimeUnit.SECONDS);
                        if (@event == null)
                        {
                            i--;
                        }
                        else
                        {
                            assertThat(@event.ProcessedBy, @is(not(thisThread)));
                        }
                    }
                }
                finally
                {
                    asyncEvents.Shutdown();
                    threadPool.shutdown();
                }
            });
        }
Ejemplo n.º 3
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test void mustNotProcessEventInSameThreadWhenNotShutDown() throws Exception
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        internal virtual void MustNotProcessEventInSameThreadWhenNotShutDown()
        {
            EventConsumer consumer = new EventConsumer();

            AsyncEvents <Event> asyncEvents = new AsyncEvents <Event>(consumer, AsyncEvents.Monitor_Fields.None);

            _executor.submit(asyncEvents);

            asyncEvents.Send(new Event());

            Thread processingThread = consumer.Poll(10, TimeUnit.SECONDS).ProcessedBy;

            asyncEvents.Shutdown();

            assertThat(processingThread, @is(not(Thread.CurrentThread)));
        }
Ejemplo n.º 4
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test void eventsMustBeProcessedByBackgroundThread() throws Exception
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        internal virtual void EventsMustBeProcessedByBackgroundThread()
        {
            EventConsumer consumer = new EventConsumer();

            AsyncEvents <Event> asyncEvents = new AsyncEvents <Event>(consumer, AsyncEvents.Monitor_Fields.None);

            _executor.submit(asyncEvents);

            Event firstSentEvent = new Event();

            asyncEvents.Send(firstSentEvent);
            Event firstProcessedEvent = consumer.Poll(10, TimeUnit.SECONDS);

            Event secondSentEvent = new Event();

            asyncEvents.Send(secondSentEvent);
            Event secondProcessedEvent = consumer.Poll(10, TimeUnit.SECONDS);

            asyncEvents.Shutdown();

            assertThat(firstProcessedEvent, @is(firstSentEvent));
            assertThat(secondProcessedEvent, @is(secondSentEvent));
        }
Ejemplo n.º 5
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test void mustProcessEventsDirectlyWhenShutDown()
        internal virtual void MustProcessEventsDirectlyWhenShutDown()
        {
            assertTimeout(ofSeconds(10), () =>
            {
                EventConsumer consumer = new EventConsumer();

                AsyncEvents <Event> asyncEvents = new AsyncEvents <Event>(consumer, AsyncEvents.Monitor_Fields.None);
                _executor.submit(asyncEvents);

                asyncEvents.Send(new Event());
                Thread threadForFirstEvent = consumer.Poll(10, TimeUnit.SECONDS).ProcessedBy;
                asyncEvents.Shutdown();

                assertThat(threadForFirstEvent, @is(not(Thread.CurrentThread)));

                Thread threadForSubsequentEvents;
                do
                {
                    asyncEvents.Send(new Event());
                    threadForSubsequentEvents = consumer.Poll(10, TimeUnit.SECONDS).ProcessedBy;
                } while (threadForSubsequentEvents != Thread.CurrentThread);
            });
        }
Ejemplo n.º 6
0
 public Monitor_Fields(AsyncEvents <T> outerInstance)
 {
     this._outerInstance = outerInstance;
 }