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 ユーザー切断テスト()
        {
            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. 3
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. 4
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. 5
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. 6
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経由で送信。エコーが返る");
                }
        }