Ejemplo n.º 1
0
        public async Task DisposeTest()
        {
            ITransport transport = CreateTransport();

            try
            {
                await InitializeAsync(transport);

                Assert.True(transport.Running);
                transport.Dispose();
                var boundPeer = new BoundPeer(
                    new PrivateKey().PublicKey,
                    new DnsEndPoint("localhost", 1234));
                var message = new Ping();
                await Assert.ThrowsAsync <ObjectDisposedException>(
                    async() => await transport.StartAsync());

                await Assert.ThrowsAsync <ObjectDisposedException>(
                    async() => await transport.StopAsync(TimeSpan.Zero));

                await Assert.ThrowsAsync <ObjectDisposedException>(
                    async() => await transport.SendMessageAsync(boundPeer, message, default));

                await Assert.ThrowsAsync <ObjectDisposedException>(
                    async() => await transport.SendMessageWithReplyAsync(
                        boundPeer,
                        message,
                        null,
                        default));

                await Assert.ThrowsAsync <ObjectDisposedException>(
                    async() => await transport.SendMessageWithReplyAsync(
                        boundPeer,
                        message,
                        null,
                        3,
                        false,
                        default));

                Assert.Throws <ObjectDisposedException>(
                    () => transport.BroadcastMessage(null, message));
                await Assert.ThrowsAsync <ObjectDisposedException>(
                    async() => await transport.ReplyMessageAsync(message, default));

                // To check multiple Dispose() throws error or not.
                transport.Dispose();
            }
            finally
            {
                transport.Dispose();
            }
        }
Ejemplo n.º 2
0
        public async Task BroadcastMessage()
        {
            var        address    = new PrivateKey().ToAddress();
            ITransport transportA = null;
            ITransport transportB = CreateTransport(
                privateKey: GeneratePrivateKeyOfBucketIndex(address, 0));
            ITransport transportC = CreateTransport(
                privateKey: GeneratePrivateKeyOfBucketIndex(address, 1));
            ITransport transportD = CreateTransport(
                privateKey: GeneratePrivateKeyOfBucketIndex(address, 2));

            var tcsB = new TaskCompletionSource <Message>();
            var tcsC = new TaskCompletionSource <Message>();
            var tcsD = new TaskCompletionSource <Message>();

            transportB.ProcessMessageHandler.Register(MessageHandler(tcsB));
            transportC.ProcessMessageHandler.Register(MessageHandler(tcsC));
            transportD.ProcessMessageHandler.Register(MessageHandler(tcsD));

            Func <Message, Task> MessageHandler(TaskCompletionSource <Message> tcs)
            {
                return(async message =>
                {
                    if (message is Ping)
                    {
                        tcs.SetResult(message);
                    }

                    await Task.Yield();
                });
            }

            try
            {
                await InitializeAsync(transportB);
                await InitializeAsync(transportC);
                await InitializeAsync(transportD);

                var table = new RoutingTable(address, bucketSize: 1);
                table.AddPeer(transportB.AsPeer as BoundPeer);
                table.AddPeer(transportC.AsPeer as BoundPeer);
                table.AddPeer(transportD.AsPeer as BoundPeer);

                transportA = CreateTransport();
                await InitializeAsync(transportA);

                transportA.BroadcastMessage(
                    table.PeersToBroadcast(transportD.AsPeer.Address),
                    new Ping());

                await Task.WhenAll(tcsB.Task, tcsC.Task);

                Assert.IsType <Ping>(tcsB.Task.Result);
                Assert.IsType <Ping>(tcsC.Task.Result);
                Assert.False(tcsD.Task.IsCompleted);

                tcsD.SetCanceled();
            }
            finally
            {
                transportA?.Dispose();
                transportB.Dispose();
                transportC.Dispose();
                transportD.Dispose();
            }
        }