Skip to content

Commit

Permalink
Allow subclasses to run custom logic when connected. Try to fix test …
Browse files Browse the repository at this point in the history
…that is failing in CI but not locally.
  • Loading branch information
Aldaviva committed Apr 9, 2023
1 parent 999e24c commit 9ba8696
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
3 changes: 3 additions & 0 deletions HidClient/AbstractHidClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,16 @@ public abstract class AbstractHidClient: IHidClient {
DeviceStream.ReadTimeout = Timeout.Infinite;
_cancellationTokenSource = new CancellationTokenSource();
IsConnected = true;
OnConnect();

try {
Task.Factory.StartNew(HidReadLoop, _cancellationTokenSource.Token, TaskCreationOptions.LongRunning, TaskScheduler.Default);
} catch (TaskCanceledException) { }
}
}

protected virtual void OnConnect() { }

private async Task HidReadLoop() {
CancellationToken cancellationToken = _cancellationTokenSource!.Token;

Expand Down
10 changes: 7 additions & 3 deletions Tests/HidClientInputTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,18 @@ public class HidClientInputTest {
[Fact]
public void SynchronizationContext() {
A.CallTo(() => _stream.ReadAsync(A<byte[]>._, An<int>._, An<int>._, A<CancellationToken>._))
.ThrowsAsync(new IOException("fake disconnected")).Once().Then
.ReturnsLazily(FakeReadAsync(5, 6, 7));
.ThrowsAsync(new IOException("fake disconnected"));

ManualResetEventSlim eventArrived = new();
SynchronizationContext synchronizationContext = A.Fake<SynchronizationContext>();
A.CallTo(() => synchronizationContext.Post(A<SendOrPostCallback>._, An<object?>._)).Invokes(() => eventArrived.Set());

FakeHidClient client = new(_deviceList) { EventSynchronizationContext = synchronizationContext };
using FakeHidClient client = new(_deviceList) { EventSynchronizationContext = synchronizationContext };

A.CallTo(() => _stream.ReadAsync(A<byte[]>._, An<int>._, An<int>._, A<CancellationToken>._))
.ThrowsAsync(new IOException("fake disconnected")).Once().Then
.ReturnsLazily(FakeReadAsync(5, 6, 7));

eventArrived.Wait(TestTimeout);

A.CallTo(() => synchronizationContext.Post(A<SendOrPostCallback>._, An<object?>._)).MustHaveHappenedOnceOrMore();
Expand Down

0 comments on commit 9ba8696

Please sign in to comment.