diff --git a/RxBlazorLightCoreTestBase/ServiceFixture.State.cs b/RxBlazorLightCoreTestBase/ServiceFixture.State.cs index 5bfbabb..72921c7 100644 --- a/RxBlazorLightCoreTestBase/ServiceFixture.State.cs +++ b/RxBlazorLightCoreTestBase/ServiceFixture.State.cs @@ -179,28 +179,24 @@ public IDisposable ObserveState(IStateObserverAsync observer) public IDisposable ObserveStateComplex(IStateObserverAsync observer, ITestOutputHelper outputHelper) { var startObservable = Observable - .Return(false) - .Select(_ => Observable.DeferAsync(async token => + .FromAsync(async ct => { outputHelper.WriteLine("StartFirstWork"); - await Task.Delay(1000, token); + await Task.Delay(1000, ct); IntState.SetValue(10); outputHelper.WriteLine("StopFirstWork"); return Observable.Timer(TimeSpan.FromSeconds(1)); - })) - .Switch(); - + }); + var stopObservable = Observable - .Return(false) - .Select(_ => Observable.DeferAsync(async token => + .FromAsync(async ct => { outputHelper.WriteLine("StartLastWork"); - await Task.Delay(1000, token); + await Task.Delay(1000, ct); IntState.SetValue(20); outputHelper.WriteLine("StopLastWork"); return Observable.Return(0); - })) - .Switch(); + }); long progress = 0; @@ -216,10 +212,8 @@ public IDisposable ObserveStateComplex(IStateObserverAsync observer, ITestOutput .Subscribe(observer); } - public IDisposable ObserveStateThrow(IStateObserverAsync observer) + public static IDisposable ObserveStateThrow(IStateObserverAsync observer) { - var error = new InvalidOperationException("ObserveStateException"); - return Observable .Interval(TimeSpan.FromSeconds(2)) .Timeout(TimeSpan.FromSeconds(1)) diff --git a/RxBlazorLightCoreTests/StateTests.cs b/RxBlazorLightCoreTests/StateTests.cs index db4d381..5f2bb3a 100644 --- a/RxBlazorLightCoreTests/StateTests.cs +++ b/RxBlazorLightCoreTests/StateTests.cs @@ -806,7 +806,7 @@ public async Task TestStateObserverComplex() await Task.Delay(5); } - Assert.InRange(stateChangeCount, 6, 7); + Assert.InRange(stateChangeCount, 4, 5); Assert.Equal(20, fixture.IntState.Value); } @@ -883,7 +883,7 @@ IDisposable subscribeTest() var disposable = subscribeTest(); fixture.ResetExceptions(); - fixture.CancellableObserverAsync.ExecuteAsync(fixture.ObserveStateThrow); + fixture.CancellableObserverAsync.ExecuteAsync(ServiceFixture.ObserveStateThrow); while (!done) { await Task.Delay(5); @@ -931,7 +931,7 @@ IDisposable subscribeTest() var disposable = subscribeTest(); fixture.ResetExceptions(); - fixture.CancellableObserverHandleErrorAsync.ExecuteAsync(fixture.ObserveStateThrow); + fixture.CancellableObserverHandleErrorAsync.ExecuteAsync(ServiceFixture.ObserveStateThrow); while (!done) { await Task.Delay(5); diff --git a/RxMudBlazorLightTestBase/Service/TestService.State.cs b/RxMudBlazorLightTestBase/Service/TestService.State.cs index 50b3a92..bafcd2a 100644 --- a/RxMudBlazorLightTestBase/Service/TestService.State.cs +++ b/RxMudBlazorLightTestBase/Service/TestService.State.cs @@ -1,4 +1,5 @@ -using System.Reactive.Linq; +using System.Reactive; +using System.Reactive.Linq; using RxBlazorLightCore; namespace RxMudBlazorLightTestBase.Service @@ -9,10 +10,7 @@ public sealed partial class TestService public Action IncrementCounter => () => Counter++; - public static Action IncrementCounterIndirect(int value, Action setter) => () => - { - setter(++value); - }; + public static Action IncrementCounterIndirect(int value, Action setter) => () => { setter(++value); }; public Func IncrementCounterAsync => async _ => { @@ -20,10 +18,7 @@ public static Action IncrementCounterIndirect(int value, Action setter) => Counter++; }; - public Action AddToCounter(int value) => () => - { - Counter += value; - }; + public Action AddToCounter(int value) => () => { Counter += value; }; public Func AddToCounterAsync(int value) { @@ -37,14 +32,12 @@ public Func AddToCounterAsync(int value) public Func IncrementCounterObservable => observer => { var stopObservable = Observable - .Return(false) - .Select(_ => Observable.DeferAsync(async ct => + .FromAsync(async ct => { await Task.Delay(2000, ct); Counter++; return Observable.Timer(TimeSpan.FromSeconds(1)); - })) - .Switch(); + }); return Observable .Interval(TimeSpan.FromMilliseconds(100)) @@ -52,18 +45,16 @@ public Func AddToCounterAsync(int value) .Select(_ => -1L) .Subscribe(observer); }; - + public Func IncrementCounterTimeoutObservable => observer => { var stopObservable = Observable - .Return(false) - .Select(_ => Observable.DeferAsync(async ct => + .FromAsync(async ct => { await Task.Delay(2000, ct); Counter++; return Observable.Timer(TimeSpan.FromSeconds(1)); - })) - .Switch(); + }); return Observable .Interval(TimeSpan.FromMilliseconds(100)) @@ -71,38 +62,48 @@ public Func AddToCounterAsync(int value) .Select(i => i > 10 ? throw new TimeoutException() : -1L) .Subscribe(observer); }; - + public Func AddCounterObservable => observer => { var startObservable = Observable - .Return(false) - .Select(_ => Observable.DeferAsync(async token => + .FromAsync(async ct => { - await Task.Delay(1000, token); + await Task.Delay(1000, ct); Counter += 10; - return Observable.Timer(TimeSpan.FromSeconds(1)); - })) - .Switch(); - + return Observable.Return(Unit.Default); + }); + var stopObservable = Observable - .Return(false) - .Select(_ => Observable.DeferAsync(async token => + .FromAsync(async ct => { - await Task.Delay(1000, token); + await Task.Delay(1000, ct); Counter += 10; - return Observable.Return(0); - })) - .Switch(); + return Observable.Return(Unit.Default); + }); + + var triggerObservable = Observable + .Interval(TimeSpan.FromMilliseconds(100)) + .Select(i => + { + Console.WriteLine($"Interval: {i}"); + return i; + }) + .TakeUntil(i => i > 2000); long progress = 0; - + return Observable - .Interval(TimeSpan.FromMilliseconds(30)) + .Interval(TimeSpan.FromMilliseconds(40)) .TakeUntil(startObservable) .Concat( Observable - .Interval(TimeSpan.FromMilliseconds(30)) - .TakeUntil(stopObservable) + .Interval(TimeSpan.FromMilliseconds(40)) + .TakeUntil(triggerObservable) + .Concat( + Observable + .Interval(TimeSpan.FromMilliseconds(40)) + .TakeUntil(stopObservable) + ) ) .Select(_ => progress++) .Subscribe(observer); @@ -151,10 +152,10 @@ public bool RatingValueCanChange(int _) } public static Func ChangePizzaAsync => async (o, n) => - { - await Task.Delay(1000); - Console.WriteLine($"{o}, {n}"); - }; + { + await Task.Delay(1000); + Console.WriteLine($"{o}, {n}"); + }; public static Action ChangePizza(string value) { @@ -167,4 +168,4 @@ public static Action ChangePizza(string value) public static Action ChangeTestColor => (o, n) => Console.WriteLine($"{o}, {n}"); } -} +} \ No newline at end of file