Esempio n. 1
0
        public async Task エコーサーバーテスト()
        {
            Peer serverPeer;

            using (var server = new EchoServer())
                using (var conn1 = await Connection.ConnectToServer(server.GetConnectSettings()))
                    using (var conn2 = await Connection.ConnectToServer(server.GetConnectSettings()))
                    {
                        serverPeer = conn1.Server;
                        Assert.IsTrue(conn1.Server.IsConnection);
                        Assert.IsTrue(conn2.Server.IsConnection);
                        for (int i = 0; i < 100; i++)
                        {
                            var text = "Text:" + (i + 1);
                            conn1.Server.Send(To(text));
                            conn2.Server.Send(To(text));
                            Message message;
                            while (!conn1.PollTryReadMessage(out message, TimeSpan.FromSeconds(1)))
                            {
                                ;
                            }
                            Assert.AreEqual(text, To(message));
                            while (!conn2.PollTryReadMessage(out message, TimeSpan.FromSeconds(1)))
                            {
                                ;
                            }
                            Assert.AreEqual(text, To(message));
                        }
                    }
            Assert.IsFalse(serverPeer.IsConnection);
        }
Esempio n. 2
0
        public async Task ローカルP2Pキャンセルテスト()
        {
            using (var host = Connection.StartLocalHost("TestRoom"))
                using (var server = new EchoServer(host))
                {
                    DiscoveryRoom room = null;
                    using (var discovery = new DiscoveryClient())
                    {
                        discovery.Start();
                        room = await discovery.GetRoomAsync("TestRoom");
                    }
                    await Assert.ThrowsExceptionAsync <OperationCanceledException>(() =>
                    {
                        var cancellationTokenSource = new CancellationTokenSource();
                        cancellationTokenSource.Cancel();
                        return(Connection.ConnectToRoom(room, token: cancellationTokenSource.Token));
                    }, "即時キャンセルの場合");

                    await Assert.ThrowsExceptionAsync <OperationCanceledException>(async() =>
                    {
                        var cancellationTokenSource = new CancellationTokenSource();
                        var task = Connection.ConnectToRoom(room, token: cancellationTokenSource.Token);
                        await Task.Yield();
                        cancellationTokenSource.Cancel();
                        await task;
                    }, "即時キャンセルではない場合");
                }
        }
Esempio n. 3
0
        public async Task ユーザー切断テスト()
        {
            using (var server = new EchoServer())
                using (var conn = await Connection.ConnectToServer(server.GetConnectSettings()))
                {
                    Assert.IsTrue(server.PeerEvents.Count > 0);
                    Assert.IsTrue(server.PeerEvents.TryDequeue(out var e));
                    Assert.AreEqual(e.EventType, PeerEvent.Type.Add);
                    Assert.AreEqual(e.Peer.ConnectionId, conn.SelfId);
                    Assert.IsTrue(e.Peer.IsConnection);
                    conn.Dispose();
                    while (true)
                    {
                        await Task.Delay(100);

                        if (server.PeerEvents.Count > 0)
                        {
                            Assert.IsTrue(server.PeerEvents.TryDequeue(out e));
                            Assert.AreEqual(e.EventType, PeerEvent.Type.Remove);
                            Assert.AreEqual(e.Peer.ConnectionId, conn.SelfId);
                            Assert.IsFalse(e.Peer.IsConnection);
                            break;
                        }
                    }
                    Assert.AreEqual(server.Conn.Peers.Count, 0);
                }
        }
Esempio n. 4
0
 public async Task サーバーランダムテスト()
 {
     using (var server = new EchoServer())
     {
         List <Task> tasks = new List <Task>();
         for (int i = 0; i < 100; i++)
         {
             tasks.Add(RandamServerRequest(server.GetConnectSettings()));
         }
         await Task.WhenAll(tasks.ToArray());
     }
 }
Esempio n. 5
0
 public async Task サーバー切断テスト()
 {
     using (var server = new EchoServer())
         using (var conn1 = await Connection.ConnectToServer(server.GetConnectSettings()))
             using (var conn2 = await Connection.ConnectToServer(server.GetConnectSettings()))
                 using (var conn3 = await Connection.ConnectToServer(server.GetConnectSettings()))
                 {
                     string sendmessage = "サーバー切断テスト";
                     server.Conn.Reliable.Broadcast(To(sendmessage));
                     server.Dispose();
                     string msg1 = null;
                     string msg2 = null;
                     string msg3 = null;
                     while (!conn1.Disposed)
                     {
                         if (conn1.TryReadMessage(out var m))
                         {
                             Assert.IsNull(msg1);
                             msg1 = To(m);
                         }
                     }
                     while (!conn2.Disposed)
                     {
                         if (conn2.TryReadMessage(out var m))
                         {
                             Assert.IsNull(msg2);
                             msg2 = To(m);
                         }
                     }
                     while (!conn3.Disposed)
                     {
                         if (conn3.TryReadMessage(out var m))
                         {
                             Assert.IsNull(msg3);
                             msg3 = To(m);
                         }
                     }
                     Assert.AreEqual(sendmessage, msg1);
                     Assert.AreEqual(sendmessage, msg2);
                     Assert.AreEqual(sendmessage, msg3);
                 }
 }
Esempio n. 6
0
        public async Task クライアントキャンセルテスト()
        {
            using (var server = new EchoServer())
            {
                await Assert.ThrowsExceptionAsync <OperationCanceledException>(() =>
                {
                    var cancellationTokenSource = new CancellationTokenSource();
                    cancellationTokenSource.Cancel();
                    return(Connection.ConnectToServer(server.GetConnectSettings(), cancellationTokenSource.Token));
                }, "即時キャンセルの場合");

                await Assert.ThrowsExceptionAsync <OperationCanceledException>(async() =>
                {
                    var cancellationTokenSource = new CancellationTokenSource();
                    var task = Connection.ConnectToServer(server.GetConnectSettings(), cancellationTokenSource.Token);
                    cancellationTokenSource.Cancel();
                    await task;
                }, "即時キャンセルではない場合");
            }
        }
Esempio n. 7
0
        public async Task アクセサテスト()
        {
            using (var server = new EchoServer())
                using (var conn = await Connection.ConnectToServer(server.GetConnectSettings()))
                {
                    server.Conn.Reliable.Target(conn.SelfId).Send(To("PeerChannelAccessor"));

                    Message message;
                    while (!conn.PollTryReadMessage(out message, TimeSpan.FromSeconds(1)))
                    {
                        ;
                    }
                    Assert.AreEqual("PeerChannelAccessor", To(message), "PeerChannelAccessor経由で送信");
                    Assert.AreEqual(DefaultChannel.Reliable, message.ChannelId, "PeerChannelAccessor経由で送信");

                    message.PeerChannel.Send(To("Message.PeerChannelAccessor"));
                    while (!conn.PollTryReadMessage(out message, TimeSpan.FromSeconds(1)))
                    {
                        ;
                    }
                    Assert.AreEqual("Message.PeerChannelAccessor", To(message), "MessageのPeerChannelAccessor経由で送信。エコーが返る");
                }
        }
Esempio n. 8
0
        public async Task ローカルP2Pテスト()
        {
            using (var host = Connection.StartLocalHost("TestRoom"))
                using (var server = new EchoServer(host))
                {
                    DiscoveryRoom room = null;
                    using (var discovery = new DiscoveryClient())
                    {
                        discovery.Start();
                        room = await discovery.GetRoomAsync("TestRoom");
                    }
                    using (var conn1 = await Connection.ConnectToRoom(room))
                        using (var conn2 = await Connection.ConnectToRoom(room))
                        {
                            host.Channel.Bind <Channel.ReliableChannelConfig>(101);
                            conn1.Channel.Bind <Channel.ReliableChannelConfig>(101);
                            conn2.Channel.Bind <Channel.ReliableChannelConfig>(101);


                            for (int i = 0; i < 100; i++)
                            {
                                var text = "Text:" + (i + 1);
                                conn1.Send(host.SelfId, To(text));
                                conn2.Send(host.SelfId, To(text));
                                Message message;
                                while (!conn1.PollTryReadMessage(out message, TimeSpan.FromSeconds(1)))
                                {
                                    ;
                                }
                                Assert.AreEqual(text, To(message));
                                Assert.AreEqual(host.SelfId, message.Peer.ConnectionId);
                                while (!conn2.PollTryReadMessage(out message, TimeSpan.FromSeconds(1)))
                                {
                                    ;
                                }
                                Assert.AreEqual(text, To(message));
                                Assert.AreEqual(host.SelfId, message.Peer.ConnectionId);

                                conn1.Send(conn2.SelfId, To(text));
                                conn2.Send(conn1.SelfId, To(text));
                                while (!conn1.PollTryReadMessage(out message, TimeSpan.FromSeconds(1)))
                                {
                                    ;
                                }
                                Assert.AreEqual(text, To(message));
                                Assert.AreEqual(conn2.SelfId, message.Peer.ConnectionId);
                                while (!conn2.PollTryReadMessage(out message, TimeSpan.FromSeconds(1)))
                                {
                                    ;
                                }
                                Assert.AreEqual(text, To(message));
                                Assert.AreEqual(conn1.SelfId, message.Peer.ConnectionId);

                                host.Send(conn1.SelfId, To(text));
                                host.Send(conn2.SelfId, To(text));
                                while (!conn1.PollTryReadMessage(out message, TimeSpan.FromSeconds(1)))
                                {
                                    ;
                                }
                                Assert.AreEqual(text, To(message));
                                Assert.AreEqual(host.SelfId, message.Peer.ConnectionId);
                                while (!conn2.PollTryReadMessage(out message, TimeSpan.FromSeconds(1)))
                                {
                                    ;
                                }
                                Assert.AreEqual(text, To(message));
                                Assert.AreEqual(host.SelfId, message.Peer.ConnectionId);

                                conn1.Channel[101].Target(host.SelfId).Send(To(text));
                                conn2.Channel[101].Target(host.SelfId).Send(To(text));
                                while (!conn1.PollTryReadMessage(out message, TimeSpan.FromSeconds(1)))
                                {
                                    ;
                                }
                                Assert.AreEqual(text, To(message));
                                Assert.AreEqual(host.SelfId, message.Peer.ConnectionId);
                                while (!conn2.PollTryReadMessage(out message, TimeSpan.FromSeconds(1)))
                                {
                                    ;
                                }
                                Assert.AreEqual(text, To(message));
                                Assert.AreEqual(host.SelfId, message.Peer.ConnectionId);
                            }

                            {
                                var peer1ByHost = host.GetPeer(conn1.SelfId);
                                var peer2ByHost = host.GetPeer(conn2.SelfId);
                                var peer1       = conn2.GetPeer(conn1.SelfId);
                                var peer2       = conn1.GetPeer(conn2.SelfId);
                                Assert.IsTrue(peer1ByHost.IsConnection);
                                Assert.IsTrue(peer2ByHost.IsConnection);
                                Assert.IsTrue(peer1.IsConnection);
                                Assert.IsTrue(peer2.IsConnection);

                                conn1.BroadcastDisconnect();
                                await Task.Delay(20);

                                conn2.TryReadMessage(out _);

                                Assert.IsTrue(peer2ByHost.IsConnection);
                                Assert.IsFalse(peer1ByHost.IsConnection);
                                Assert.IsFalse(peer1.IsConnection);
                                Assert.IsFalse(peer2.IsConnection);

                                conn2.BroadcastDisconnect();
                                await Task.Delay(20);

                                Assert.IsFalse(peer2ByHost.IsConnection);
                            }
                        }
                }
        }