Skip to content

Commit

Permalink
Try to fix test that is only failing in CI
Browse files Browse the repository at this point in the history
  • Loading branch information
Aldaviva committed Feb 17, 2023
1 parent 28da34d commit 844d738
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
3 changes: 2 additions & 1 deletion Demo/Demo.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
using HidSharp;
using PowerMate;

Console.WriteLine("All devices:");
foreach (Device device in DeviceList.Local.GetAllDevices()) {
Console.WriteLine($"Detected device {device.GetFriendlyName()}");
Console.WriteLine($" {device.GetFriendlyName()}");
}

using IPowerMateClient powerMate = new PowerMateClient();
Expand Down
23 changes: 14 additions & 9 deletions Tests/PowerMateClientTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ namespace Tests;

public class PowerMateClientTest {

private static readonly TimeSpan TestTimeout = TimeSpan.FromSeconds(4);

private readonly HidDevice _device = A.Fake<HidDevice>();
private readonly DeviceList _deviceList = A.Fake<DeviceList>();
private readonly HidStream _stream = A.Fake<HidStream>();
Expand Down Expand Up @@ -39,7 +41,7 @@ public class PowerMateClientTest {
actualEvent = @event;
eventArrived.Set();
};
eventArrived.Wait(1000);
eventArrived.Wait(TestTimeout);
actualEvent.HasValue.Should().BeTrue();
actualEvent!.Value.IsPressed.Should().BeTrue();
actualEvent!.Value.IsRotationClockwise.Should().BeNull();
Expand All @@ -52,22 +54,25 @@ public class PowerMateClientTest {
Enumerable.Empty<HidDevice>(),
new[] { _device });

bool? connectedEventArg = null;
ManualResetEventSlim eventArrived = new();
PowerMateClient client = new(_deviceList);
bool? connectedEventArg = null;
CountdownEvent eventsArrived = new(2);
PowerMateClient client = new(_deviceList);
client.IsConnected.Should().BeFalse();
PowerMateInput? actualEvent = null;
client.InputReceived += (_, @event) => {
actualEvent = @event;
eventArrived.Set();
eventsArrived.AddCount();
};
client.IsConnectedChanged += (sender, b) => {
connectedEventArg = b;
eventsArrived.AddCount();
};
client.IsConnectedChanged += (sender, b) => connectedEventArg = b;

actualEvent.HasValue.Should().BeFalse();

_deviceList.RaiseChanged();

eventArrived.Wait(1000);
eventsArrived.Wait(TestTimeout);
client.IsConnected.Should().BeTrue();
connectedEventArg.Should().BeTrue();
actualEvent.HasValue.Should().BeTrue();
Expand Down Expand Up @@ -101,7 +106,7 @@ public class PowerMateClientTest {

_deviceList.RaiseChanged();

eventArrived.Wait(1000);
eventArrived.Wait(TestTimeout);
actualEvent.HasValue.Should().BeTrue();
actualEvent!.Value.IsPressed.Should().BeTrue();
actualEvent!.Value.IsRotationClockwise.Should().BeNull();
Expand All @@ -115,7 +120,7 @@ public class PowerMateClientTest {
A.CallTo(() => synchronizationContext.Post(A<SendOrPostCallback>._, An<object?>._)).Invokes(() => eventArrived.Set());

PowerMateClient client = new(_deviceList) { EventSynchronizationContext = synchronizationContext };
eventArrived.Wait(1000);
eventArrived.Wait(TestTimeout);

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

0 comments on commit 844d738

Please sign in to comment.