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.WhenMet( () => cTime.IsConsistent().Should().BeFalse(), TimeSpan.FromSeconds(5)); var time = await cTime.UseAsync(); (DateTime.Now - time).Should().BeLessThan(epsilon); }
public async Task AutoRecomputeTest() { 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.WithZeroUpdateDelay(), async(_, ct) => await c.UseAsync(ct)); state.Updated += s => Log.LogInformation($"{++count} -> {s.Value:hh:mm:ss:fff}"); await TestEx.WhenMet( () => count.Should().BeGreaterThan(2), TimeSpan.FromSeconds(5)); var lastCount = count; state.Dispose(); await Task.Delay(1000); count.Should().Be(lastCount); }
public async Task TimerTest() { await using var serving = await WebHost.Serve(); var publisher = WebServices.GetRequiredService <IPublisher>(); var replicator = ClientServices.GetRequiredService <IReplicator>(); var tp = WebServices.GetRequiredService <ITimeService>(); var pub = await publisher.Publish(_ => tp.GetTime()); var rep = replicator.GetOrAdd <DateTime>(pub.Ref); var count = 0; using var state = Services.StateFactory().NewLive <DateTime>( o => o.WithInstantUpdates(), async(_, ct) => await rep.Computed.Use(ct)); state.Updated += (s, _) => { Out.WriteLine($"{s.Value}"); count++; }; await TestEx.WhenMet( () => count.Should().BeGreaterThan(2), TimeSpan.FromSeconds(5)); }
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)); }
public async Task TimerTest() { await using var serving = await WebHost.Serve(); var publisher = WebServices.GetRequiredService <IPublisher>(); var replicator = ClientServices.GetRequiredService <IReplicator>(); var tp = WebServices.GetRequiredService <ITimeService>(); var pub = await publisher.Publish(_ => tp.GetTime()); var rep = replicator.GetOrAdd <DateTime>(pub.Ref); await rep.RequestUpdate().AsAsyncFunc() .Should().CompleteWithinAsync(TimeSpan.FromMinutes(1)); var count = 0; using var state = WebServices.StateFactory().NewComputed <DateTime>( UpdateDelayer.ZeroUpdateDelay, async(_, ct) => await rep.Computed.Use(ct)); state.Updated += (s, _) => { Out.WriteLine($"Client: {s.Value}"); count++; }; await TestEx.WhenMet( () => count.Should().BeGreaterThan(2), TimeSpan.FromSeconds(5)); }
public async Task BasicTest() { await using var _ = await WebHost.Serve(); var kv = WebServices.GetRequiredService <IKeyValueService <string> >(); (await kv.TryGet("")).Should().Be(Option.None <string>()); (await kv.Get("")).Should().BeNull(); await kv.Set("", "1"); (await kv.TryGet("")).Should().Be(Option.Some("1")); (await kv.Get("")).Should().Be("1"); using var kvm = ClientServices.GetRequiredService <IComputedState <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.WhenMet(() => { 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.Set(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() { 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.State; c.IsConsistent.Should().BeFalse(); c.Value.Key.Should().Be(""); c.Value.Value.Should().BeNull(); c.Value.UpdateCount.Should().Be(0); await TestEx.WhenMet(() => { kvm.UpdateError.Should().BeNull(); var c = kvm.State; 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.State.Value.Key, "2"); await Task.Delay(300); c = kvm.State; c.IsConsistent.Should().BeFalse(); c.Value.Value.Should().Be("1"); c.Value.UpdateCount.Should().Be(1); await Task.Delay(1000); c = kvm.State; c.IsConsistent.Should().BeTrue(); c.Value.Value.Should().Be("2"); c.Value.UpdateCount.Should().Be(2); }
public async Task BasicTest() { if (OSInfo.IsAnyUnix) { // Screenshots don't work on Unix return; } var c = await GetScreenshotComputed(); for (var i = 0; i < 10; i++) { c.Value.Base64Content.Length.Should().BeGreaterThan(0); await TestEx.WhenMet( () => c.IsConsistent().Should().BeFalse(), TimeSpan.FromSeconds(0.5)); c = await GetScreenshotComputed(); } }