public async Task AutoRecomputeTest()
        {
            var stateFactory = Services.StateFactory();
            var time         = Services.GetRequiredService <ITimeService>();
            var c            = await Computed.CaptureAsync(
                _ => time.GetTimeWithOffsetAsync(TimeSpan.FromSeconds(1)));

            var count = 0L;

            using var state = stateFactory.NewLive <DateTime>(
                      o => o.WithInstantUpdates(),
                      async(_, ct) => await c.UseAsync(ct));
            state.Updated += (s, _)
                             => Log.LogInformation($"{++count} -> {s.Value:hh:mm:ss:fff}");

            await TestEx.WhenMetAsync(
                () => count.Should().BeGreaterThan(2),
                TimeSpan.FromSeconds(5));

            var lastCount = count;

            state.Dispose();

            await Task.Delay(1000);

            count.Should().Be(lastCount);
        }
예제 #2
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));
        }
예제 #3
0
        public async Task Test1()
        {
            var epsilon = TimeSpan.FromSeconds(0.5);

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

            var client = Services.GetRequiredService <IClientTimeService>();
            var cTime  = await Computed.CaptureAsync(_ => client.GetTimeAsync());

            cTime.Options.AutoInvalidateTime.Should().Be(ComputedOptions.Default.AutoInvalidateTime);
            if (!cTime.IsConsistent())
            {
                cTime = await cTime.UpdateAsync(false);

                cTime.IsConsistent().Should().BeTrue();
            }
            (DateTime.Now - cTime.Value).Should().BeLessThan(epsilon);

            await TestEx.WhenMetAsync(
                () => cTime.IsConsistent().Should().BeFalse(),
                TimeSpan.FromSeconds(5));

            var time = await cTime.UseAsync();

            (DateTime.Now - time).Should().BeLessThan(epsilon);
        }
예제 #4
0
        public async Task BasicTest()
        {
            var kv = Services.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");

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

            using var kvm = Services.GetRequiredService <ILiveState <KeyValueModel <string> > >();
            var kvc = Services.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);
        }
        public async Task BasicTest()
        {
            if (OSInfo.Kind == OSKind.Unix)
            {
                // Screenshots don't work on Unix
                return;
            }

            var c = await GetScreenshotComputedAsync();

            for (var i = 0; i < 10; i++)
            {
                c.Value.Base64Content.Length.Should().BeGreaterThan(0);
                await TestEx.WhenMetAsync(
                    () => c.IsConsistent().Should().BeFalse(),
                    TimeSpan.FromSeconds(0.5));

                c = await GetScreenshotComputedAsync();
            }
        }
예제 #6
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 state = StateFactory.NewLive <DateTime>(
                      o => o.WithInstantUpdates(),
                      async(_, ct) => await rep.Computed.UseAsync(ct));
            state.Updated += (s, _) => {
                Out.WriteLine($"{s.Value}");
                count++;
            };

            await TestEx.WhenMetAsync(
                () => count.Should().BeGreaterThan(2),
                TimeSpan.FromSeconds(5));
        }