예제 #1
0
        public void TestAppInboundChannel()
        {
            var consumer     = new MockChannelReaderConsumer();
            var accessSafely = consumer.AfterCompleting(2);

            _appReader.OpenFor(consumer);

            var buffer = new MemoryStream(1024);

            buffer.SetLength(1024);

            var message1    = AppMessage + 1;
            var rawMessage1 = RawMessage.From(0, 0, message1);

            _appChannel.Write(rawMessage1.AsStream(buffer));

            ProbeUntilConsumed(() => accessSafely.ReadFromNow <int>("count") < 1, _appReader);

            Assert.Equal(1, accessSafely.ReadFromNow <int>("count"));
            Assert.Equal(message1, consumer.Messages.First());

            var message2    = AppMessage + 2;
            var rawMessage2 = RawMessage.From(0, 0, message2);

            _appChannel.Write(rawMessage2.AsStream(buffer));

            ProbeUntilConsumed(() => accessSafely.ReadFrom <int>("count") < 2, _appReader);

            Assert.Equal(2, accessSafely.ReadFrom <int>("count"));
            Assert.Equal(message2, consumer.Messages.Last());
        }
예제 #2
0
        public void TestPublisherChannelReader()
        {
            var publisherConsumer = new MockChannelReaderConsumer();
            var publisherAccess   = publisherConsumer.AfterCompleting(1);

            var publisher = new MulticastPublisherReader(
                "test-publisher",
                new Group("237.37.37.2", 37381),
                37389,
                1024,
                publisherConsumer,
                ConsoleLogger.TestInstance());

            var socketWriter = new SocketChannelWriter(
                Address.From(
                    Host.Of("localhost"),
                    37389,
                    AddressType.Main),
                ConsoleLogger.TestInstance());

            socketWriter.Write(RawMessage.From(1, 1, "test-response"), new MemoryStream());

            publisher.ProcessChannel();

            Assert.Equal(1, publisherAccess.ReadFrom <int>("count"));
        }
예제 #3
0
        public void TestPublisherChannelReader()
        {
            var publisherConsumer = new MockChannelReaderConsumer();
            var publisherAccess   = publisherConsumer.AfterCompleting(1);

            var publisher = new MulticastPublisherReader(
                "test-publisher",
                new Group("237.37.37.2", 37381),
                37389,
                1024,
                publisherConsumer,
                ConsoleLogger.TestInstance());

            var socketWriter = new SocketChannelWriter(
                Address.From(
                    Host.Of("localhost"),
                    37389,
                    AddressType.Main),
                ConsoleLogger.TestInstance());

            Func <int> write = () => socketWriter.Write(RawMessage.From(1, 1, "test-response"), new MemoryStream());

            ProbeUntilConsumed(() => publisherAccess.ReadFromNow <int>("count") < 1, publisher, write, 10);

            Assert.Equal(1, publisherAccess.ReadFromExpecting("count", 1, 10_000));
        }
예제 #4
0
        public void TestPublisherChannelReader()
        {
            var publisherCount = 0;
            var accessSafely   = AccessSafely.AfterCompleting(1)
                                 .WritingWith <int>("publisherCount", (value) => publisherCount += value)
                                 .ReadingWith("publisherCount", () => publisherCount);

            var publisherConsumer = new MockChannelReaderConsumer("publisherCount");

            publisherConsumer.UntilConsume = accessSafely;

            var publisher = new MulticastPublisherReader(
                "test-publisher",
                new Group("237.37.37.2", 37381),
                37389,
                1024,
                publisherConsumer,
                ConsoleLogger.TestInstance());

            var socketWriter = new SocketChannelWriter(
                Address.From(
                    Host.Of("localhost"),
                    37389,
                    AddressType.Main),
                ConsoleLogger.TestInstance());

            socketWriter.Write(RawMessage.From(1, 1, "test-response"), new MemoryStream());

            publisher.ProcessChannel();

            Assert.Equal(1, publisherCount);
        }
예제 #5
0
        public void TestMulticastPublishSubscribe()
        {
            var publisherCount  = 0;
            var subscriberCount = 0;
            var accessSafely    = AccessSafely.Immediately()
                                  .WritingWith <int>("publisherCount", (value) => publisherCount += value)
                                  .ReadingWith("publisherCount", () => publisherCount)
                                  .WritingWith <int>("subscriberCount", (value) => subscriberCount += value)
                                  .ReadingWith("subscriberCount", () => subscriberCount);

            var publisherConsumer = new MockChannelReaderConsumer("publisherCount");

            publisherConsumer.UntilConsume = accessSafely;

            var publisher = new MulticastPublisherReader(
                "test-publisher",
                new Group("237.37.37.1", 37771),
                37779,
                1024,
                publisherConsumer,
                ConsoleLogger.TestInstance());

            var subscriber = new MulticastSubscriber(
                "test-subscriber",
                new Group("237.37.37.1", 37771),
                1024,
                10,
                ConsoleLogger.TestInstance());

            var subscriberConsumer = new MockChannelReaderConsumer("subscriberCount");

            subscriberConsumer.UntilConsume = accessSafely;
            subscriber.OpenFor(subscriberConsumer);

            for (int idx = 0; idx < 10; ++idx)
            {
                publisher.SendAvailability();
            }

            publisher.ProcessChannel();

            for (int i = 0; i < 2; ++i)
            {
                subscriber.ProbeChannel();
            }

            subscriberConsumer.UntilConsume.ReadFromExpecting("subscriberCount", 10);

            Assert.Equal(0, publisherCount);
            Assert.Equal(10, subscriberCount);
        }
예제 #6
0
        public void TestPublisherChannelReaderWithMultipleClients()
        {
            var publisherConsumer = new MockChannelReaderConsumer();
            var publisherAccess   = publisherConsumer.AfterCompleting(4);

            var publisher = new MulticastPublisherReader(
                "test-publisher",
                new Group("237.37.37.2", 37391),
                37399,
                1024,
                publisherConsumer,
                ConsoleLogger.TestInstance());

            var client1 = new SocketChannelWriter(
                Address.From(
                    Host.Of("localhost"),
                    37399,
                    AddressType.Main),
                ConsoleLogger.TestInstance());

            var client2 = new SocketChannelWriter(
                Address.From(
                    Host.Of("localhost"),
                    37399,
                    AddressType.Main),
                ConsoleLogger.TestInstance());

            var client3 = new SocketChannelWriter(
                Address.From(
                    Host.Of("localhost"),
                    37399,
                    AddressType.Main),
                ConsoleLogger.TestInstance());

            Func <int> write1 = () => client1.Write(RawMessage.From(1, 1, "test-response1"), new MemoryStream());
            Func <int> write2 = () => client2.Write(RawMessage.From(1, 1, "test-response2"), new MemoryStream());
            Func <int> write3 = () => client3.Write(RawMessage.From(1, 1, "test-response3"), new MemoryStream());

            Func <int> writes = () =>
            {
                write1();
                write2();
                write3();
                return(write1());
            };

            ProbeUntilConsumed(() => publisherAccess.ReadFromNow <int>("count") < 4, publisher, writes, 10);

            Assert.True(publisherAccess.ReadFrom <int>("count") >= 4);
        }
예제 #7
0
        public void TestPublisherChannelReaderWithMultipleClients()
        {
            var publisherConsumer = new MockChannelReaderConsumer();
            var publisherAccess   = publisherConsumer.AfterCompleting(4);

            var publisher = new MulticastPublisherReader(
                "test-publisher",
                new Group("237.37.37.2", 37391),
                37399,
                1024,
                publisherConsumer,
                ConsoleLogger.TestInstance());

            var client1 = new SocketChannelWriter(
                Address.From(
                    Host.Of("localhost"),
                    37399,
                    AddressType.Main),
                ConsoleLogger.TestInstance());

            var client2 = new SocketChannelWriter(
                Address.From(
                    Host.Of("localhost"),
                    37399,
                    AddressType.Main),
                ConsoleLogger.TestInstance());

            var client3 = new SocketChannelWriter(
                Address.From(
                    Host.Of("localhost"),
                    37399,
                    AddressType.Main),
                ConsoleLogger.TestInstance());

            client1.Write(RawMessage.From(1, 1, "test-response1"), new MemoryStream());
            client2.Write(RawMessage.From(1, 1, "test-response2"), new MemoryStream());
            client3.Write(RawMessage.From(1, 1, "test-response3"), new MemoryStream());
            client1.Write(RawMessage.From(1, 1, "test-response1"), new MemoryStream());

            publisher.ProbeChannel();
            publisher.ProbeChannel();
            publisher.ProbeChannel();
            publisher.ProbeChannel();

            Assert.Equal(4, publisherAccess.ReadFrom <int>("count"));
        }
예제 #8
0
        public void TestMulticastPublishSubscribe()
        {
            var publisherConsumer = new MockChannelReaderConsumer();
            var publisherAccess   = publisherConsumer.AfterCompleting(0);

            var publisher = new MulticastPublisherReader(
                "test-publisher",
                new Group("237.37.37.2", 37771),
                37379,
                1024,
                publisherConsumer,
                ConsoleLogger.TestInstance());

            var subscriber = new MulticastSubscriber(
                "test-subscriber",
                new Group("237.37.37.2", 37771),
                1024,
                10,
                ConsoleLogger.TestInstance());

            var subscriberConsumer = new MockChannelReaderConsumer();
            var subscriberAccess   = subscriberConsumer.AfterCompleting(10);

            subscriber.OpenFor(subscriberConsumer);

            for (var idx = 0; idx < 10; ++idx)
            {
                publisher.SendAvailability();
            }

            publisher.ProcessChannel();

            for (var i = 0; i < 2; ++i)
            {
                subscriber.ProbeChannel();
            }

            Assert.Equal(0, publisherAccess.ReadFrom <int>("count"));
            Assert.Equal(10, subscriberAccess.ReadFromExpecting("count", 10, 10_000));
        }
        public void TestChannelWriter()
        {
            var consumer     = new MockChannelReaderConsumer("consume");
            var consumeCount = 0;
            var accessSafely = AccessSafely.Immediately()
                               .WritingWith <int>("consume", (value) => consumeCount += value)
                               .ReadingWith("consume", () => consumeCount);

            consumer.UntilConsume = accessSafely;

            _channelReader.OpenFor(consumer);

            var buffer = new MemoryStream(1024);

            buffer.SetLength(1024);

            var message1    = TestMessage + 1;
            var rawMessage1 = RawMessage.From(0, 0, message1);

            _channelWriter.Write(rawMessage1, buffer);

            ProbeUntilConsumed(() => accessSafely.ReadFrom <int>("consume") < 1, _channelReader);

            Assert.Equal(1, consumer.UntilConsume.ReadFrom <int>("consume"));
            Assert.Equal(message1, consumer.Messages.First());

            var message2    = TestMessage + 2;
            var rawMessage2 = RawMessage.From(0, 0, message2);

            _channelWriter.Write(rawMessage2, buffer);

            ProbeUntilConsumed(() => accessSafely.ReadFrom <int>("consume") < 2, _channelReader);

            Assert.Equal(2, consumer.UntilConsume.ReadFrom <int>("consume"));
            Assert.Equal(message2, consumer.Messages.Last());
        }