Пример #1
0
        public async Task TestSendingMessageToNotConnectedPeer()
        {
            TcpPeerMessenger messenger = new TcpPeerMessenger(new MessageSerializer());
            Task             startTask = messenger.StartAsync();

            try
            {
                await Assert.ThrowsAsync <ArgumentException>(async() =>
                {
                    await messenger.SendMessageAsync("127.0.0.1", 19999, "Hello");
                });
            }
            finally
            {
                messenger.Stop();
                await startTask;
            }
        }
Пример #2
0
        public void TestCommunication()
        {
            TcpPeerMessenger messenger1 = new TcpPeerMessenger(new MessageSerializer());
            TcpPeerMessenger messenger2 = new TcpPeerMessenger(new MessageSerializer());

            async Task testAction()
            {
                string     messageTextSendBy1 = "Hi";
                string     messageTextSendBy2 = "Hello";
                IPEndPoint endPoint1          = null;

                messenger2.ClientAccepted += (s, e) => endPoint1 = e;

                Message messageReceivedFrom1 = null;

                messenger2.MessageReceived += (s, msg) => messageReceivedFrom1 = msg;

                Message messageReceivedFrom2 = null;

                messenger1.MessageReceived += (s, msg) =>
                {
                    messageReceivedFrom2 = msg;
                };

                IPEndPoint endPoint2 = GetMessengerEndPoint(messenger2);
                await messenger1.ConnectToAsync(endPoint2);

                await messenger1.SendMessageAsync(endPoint2, messageTextSendBy1);

                await messenger2.SendMessageAsync(endPoint1, messageTextSendBy2);

                await Task.Delay(300);

                Assert.Equal(messageReceivedFrom1.Body, messageTextSendBy1);
                Assert.Equal(messageReceivedFrom2.Body, messageTextSendBy2);
            }

            StartMessengersAndExecuteActionWhenAllStarted(new[] { messenger1, messenger2 }, testAction);
        }