예제 #1
0
        public async Task TimerTest()
        {
            await using var serving = await WebHost.ServeAsync();

            var publisher  = WebServices.GetRequiredService <IPublisher>();
            var replicator = ClientServices.GetRequiredService <IReplicator>();
            var tp         = WebServices.GetRequiredService <ITimeService>();

            var pub = await publisher.PublishAsync(_ => tp.GetTimeAsync());

            var rep = replicator.GetOrAdd <DateTime>(pub.Ref);
            await rep.RequestUpdateAsync().AsAsyncFunc()
            .Should().CompleteWithinAsync(TimeSpan.FromMinutes(1));

            var count = 0;

            using var state = WebServices.StateFactory().NewLive <DateTime>(
                      o => o.WithInstantUpdates(),
                      async(_, ct) => await rep.Computed.UseAsync(ct));
            state.Updated += (s, _) => {
                Out.WriteLine($"Client: {s.Value}");
                count++;
            };

            await TestEx.WhenMetAsync(
                () => count.Should().BeGreaterThan(2),
                TimeSpan.FromSeconds(5));
        }
예제 #2
0
        public async Task CommandTest()
        {
            await using var _ = await WebHost.ServeAsync();

            // Server commands
            var kv = WebServices.GetRequiredService <IKeyValueService <string> >();

            (await kv.GetAsync("")).Should().BeNull();

            await kv.SetCommandAsync(new IKeyValueService <string> .SetCommand("", "1"));

            (await kv.GetAsync("")).Should().Be("1");

            await WebServices.Commander().CallAsync(new IKeyValueService <string> .SetCommand("", "2"));

            (await kv.GetAsync("")).Should().Be("2");

            // Client commands
            var kvc = ClientServices.GetRequiredService <IKeyValueServiceClient <string> >();

            (await kv.GetAsync("")).Should().Be("2");

            await kvc.SetCommandAsync(new IKeyValueService <string> .SetCommand("", "1"));

            await Task.Delay(100); // Remote invalidation takes some time

            (await kvc.GetAsync("")).Should().Be("1");

            await ClientServices.Commander().CallAsync(new IKeyValueService <string> .SetCommand("", "2"));

            await Task.Delay(100); // Remote invalidation takes some time

            (await kvc.GetAsync("")).Should().Be("2");
        }
예제 #3
0
        public async Task TestRewriteClient()
        {
            await using var serving = await WebHost.ServeAsync();

            var client  = ClientServices.GetRequiredService <IEdgeCaseRewriteClient>();
            var tfv     = ClientServices.GetTypeViewFactory <IEdgeCaseService>();
            var service = tfv.CreateView(client);

            // ReSharper disable once SuspiciousTypeConversion.Global
            (service is TypeView <IEdgeCaseService>).Should().BeTrue();
            // ReSharper disable once SuspiciousTypeConversion.Global
            (service is TypeView <IEdgeCaseRewriteClient, IEdgeCaseService>).Should().BeTrue();

            // This part tests that proxy builder generates
            // a different type for each combination of <TView, TImplementation>
            var otherClient  = ClientServices.GetRequiredService <IEdgeCaseClient>();
            var otherService = tfv.CreateView(otherClient);

            service.GetType().Should().NotBeSameAs(otherService.GetType());

            ThrowIfContainsErrorExceptionType              = typeof(ServiceException);
            ThrowIfContainsErrorNonComputeExceptionType    = typeof(ServiceException);
            ThrowIfContainsErrorRewriteErrorsExceptionType = typeof(ServiceException);

            await ActualTest(service);
        }
예제 #4
0
        public async Task ConnectToPublisherTest()
        {
            await using var serving = await WebHost.ServeAsync();

            var channel = await ConnectToPublisherAsync();

            channel.Writer.Complete();
        }
예제 #5
0
        public async Task Test1()
        {
            var epsilon = TimeSpan.FromSeconds(0.5);

            await using var serving = await WebHost.ServeAsync();

            var client = ClientServices.GetRequiredService <IClientTimeService>();
            var cTime  = await Computed.CaptureAsync(_ => client.GetTimeAsync(default));
예제 #6
0
        public async Task TestClient()
        {
            await using var serving = await WebHost.ServeAsync();

            var client  = ClientServices.GetRequiredService <IEdgeCaseClient>();
            var tfv     = ClientServices.GetTypeViewFactory <IEdgeCaseService>();
            var service = tfv.CreateView(client);

            await ActualTest(service);
        }
예제 #7
0
        public async Task ServerTimeModelTest1()
        {
            await using var serving = await WebHost.ServeAsync();

            using var stm = ClientServices.GetRequiredService <ILiveState <ServerTimeModel1> >();

            var c = stm.Computed;

            c.IsConsistent().Should().BeFalse();
            c.Value.Time.Should().Be(default);
예제 #8
0
        public async Task ContainerConfigTest()
        {
            await using var serving = await WebHost.ServeAsync();

            var agentInfo1 = WebServices.GetRequiredService <AgentInfo>();
            var agentInfo2 = Services.GetRequiredService <AgentInfo>();
            var notifier1  = WebServices.GetRequiredService <IOperationCompletionNotifier>();
            var notifier2  = Services.GetRequiredService <IOperationCompletionNotifier>();

            agentInfo1.Should().NotBe(agentInfo2);
            agentInfo1.Id.Should().NotBe(agentInfo2.Id);
            notifier1.Should().NotBe(notifier2);
        }
예제 #9
0
        public async Task BasicTest()
        {
            await using var _ = await WebHost.ServeAsync();

            var kv = WebServices.GetRequiredService <IKeyValueService <string> >();

            (await kv.TryGetAsync("")).Should().Be(Option.None <string>());
            (await kv.GetAsync("")).Should().BeNull();
            await kv.SetAsync("", "1");

            (await kv.TryGetAsync("")).Should().Be(Option.Some("1"));
            (await kv.GetAsync("")).Should().Be("1");

            using var kvm = ClientServices.GetRequiredService <ILiveState <KeyValueModel <string> > >();
            var kvc = ClientServices.GetRequiredService <IKeyValueServiceClient <string> >();

            // First read
            var c = kvm.Computed;

            c.IsConsistent().Should().BeFalse();
            c.Value.Key.Should().Be("");
            c.Value.Value.Should().BeNull();
            c.Value.UpdateCount.Should().Be(0);

            await TestEx.WhenMetAsync(() => {
                var snapshot = kvm.Snapshot;
                snapshot.Computed.HasValue.Should().BeTrue();
                var c = snapshot.Computed;
                c.IsConsistent().Should().BeTrue();
                c.Value.Key.Should().Be("");
                c.Value.Value.Should().Be("1");
                c.Value.UpdateCount.Should().Be(1);
            }, TimeSpan.FromSeconds(1));

            // Update
            await kvc.SetAsync(kvm.Computed.Value.Key, "2");

            await Task.Delay(300);

            c = kvm.Computed;
            c.IsConsistent().Should().BeFalse();
            c.Value.Value.Should().Be("1");
            c.Value.UpdateCount.Should().Be(1);

            await Task.Delay(1000);

            c = kvm.Computed;
            c.IsConsistent().Should().BeTrue();
            c.Value.Value.Should().Be("2");
            c.Value.UpdateCount.Should().Be(2);
        }
예제 #10
0
        public async Task NoConnectionTest()
        {
            await using var serving = await WebHost.ServeAsync();

            var publisher  = WebServices.GetRequiredService <IPublisher>();
            var replicator = ClientServices.GetRequiredService <IReplicator>();
            var tp         = WebServices.GetRequiredService <ITimeService>();

            var pub = await publisher.PublishAsync(_ => tp.GetTimeAsync());

            var rep = replicator.GetOrAdd <DateTime>(("NoPublisher", pub.Id));
            await rep.RequestUpdateAsync().AsAsyncFunc()
            .Should().ThrowAsync <WebSocketException>();
        }
예제 #11
0
        public async Task MultiHostInvalidationTest()
        {
            var users = Services.GetRequiredService <IUserService>();

            await using var _ = await WebHost.ServeAsync();

            var webUsers = WebServices.GetRequiredService <IUserService>();

            async Task PingPong(IUserService users1, IUserService users2, User user)
            {
                var count0 = await users1.CountAsync();

                (await users2.CountAsync()).Should().Be(count0);

                await users1.CreateAsync(new(user));

                (await users1.CountAsync()).Should().Be(++count0);

                await DelayAsync(0.5);

                var user2 = await users2.TryGetAsync(user.Id);

                user2.Should().NotBeNull();
                user2 !.Id.Should().Be(user.Id);
                (await users2.CountAsync()).Should().Be(count0);
            }

            for (var i = 0; i < 5; i++)
            {
                var id1 = i * 2;
                var id2 = id1 + 1;
                Out.WriteLine($"{i}: ping...");
                await PingPong(users, webUsers, new User()
                {
                    Id = id1, Name = id1.ToString()
                });

                Out.WriteLine($"{i}: pong...");
                await PingPong(users, webUsers, new User()
                {
                    Id = id2, Name = id2.ToString()
                });

                // await PingPong(webUsers, users, new User() { Id = id2, Name = id2.ToString()});
            }
        }
예제 #12
0
        public async Task BasicTest()
        {
            await using var serving = await WebHost.ServeAsync();

            var authServer     = WebServices.GetRequiredService <IServerSideAuthService>();
            var authClient     = ClientServices.GetRequiredService <IAuthService>();
            var sessionFactory = ClientServices.GetRequiredService <ISessionFactory>();
            var sessionA       = sessionFactory.CreateSession();
            var sessionB       = sessionFactory.CreateSession();
            var alice          = new User("Local", "alice", "Alice");
            var bob            = new User("Local", "bob", "Bob");
            var guest          = new User("<guest>");

            var session = sessionA;
            await WebServices.Commander().CallAsync(new SignInCommand(bob, session).MarkServerSide());

            var user = await authServer.GetUserAsync(session);

            user.Name.Should().Be(bob.Name);

            user = await authClient.GetUserAsync(sessionA);

            user.Name.Should().Be(bob.Name);
            user = await authClient.GetUserAsync(session);

            user.Name.Should().Be(bob.Name);

            session = sessionB;
            user    = await authClient.GetUserAsync(session);

            user.Id.Should().Be(sessionB.Id);
            user.Name.Should().Be(User.GuestName);

            session = sessionFactory.CreateSession();
            user    = await authClient.GetUserAsync(session);

            // User.Id should be equal to new AuthSession.Id
            user.Id.Length.Should().BeGreaterThan(8);
            user.Name.Should().Be(User.GuestName);
        }
예제 #13
0
        public async Task ExceptionTest()
        {
            await using var _ = await WebHost.ServeAsync();

            var kv = WebServices.GetRequiredService <IKeyValueService <string> >();

            try {
                await kv.GetAsync("error");
            }
            catch (ApplicationException ae) {
                ae.Message.Should().Be("Error!");
            }

            var kvc = ClientServices.GetRequiredService <IKeyValueServiceClient <string> >();

            try {
                await kvc.GetAsync("error");
            }
            catch (ApplicationException ae) {
                ae.Message.Should().Be("Error!");
            }
        }
        public async Task BasicTest()
        {
            if (OSInfo.IsAnyUnix)
            {
                // Screenshots don't work on Unix
                return;
            }

            var epsilon = TimeSpan.FromSeconds(0.5);

            await using var serving = await WebHost.ServeAsync();

            var service = ClientServices.GetRequiredService <IScreenshotServiceClient>();

            ScreenshotController.CallCount = 0;
            for (int i = 0; i < 20; i++)
            {
                var screenshot = await service.GetScreenshotAsync(100);

                (DateTime.Now - screenshot.CapturedAt).Should().BeLessThan(epsilon);
                await Task.Delay(TimeSpan.FromSeconds(0.1));
            }
            ScreenshotController.CallCount.Should().BeLessThan(10);
        }
예제 #15
0
        public async Task BasicTest()
        {
            await using var serving = await WebHost.ServeAsync();

            var publisher  = WebServices.GetRequiredService <IPublisher>();
            var replicator = ClientServices.GetRequiredService <IReplicator>();

            using var scope = WebServices.CreateScope();
            var sp = scope.ServiceProvider.GetRequiredService <ISimplestProvider>();

            sp.SetValue("");
            var p1 = await publisher.PublishAsync(_ => sp.GetValueAsync());

            p1.Should().NotBeNull();

            var r1  = replicator.GetOrAdd <string>(p1.Ref, true);
            var r1c = await r1.Computed.UpdateAsync(false);

            r1c.IsConsistent().Should().BeTrue();
            r1c.Value.Should().Be("");
            r1.Computed.Should().Be(r1c);

            sp.SetValue("1");
            await Task.Delay(100);

            r1c.IsConsistent().Should().BeFalse();
            r1.Computed.Should().Be(r1c);

            r1c = await r1c.UpdateAsync(false);

            r1c.Value.Should().Be("1");

            var r1c1 = await r1c.UpdateAsync(false);

            r1c1.Should().Be(r1c);
        }
예제 #16
0
        public async Task BasicTest2()
        {
            await using var serving = await WebHost.ServeAsync();

            var authServer     = WebServices.GetRequiredService <IServerSideAuthService>();
            var authClient     = ClientServices.GetRequiredService <IAuthService>();
            var authLocal      = Services.GetRequiredService <IServerSideAuthService>();
            var sessionFactory = ClientServices.GetRequiredService <ISessionFactory>();
            var sessionA       = sessionFactory.CreateSession();
            var sessionB       = sessionFactory.CreateSession();
            var bob            = new User("", "Bob")
                                 .WithClaim("id", "bob")
                                 .WithIdentity("g:1");

            var session = sessionA;
            await authServer.SignInAsync(new SignInCommand(session, bob).MarkServerSide());

            var user = await authServer.GetUserAsync(session);

            user.Name.Should().Be(bob.Name);
            long.TryParse(user.Id, out var _).Should().BeTrue();
            user.Claims.Count.Should().Be(1);
            user.Identities.Single(); // Client-side users shouldn't have them

            // Server-side methods to get the same user
            var sameUser = await authServer.TryGetUserAsync(user.Id);

            sameUser !.Id.Should().Be(user.Id);
            sameUser.Name.Should().Be(user.Name);
            sameUser.Identities.Keys.Select(i => i.Id.Value).Should().BeEquivalentTo(new [] { "g:1" });
            bob = user;

            // Checking if the client is able to see the same user & sessions
            user = await authClient.GetUserAsync(sessionA);

            user.Id.Should().Be(bob.Id);
            user.IsAuthenticated.Should().BeTrue();
            user = await authClient.GetUserAsync(session);

            user.Id.Should().Be(bob.Id);
            user.IsAuthenticated.Should().BeTrue();

            // Checking if local service is able to see the same user & sessions
            if (!Options.UseInMemoryAuthService)
            {
                await DelayAsync(0.5);

                user = await authLocal.GetUserAsync(session);

                user.Id.Should().Be(bob.Id);
                user.IsAuthenticated.Should().BeTrue();
            }

            // Checking guest session
            session = sessionB;
            user    = await authClient.GetUserAsync(session);

            user.IsAuthenticated.Should().BeFalse();

            // Checking sign-out
            await authServer.SignOutAsync(new(sessionA));

            user = await authServer.GetUserAsync(sessionA);

            user.IsAuthenticated.Should().BeFalse();
            await DelayAsync(0.5);

            user = await authClient.GetUserAsync(sessionA);

            user.IsAuthenticated.Should().BeFalse();
            if (!Options.UseInMemoryAuthService)
            {
                user = await authLocal.GetUserAsync(sessionA);

                user.IsAuthenticated.Should().BeFalse();
            }
        }
예제 #17
0
        public async Task BasicTest1()
        {
            await using var serving = await WebHost.ServeAsync();

            var authServer     = WebServices.GetRequiredService <IServerSideAuthService>();
            var authClient     = ClientServices.GetRequiredService <IAuthService>();
            var authLocal      = Services.GetRequiredService <IServerSideAuthService>();
            var sessionFactory = ClientServices.GetRequiredService <ISessionFactory>();
            var sessionA       = sessionFactory.CreateSession();
            var sessionB       = sessionFactory.CreateSession();
            var bob            = new User("", "Bob").WithIdentity("g:1");

            var session = sessionA;
            await WebServices.Commander().CallAsync(
                new SignInCommand(session, bob).MarkServerSide());

            var user = await authServer.GetUserAsync(session);

            user.Name.Should().Be(bob.Name);
            long.TryParse(user.Id, out var _).Should().BeTrue();
            user.Claims.Count.Should().Be(0);
            bob = user;

            // Trying to edit user name
            var newName = "Bobby";
            await authClient.EditUserAsync(new(session, newName));

            user = await authServer.GetUserAsync(session);

            user.Name.Should().Be(newName);
            bob = bob with {
                Name = newName
            };

            // Checking if the client is able to see the same user & sessions
            user = await authClient.GetUserAsync(sessionA);

            user.Id.Should().Be(bob.Id);
            user.IsAuthenticated.Should().BeTrue();
            user = await authClient.GetUserAsync(session);

            user.Id.Should().Be(bob.Id);
            user.IsAuthenticated.Should().BeTrue();

            // Checking if local service is able to see the same user & sessions
            if (!Options.UseInMemoryAuthService)
            {
                await DelayAsync(0.5);

                user = await authLocal.GetUserAsync(session);

                user.Id.Should().Be(bob.Id);
                user.IsAuthenticated.Should().BeTrue();
            }

            // Checking guest session
            session = sessionB;
            user    = await authClient.GetUserAsync(session);

            user.IsAuthenticated.Should().BeFalse();

            // Checking sign-out
            await WebServices.Commander().CallAsync(new SignOutCommand(sessionA));

            user = await authServer.GetUserAsync(sessionA);

            user.IsAuthenticated.Should().BeFalse();
            await DelayAsync(0.5);

            user = await authClient.GetUserAsync(sessionA);

            user.IsAuthenticated.Should().BeFalse();
            if (!Options.UseInMemoryAuthService)
            {
                user = await authLocal.GetUserAsync(sessionA);

                user.IsAuthenticated.Should().BeFalse();
            }
        }
예제 #18
0
        public async Task DropReconnectTest()
        {
            if (TestRunnerInfo.IsBuildAgent())
            {
                // TODO: Fix intermittent failures on GitHub
                return;
            }

            var serving = await WebHost.ServeAsync();

            var publisher  = WebServices.GetRequiredService <IPublisher>();
            var replicator = ClientServices.GetRequiredService <IReplicator>();
            var tp         = Services.GetRequiredService <ITimeService>();

            Debug.WriteLine("0");
            var pub = await publisher.PublishAsync(_ => tp.GetTimeAsync());

            var rep = replicator.GetOrAdd <DateTime>(pub.Ref);

            Debug.WriteLine("1");
            await rep.RequestUpdateAsync().AsAsyncFunc()
            .Should().CompleteWithinAsync(TimeSpan.FromMinutes(1));

            Debug.WriteLine("2");
            var state = replicator.GetPublisherConnectionState(pub.Publisher.Id);

            state.Computed.IsConsistent().Should().BeTrue();
            Debug.WriteLine("3");
            await state.Computed.UpdateAsync(false);

            Debug.WriteLine("4");
            state.Should().Be(replicator.GetPublisherConnectionState(pub.Publisher.Id));
            state.Value.Should().BeTrue();

            Debug.WriteLine("WebServer: stopping.");
            await serving.DisposeAsync();

            Debug.WriteLine("WebServer: stopped.");

            // First try -- should fail w/ WebSocketException or ChannelClosedException
            Debug.WriteLine("5");
            await rep.RequestUpdateAsync().AsAsyncFunc()
            .Should().ThrowAsync <Exception>();

            Debug.WriteLine("6");
            state.Should().Be(replicator.GetPublisherConnectionState(pub.Publisher.Id));
            await state.Computed.UpdateAsync(false);

            Debug.WriteLine("7");
            state.Should().Be(replicator.GetPublisherConnectionState(pub.Publisher.Id));
            state.Error.Should().BeAssignableTo <Exception>();

            // Second try -- should fail w/ WebSocketException
            Debug.WriteLine("8");
            await rep.Computed.UpdateAsync(false).AsAsyncFunc()
            .Should().ThrowAsync <WebSocketException>();

            Debug.WriteLine("9");
            rep.UpdateError.Should().BeOfType <WebSocketException>();
            await state.Computed.UpdateAsync(false);

            Debug.WriteLine("10");
            state.Error.Should().BeOfType <WebSocketException>();

            // The remaining part of this test shouldn't work:
            // since the underlying web host is actually re-created on
            // every ServeAsync call, its endpoints change,
            // and moreover, IPublisher, etc. dies there,
            // so reconnect won't happen in this case.
            //
            // TODO: Add similar test relying on Replica Services.

            /*
             * Debug.WriteLine("WebServer: starting.");
             * serving = await WebHost.ServeAsync();
             * await Task.Delay(1000);
             * Debug.WriteLine("WebServer: started.");
             *
             * Debug.WriteLine("11");
             * await rep.RequestUpdateAsync().AsAsyncFunc()
             *  .Should().CompleteWithinAsync(TimeSpan.FromMinutes(1));
             * Debug.WriteLine("12");
             * await state.Computed.UpdateAsync(false);
             * Debug.WriteLine("13");
             * state.Value.Should().BeTrue();
             *
             * Debug.WriteLine("100");
             * await serving.DisposeAsync();
             * Debug.WriteLine("101");
             */
        }
예제 #19
0
        public async Task CommunicationTest()
        {
            await using var serving = await WebHost.ServeAsync();

            var publisher = WebServices.GetRequiredService <IPublisher>();

            using var wss = WebServices.CreateScope();
            var sp = wss.ServiceProvider.GetRequiredService <ISimplestProvider>();

            var cp = CreateChannelPair("c1");

            publisher.ChannelHub.Attach(cp.Channel1).Should().BeTrue();
            var cReader = cp.Channel2.Reader;

            sp.SetValue("");

            var p1 = await publisher.PublishAsync(_ => sp.GetValueAsync());

            p1.Should().NotBeNull();

            Debug.WriteLine("a1");
            await publisher.SubscribeAsync(cp.Channel1, p1, true);

            Debug.WriteLine("a2");
            var m = await cReader.AssertReadAsync();

            m.Should().BeOfType <PublicationStateMessage <string> >()
            .Which.Output !.Value.Value.Should().Be("");
            Debug.WriteLine("a3");
            await cReader.AssertCannotReadAsync();

            Debug.WriteLine("b1");
            sp.SetValue("1");
            Debug.WriteLine("b2");
            m = await cReader.AssertReadAsync();

            Debug.WriteLine("b3");
            m.Should().BeOfType <PublicationStateMessage <string> >()
            .Which.IsConsistent.Should().BeFalse();
            Debug.WriteLine("b4");
            var pm = (PublicationMessage)m;

            pm.PublisherId.Should().Be(publisher.Id);
            pm.PublicationId.Should().Be(p1.Id);
            Debug.WriteLine("b5");
            await cReader.AssertCannotReadAsync();

            Debug.WriteLine("c1");
            sp.SetValue("12");
            // No auto-update after invalidation
            Debug.WriteLine("c2");
            await cReader.AssertCannotReadAsync();

            Debug.WriteLine("d1");
            await publisher.SubscribeAsync(cp.Channel1, p1, true);

            Debug.WriteLine("d2");
            m = await cReader.AssertReadAsync();

            Debug.WriteLine("d3");
            m.Should().BeOfType <PublicationStateMessage <string> >()
            .Which.IsConsistent.Should().BeTrue();
            m.Should().BeOfType <PublicationStateMessage <string> >()
            .Which.Output !.Value.Value.Should().Be("12");
            Debug.WriteLine("d4");
            await cReader.AssertCannotReadAsync();

            Debug.WriteLine("e1");
            await p1.DisposeAsync();

            Debug.WriteLine("e2");
            await cReader.AssertCannotReadAsync();

            Debug.WriteLine("f1");
            await publisher.SubscribeAsync(cp.Channel1, p1, true);

            Debug.WriteLine("f2");
            m = await cReader.AssertReadAsync();

            Debug.WriteLine("f3");
            m.Should().BeOfType <PublicationAbsentsMessage>();
        }