Пример #1
0
        public async Task BasicTest()
        {
            await using var serving = await WebSocketHost.ServeAsync();

            var sp = Services.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);
        }
Пример #2
0
        public async Task TimerTest()
        {
            await using var serving = await WebSocketHost.ServeAsync();

            var tp = Services.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 = StateFactory.NewLive <DateTime>(
                      o => o.WithZeroUpdateDelay(),
                      async(_, ct) => await rep.Computed.UseAsync(ct));
            state.Updated += s => {
                Out.WriteLine($"Client: {s.Value}");
                count++;
            };

            await TestEx.WhenMet(
                () => count.Should().BeGreaterThan(2),
                TimeSpan.FromSeconds(5));
        }
Пример #3
0
        public async Task NoConnectionTest()
        {
            await using var serving = await WebSocketHost.ServeAsync();

            var tp = Services.GetRequiredService <ITimeService>();

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

            var rep = Replicator.GetOrAdd <DateTime>("NoPublisher", pub.Id);
            await rep.RequestUpdateAsync().AsAsyncFunc()
            .Should().ThrowAsync <WebSocketException>();
        }
Пример #4
0
        public async Task TimerTest()
        {
            await using var serving = await WebSocketHost.ServeAsync();

            var tp = Services.GetRequiredService <ITimeService>();

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

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

            var count = 0;

            using var _ = rep.Computed.AutoUpdate((c, o, _) => {
                Out.WriteLine($"{c.Value}");
                count++;
            });

            await Task.Delay(1100);

            count.Should().BeGreaterThan(2);
        }
Пример #5
0
        public async Task DropReconnectTest()
        {
            if (TestRunnerInfo.GitHub.IsActionRunning)
            {
                // TODO: Fix intermittent failures on GitHub
                return;
            }

            var serving = await WebSocketHost.ServeAsync();

            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.IsConsistent.Should().BeFalse();
            Debug.WriteLine("3");
            state = await state.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));
            state = await state.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>();
            state = await state.UpdateAsync(false);

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

            Debug.WriteLine("WebServer: starting.");
            serving = await WebSocketHost.ServeAsync();

            await Task.Delay(1000);

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

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

            Debug.WriteLine("12");
            state = await state.UpdateAsync(false);

            Debug.WriteLine("13");
            state.Value.Should().BeTrue();

            Debug.WriteLine("100");
            await serving.DisposeAsync();

            Debug.WriteLine("101");
        }