-
Notifications
You must be signed in to change notification settings - Fork 1
Add Observable and SynchronizedObserver utilities #46
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
d1ef663
feat: add Observable and SynchronizedObserver utilities
Copilot 50aa5cb
refactor: use var for all local variable declarations in ObservableTests
Copilot 762d7ec
Remove redundant SynchronizedObserver tests; make Observable<T> a fil…
Copilot 325b676
Rename DelegateObserver to FakeObserver in ObservableTests
Copilot 3473b18
Use Lock type in SynchronizedObserver
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,177 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Threading; | ||
| using FluentAssertions; | ||
| using PowerKit; | ||
| using Xunit; | ||
|
|
||
| namespace PowerKit.Tests; | ||
|
|
||
| file class FakeObserver<T>( | ||
| Action<T>? onNext = null, | ||
| Action<Exception>? onError = null, | ||
| Action? onCompleted = null | ||
| ) : IObserver<T> | ||
| { | ||
| public void OnNext(T value) => onNext?.Invoke(value); | ||
|
|
||
| public void OnError(Exception error) => onError?.Invoke(error); | ||
|
|
||
| public void OnCompleted() => onCompleted?.Invoke(); | ||
| } | ||
|
|
||
| public class ObservableTests | ||
| { | ||
| [Fact] | ||
| public void Observable_Create_Subscribe_Test() | ||
| { | ||
| // Arrange | ||
| var subscribed = false; | ||
| var observable = Observable.Create<int>(_ => | ||
| { | ||
| subscribed = true; | ||
| return Disposable.Null; | ||
| }); | ||
|
|
||
| // Act | ||
| subscribed.Should().BeFalse(); | ||
| observable.Subscribe(new FakeObserver<int>()); | ||
|
|
||
| // Assert | ||
| subscribed.Should().BeTrue(); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Observable_Create_OnNext_Test() | ||
| { | ||
| // Arrange | ||
| var received = new List<int>(); | ||
| var observable = Observable.Create<int>(observer => | ||
| { | ||
| observer.OnNext(1); | ||
| observer.OnNext(2); | ||
| observer.OnNext(3); | ||
| observer.OnCompleted(); | ||
| return Disposable.Null; | ||
| }); | ||
|
|
||
| // Act | ||
| observable.Subscribe(new FakeObserver<int>(onNext: received.Add)); | ||
|
|
||
| // Assert | ||
| received.Should().Equal(1, 2, 3); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Observable_Create_OnError_Test() | ||
| { | ||
| // Arrange | ||
| var receivedError = default(Exception); | ||
| var observable = Observable.Create<int>(observer => | ||
| { | ||
| observer.OnError(new InvalidOperationException("test error")); | ||
| return Disposable.Null; | ||
| }); | ||
|
|
||
| // Act | ||
| observable.Subscribe(new FakeObserver<int>(onError: ex => receivedError = ex)); | ||
|
|
||
| // Assert | ||
| receivedError.Should().BeOfType<InvalidOperationException>(); | ||
| receivedError!.Message.Should().Be("test error"); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Observable_Create_OnCompleted_Test() | ||
| { | ||
| // Arrange | ||
| var completed = false; | ||
| var observable = Observable.Create<int>(observer => | ||
| { | ||
| observer.OnCompleted(); | ||
| return Disposable.Null; | ||
| }); | ||
|
|
||
| // Act | ||
| observable.Subscribe(new FakeObserver<int>(onCompleted: () => completed = true)); | ||
|
|
||
| // Assert | ||
| completed.Should().BeTrue(); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Observable_Create_Dispose_Test() | ||
| { | ||
| // Arrange | ||
| var disposed = false; | ||
| var observable = Observable.Create<int>(_ => Disposable.Create(() => disposed = true)); | ||
|
|
||
| // Act | ||
| disposed.Should().BeFalse(); | ||
| var subscription = observable.Subscribe(new FakeObserver<int>()); | ||
| subscription.Dispose(); | ||
|
|
||
| // Assert | ||
| disposed.Should().BeTrue(); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Observable_CreateSynchronized_OnNext_Test() | ||
| { | ||
| // Arrange | ||
| var received = new List<int>(); | ||
| var observable = Observable.CreateSynchronized<int>(observer => | ||
| { | ||
| observer.OnNext(1); | ||
| observer.OnNext(2); | ||
| observer.OnNext(3); | ||
| observer.OnCompleted(); | ||
| return Disposable.Null; | ||
| }); | ||
|
|
||
| // Act | ||
| observable.Subscribe(new FakeObserver<int>(onNext: received.Add)); | ||
|
|
||
| // Assert | ||
| received.Should().Equal(1, 2, 3); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Observable_CreateSynchronized_ThreadSafe_Test() | ||
| { | ||
| // Arrange | ||
| const int threadCount = 10; | ||
| const int valuesPerThread = 100; | ||
| var received = new List<int>(); | ||
| var observable = Observable.CreateSynchronized<int>(observer => | ||
| { | ||
| var threads = new List<Thread>(); | ||
|
|
||
| for (var i = 0; i < threadCount; i++) | ||
| { | ||
| var thread = new Thread(() => | ||
| { | ||
| for (var j = 0; j < valuesPerThread; j++) | ||
| { | ||
| observer.OnNext(j); | ||
| } | ||
| }); | ||
| threads.Add(thread); | ||
| } | ||
|
|
||
| foreach (var t in threads) | ||
| t.Start(); | ||
| foreach (var t in threads) | ||
| t.Join(); | ||
|
|
||
| observer.OnCompleted(); | ||
| return Disposable.Null; | ||
| }); | ||
|
|
||
| // Act | ||
| observable.Subscribe(new FakeObserver<int>(onNext: v => received.Add(v))); | ||
|
|
||
| // Assert | ||
| received.Should().HaveCount(threadCount * valuesPerThread); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| #nullable enable | ||
| using System; | ||
| using System.Diagnostics.CodeAnalysis; | ||
|
|
||
| namespace PowerKit; | ||
|
|
||
| #if NET40_OR_GREATER || NETSTANDARD || NET | ||
| /// <summary> | ||
| /// Represents an observable sequence of values. | ||
| /// </summary> | ||
| #if !POWERKIT_INCLUDE_COVERAGE | ||
| [ExcludeFromCodeCoverage] | ||
| #endif | ||
| file class Observable<T>(Func<IObserver<T>, IDisposable> subscribe) : IObservable<T> | ||
| { | ||
| /// <inheritdoc /> | ||
| public IDisposable Subscribe(IObserver<T> observer) => subscribe(observer); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Provides utility methods for creating <see cref="IObservable{T}" /> instances. | ||
| /// </summary> | ||
| #if !POWERKIT_INCLUDE_COVERAGE | ||
| [ExcludeFromCodeCoverage] | ||
| #endif | ||
| internal static class Observable | ||
| { | ||
| /// <summary> | ||
| /// Creates an observable that invokes the specified subscribe function when subscribed to. | ||
| /// </summary> | ||
| public static IObservable<T> Create<T>(Func<IObserver<T>, IDisposable> subscribe) => | ||
| new Observable<T>(subscribe); | ||
|
|
||
| /// <summary> | ||
| /// Creates an observable that invokes the specified subscribe function when subscribed to, | ||
| /// wrapping the observer in a <see cref="SynchronizedObserver{T}" /> to ensure thread safety. | ||
| /// </summary> | ||
| public static IObservable<T> CreateSynchronized<T>(Func<IObserver<T>, IDisposable> subscribe) => | ||
| Create<T>(observer => subscribe(new SynchronizedObserver<T>(observer))); | ||
| } | ||
| #endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| #nullable enable | ||
| using System; | ||
| using System.Diagnostics.CodeAnalysis; | ||
|
|
||
| namespace PowerKit; | ||
|
|
||
| #if NET40_OR_GREATER || NETSTANDARD || NET | ||
| /// <summary> | ||
| /// An observer that synchronizes access to the underlying observer. | ||
| /// </summary> | ||
| #if !POWERKIT_INCLUDE_COVERAGE | ||
| [ExcludeFromCodeCoverage] | ||
| #endif | ||
| internal class SynchronizedObserver<T>(IObserver<T> observer) : IObserver<T> | ||
| { | ||
| private readonly object _lock = new(); | ||
|
|
||
| /// <inheritdoc /> | ||
| public void OnCompleted() | ||
| { | ||
| lock (_lock) | ||
| { | ||
| observer.OnCompleted(); | ||
| } | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| public void OnError(Exception error) | ||
| { | ||
| lock (_lock) | ||
| { | ||
| observer.OnError(error); | ||
| } | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| public void OnNext(T value) | ||
| { | ||
| lock (_lock) | ||
| { | ||
| observer.OnNext(value); | ||
| } | ||
| } | ||
| } | ||
| #endif | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.