Пример #1
0
        public async Task Server_ClientWebRepository_CommunicationValid()
        {
            using Server server = new Server(TEST_PORT, new DataRepository(), null);
            using AutoResetEvent
                  clientConnEvent    = new AutoResetEvent(false),
                  clientDisconnEvent = new AutoResetEvent(false);
            server.ServerWebSocketConnection.OnError           += (o, a) => Assert.Fail($"Server error! {a.Exception}");
            server.ServerWebSocketConnection.OnClientConnected += (o, a) => clientConnEvent.Set();
            server.ServerWebSocketConnection.OnClose           += (o, a) => clientDisconnEvent.Set();
            Task serverTask = Task.Run(server.RunServer);

            using (WebRepository client = new WebRepository())
            {
                Assert.IsTrue(await client.OpenRepository(ClientUtil.CreateLocalConnectionString(TEST_PORT)));
                Assert.IsTrue(clientConnEvent.WaitOne(TEST_TIMEOUT));
                Assert.AreEqual((await server.DataRepository.GetAllClients()).Count, (await client.GetAllClients()).Count);
                Assert.AreEqual((await server.DataRepository.GetAllProducts()).Count, (await client.GetAllProducts()).Count);
                Assert.AreEqual((await server.DataRepository.GetAllOrders()).Count, (await client.GetAllOrders()).Count);
                TestObserver testObserver = new TestObserver();
                using (IDisposable _ = server.DataRepository.Subscribe((IObserver <DataChanged <IClient> >)testObserver),
                       __ = server.DataRepository.Subscribe((IObserver <DataChanged <IProduct> >)testObserver))
                {
                    await client.RemoveClient((await client.GetAllClients()).First());

                    await client.RemoveProduct((await client.GetAllProducts()).First());
                }
                Assert.AreEqual(0, testObserver.ClientErrors.Count);
                Assert.AreEqual(0, testObserver.ProductErrors.Count);
                Assert.AreEqual(1, testObserver.ClientNext.Count);
                Assert.AreEqual(1, testObserver.ProductNext.Count);
                Assert.AreEqual(DataChangedAction.Remove, testObserver.ClientNext.Dequeue().Action);
                Assert.AreEqual(DataChangedAction.Remove, testObserver.ProductNext.Dequeue().Action);
            }
            Assert.IsTrue(clientDisconnEvent.WaitOne(TEST_TIMEOUT));
        }