Esempio n. 1
0
        void Start()
        {
            IEventExecutorGroup group = new DefaultEventExecutorGroup();

            bootstrap = new Bootstrap();
            bootstrap.Group(group);
        }
Esempio n. 2
0
        public async Task TestStagedExecution()
        {
            IEventLoopGroup     l  = new DefaultEventLoopGroup(4, new DefaultThreadFactory("l"));
            IEventExecutorGroup e1 = new DefaultEventExecutorGroup(4, new DefaultThreadFactory("e1"));
            IEventExecutorGroup e2 = new DefaultEventExecutorGroup(4, new DefaultThreadFactory("e2"));
            ThreadNameAuditor   h1 = new ThreadNameAuditor();
            ThreadNameAuditor   h2 = new ThreadNameAuditor();
            ThreadNameAuditor   h3 = new ThreadNameAuditor(true);

            IChannel ch = new LocalChannel();

            // With no EventExecutor specified, h1 will be always invoked by EventLoop 'l'.
            ch.Pipeline.AddLast(h1);
            // h2 will be always invoked by EventExecutor 'e1'.
            ch.Pipeline.AddLast(e1, h2);
            // h3 will be always invoked by EventExecutor 'e2'.
            ch.Pipeline.AddLast(e2, h3);

            await l.RegisterAsync(ch);

            await ch.ConnectAsync(_localAddr);

            // Fire inbound events from all possible starting points.
            ch.Pipeline.FireChannelRead("1");
            ch.Pipeline.Context(h1).FireChannelRead("2");
            ch.Pipeline.Context(h2).FireChannelRead("3");
            ch.Pipeline.Context(h3).FireChannelRead("4");
            // Fire outbound events from all possible starting points.
            ch.Pipeline.WriteAsync("5").Ignore();
            ch.Pipeline.Context(h3).WriteAsync("6").Ignore();
            ch.Pipeline.Context(h2).WriteAsync("7").Ignore();
            await ch.Pipeline.Context(h1).WriteAndFlushAsync("8");

            await ch.CloseAsync();

            // Wait until all events are handled completely.
            while (h1._outboundThreadNames.Count < 3 || h3._inboundThreadNames.Count < 3 ||
                   h1._removalThreadNames.Count < 1)
            {
                if (h1._exception.Value != null)
                {
                    throw h1._exception.Value;
                }
                if (h2._exception.Value != null)
                {
                    throw h2._exception.Value;
                }
                if (h3._exception.Value != null)
                {
                    throw h3._exception.Value;
                }

                Thread.Sleep(10);
            }

            string currentName = Thread.CurrentThread.Name;

            try
            {
                // Events should never be handled from the current thread.
                Assert.DoesNotContain(currentName, h1._inboundThreadNames);
                Assert.DoesNotContain(currentName, h2._inboundThreadNames);
                Assert.DoesNotContain(currentName, h3._inboundThreadNames);
                Assert.DoesNotContain(currentName, h1._outboundThreadNames);
                Assert.DoesNotContain(currentName, h2._outboundThreadNames);
                Assert.DoesNotContain(currentName, h3._outboundThreadNames);
                Assert.DoesNotContain(currentName, h1._removalThreadNames);
                Assert.DoesNotContain(currentName, h2._removalThreadNames);
                Assert.DoesNotContain(currentName, h3._removalThreadNames);

                // Assert that events were handled by the correct executor.
                foreach (string name in h1._inboundThreadNames)
                {
                    Assert.StartsWith("l-", name);
                }
                foreach (string name in h2._inboundThreadNames)
                {
                    Assert.StartsWith("e1-", name);
                }
                foreach (string name in h3._inboundThreadNames)
                {
                    Assert.StartsWith("e2-", name);
                }
                foreach (string name in h1._outboundThreadNames)
                {
                    Assert.StartsWith("l-", name);
                }
                foreach (string name in h2._outboundThreadNames)
                {
                    Assert.StartsWith("e1-", name);
                }
                foreach (string name in h3._outboundThreadNames)
                {
                    Assert.StartsWith("e2-", name);
                }
                foreach (string name in h1._removalThreadNames)
                {
                    Assert.StartsWith("l-", name);
                }
                foreach (string name in h2._removalThreadNames)
                {
                    Assert.StartsWith("e1-", name);
                }
                foreach (string name in h3._removalThreadNames)
                {
                    Assert.StartsWith("e2-", name);
                }

                // Assert that the events for the same handler were handled by the same thread.
                HashSet <string> names = new HashSet <string>();
                names.UnionWith(h1._inboundThreadNames);
                names.UnionWith(h1._outboundThreadNames);
                names.UnionWith(h1._removalThreadNames);
                Assert.Single(names);

                names.Clear();
                names.UnionWith(h2._inboundThreadNames);
                names.UnionWith(h2._outboundThreadNames);
                names.UnionWith(h2._removalThreadNames);
                Assert.Single(names);

                names.Clear();
                names.UnionWith(h3._inboundThreadNames);
                names.UnionWith(h3._outboundThreadNames);
                names.UnionWith(h3._removalThreadNames);
                Assert.Single(names);

                // Count the number of events
                Assert.Single(h1._inboundThreadNames);
                Assert.Equal(2, h2._inboundThreadNames.Count);
                Assert.Equal(3, h3._inboundThreadNames.Count);
                Assert.Equal(3, h1._outboundThreadNames.Count);
                Assert.Equal(2, h2._outboundThreadNames.Count);
                Assert.Single(h3._outboundThreadNames);
                Assert.Single(h1._removalThreadNames);
                Assert.Single(h2._removalThreadNames);
                Assert.Single(h3._removalThreadNames);
            }
            catch (Exception)
            {
                //System.out.println("H1I: " + h1.inboundThreadNames);
                //System.out.println("H2I: " + h2.inboundThreadNames);
                //System.out.println("H3I: " + h3.inboundThreadNames);
                //System.out.println("H1O: " + h1.outboundThreadNames);
                //System.out.println("H2O: " + h2.outboundThreadNames);
                //System.out.println("H3O: " + h3.outboundThreadNames);
                //System.out.println("H1R: " + h1.removalThreadNames);
                //System.out.println("H2R: " + h2.removalThreadNames);
                //System.out.println("H3R: " + h3.removalThreadNames);
                throw;
            }
            finally
            {
                Task.WaitAll(
                    l.ShutdownGracefullyAsync(),
                    e1.ShutdownGracefullyAsync(),
                    e2.ShutdownGracefullyAsync());
            }
        }