public async Task Random_packets_sent_only_before_first_packet_to_first_destination()
        {
            var settings = new RandomFirstSessionPrefixInterceptor.Settings {
                CountMin = 2, CountMax = 2
            };
            var interceptor = new RandomFirstSessionPrefixInterceptor(this._bufferPool, settings, new NullLoggerFactory());
            var client      = new FakeUdpClient();

            var destinationEndPoint = FakeEndPoints.CreateRemote();

            var payload = new ArraySegment <byte>(new byte[] { 1, 2, 3, 4 });
            var isSendingHandledByInterceptor = await interceptor
                                                .TryInterceptOutgoingAsync(payload, destinationEndPoint, client)
                                                .ConfigureAwait(true);

            isSendingHandledByInterceptor.Should().BeFalse();

            payload = new ArraySegment <byte>(new byte[] { 5, 6, 7, 8 });
            isSendingHandledByInterceptor = await interceptor
                                            .TryInterceptOutgoingAsync(payload, destinationEndPoint, client)
                                            .ConfigureAwait(true);

            isSendingHandledByInterceptor.Should().BeFalse();

            var packets = client.WaitForSentPackets(2);

            packets[0].Packet.Length.Should().BeInRange(settings.BytesMin, settings.BytesMax);
            packets[1].Packet.Length.Should().BeInRange(settings.BytesMin, settings.BytesMax);
        }
Ejemplo n.º 2
0
        public void Proxy_terminates_with_dispose()
        {
            using (var clientStream = new LoopbackTcpStream())
            {
                var boundUdpClient    = new FakeUdpClient();
                var relayingUdpClient = new FakeUdpClient();
                var timerFactory      = new ManuallyInvokedTimerFactory();
                var bufferPool        = new DebugArrayPool <byte>();
                var interceptor       = A.Fake <IDatagramInterceptor>();

                using (var proxy = new UdpProxy(
                           clientStream,
                           new FakeUdpClientFactory(boundUdpClient, relayingUdpClient),
                           interceptor,
                           bufferPool,
                           timerFactory,
                           new NullLoggerFactory()))
                {
                    var task = proxy.RunAsync(CancellationToken.None);
                    proxy.Dispose();

                    task.Awaiting(x => x).Should().Throw <Exception>();
                }

                A.CallTo(() => interceptor.Dispose()).MustHaveHappenedOnceExactly();
            }
        }
Ejemplo n.º 3
0
        public void Dispose_aborts_pending_receive()
        {
            var client   = new FakeUdpClient();
            var readTask = client.ReceiveAsync(new byte[1], 0, FakeEndPoints.CreateLocal());

            client.Dispose();
            readTask.Awaiting(t => t).Should().Throw <Exception>();
        }
Ejemplo n.º 4
0
        public async Task Written_packet_can_be_retrieved()
        {
            using (var client = new FakeUdpClient())
            {
                var destination = FakeEndPoints.CreateRemote();
                var packet      = new byte[] { 1, 2, 3, 4, 5 };
                await client.SendAsync(packet, 0, packet.Length, destination).ConfigureAwait(true);

                var received = client.WaitForSentPackets(1).First();
                received.Destination.Should().Be(destination);
                received.Packet.Should().Equal(1, 2, 3, 4, 5);
            }
        }
Ejemplo n.º 5
0
        public async Task Staging_packet_makes_it_available_in_receive()
        {
            using (var client = new FakeUdpClient())
            {
                var remote = FakeEndPoints.CreateRemote();
                client.StageReceivedPacket(remote, 1, 2, 3);
                var buffer = new byte[10];
                var result = await client.ReceiveAsync(buffer, 0, remote).ConfigureAwait(true);

                result.ReceivedBytes.Should().Be(3);
                result.RemoteEndPoint.Should().Be(remote);
                buffer.Take(3).Should().Equal(1, 2, 3);
            }
        }
Ejemplo n.º 6
0
        public void Proxy_terminates_with_client_stream_termination()
        {
            using (var clientStream = new LoopbackTcpStream())
            {
                var boundUdpClient    = new FakeUdpClient();
                var relayingUdpClient = new FakeUdpClient();
                var timerFactory      = new ManuallyInvokedTimerFactory();
                var bufferPool        = new DebugArrayPool <byte>();
                using (var proxy = new UdpProxy(
                           clientStream,
                           new FakeUdpClientFactory(boundUdpClient, relayingUdpClient),
                           new PassthroughInterceptor(),
                           bufferPool,
                           timerFactory,
                           new NullLoggerFactory()))
                {
                    var task = proxy.RunAsync(CancellationToken.None);
                    clientStream.Dispose();

                    task.Awaiting(x => x).Should().Throw <Exception>();
                }
            }
        }