示例#1
0
        public async Task TestClientStopOnServerStop()
        {
            var client = await TcpReactiveClient.CreateClientConnection(EndPoint);

            client.AssertIsConnected();

            await Task.Delay(1000);

            this.Server.Stop();

            await this.Server.WhenStatusChanged()
            .Where(s => s == RunStatus.Stopped)
            .Take(1);

            var t = client.WhenStatusChanged()
                    .Where(s => s == RunStatus.Stopped)
                    .ToTask();

            var delay = Task.Delay(TimeSpan.FromSeconds(client.RetryDelay.TotalSeconds * client.RetryCount).Add(TimeSpan.FromSeconds(1d)));
            await Task.WhenAny(delay, t);

            if (!t.IsCompleted)
            {
                Assert.Fail("should've completed");
            }

            client.AssertIsNotConnected();
        }
示例#2
0
        public async Task AfterSet_KeepAliveTimeSeconds_EqualsSame()
        {
            var client = await TcpReactiveClient.CreateClientConnection(EndPoint);

            client.KeepAliveTimeSeconds = 200;

            Assert.AreEqual(200, client.KeepAliveTimeSeconds);
        }
示例#3
0
        public async Task AfterSet_KeepAlive_EqualsSame()
        {
            var client = await TcpReactiveClient.CreateClientConnection(EndPoint);

            client.KeepAlive = true;

            Assert.AreEqual(true, client.KeepAlive);
        }
示例#4
0
        public async Task TestCreateConnection()
        {
            var client = await TcpReactiveClient.CreateClientConnection(EndPoint);

            Assert.IsNotNull(client);
            await Assert.ThrowsExceptionAsync <SocketException>(() => TcpReactiveClient.CreateClientConnection(IpAddress, Port ^ 11111).ToTask());

            var client2 = await TcpReactiveClient.CreateClientConnection(EndPoint);

            Assert.IsNotNull(client2);
            client2.AssertIsConnected();
        }
示例#5
0
        public async Task TestWriteAndReceive()
        {
            byte[] bytes            = new byte[] { 2, 3, 10, 8, 16 };
            var    serverClientTask = this.Server.WhenClientStatusChanged().Where(c => c.Status == RunStatus.Started).Take(1).ToTask();
            var    client           = await TcpReactiveClient.CreateClientConnection(EndPoint);

            var serverClient       = await serverClientTask;
            var receivedResultTask = serverClient.WhenDataReceived().Take(1).ToTask();
            var result             = await client.Write(bytes);

            Assert.AreSame(client, result.Client);
            Assert.AreSame(bytes, result.Data);
            Assert.AreEqual(ClientEvent.Write, result.EventType);
            Assert.IsTrue(result.Success);
            var receivedResult = await receivedResultTask;

            Assert.AreSame(serverClient, receivedResult.Client);
            CollectionAssert.AreEqual(bytes, receivedResult.Data);
        }
示例#6
0
        public async Task TestClientReadTimeout()
        {
            var client = await TcpReactiveClient.CreateClientConnection(EndPoint, false);

            client.ReceiveTimeout = client.SendTimeout = TimeSpan.FromSeconds(6d);
            client.Start();

            client.AssertIsConnected();

            var t = client.WhenStatusChanged()
                    .Where(s => s == RunStatus.Stopped)
                    .ToTask();

            await Task.WhenAny(Task.Delay(client.ReceiveTimeout.Add(TimeSpan.FromSeconds(1d))), t);

            if (!t.IsCompleted)
            {
                Assert.Fail("should've completed");
            }

            client.AssertIsNotConnected();
        }
示例#7
0
 public static void AssertIsNotConnected(this TcpReactiveClient client) =>
 Assert.IsTrue(
     client.Status == RunStatus.Stopped &&
     !client.IsConnected());
示例#8
0
 public static IObservable <ClientResult> Write(this TcpReactiveClient client, IHotAPacket packet) =>
 client.Write(packet.ToData());