Skip to content

Commit

Permalink
Fix potential race conditions with user subscription test
Browse files Browse the repository at this point in the history
  • Loading branch information
Cyberboss committed Oct 3, 2024
1 parent 330fbdf commit d24abb5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
12 changes: 8 additions & 4 deletions tests/Tgstation.Server.Tests/Live/HoldLastObserver.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Threading;

namespace Tgstation.Server.Tests.Live
{
Expand All @@ -10,9 +11,12 @@ sealed class HoldLastObserver<T> : IObserver<T>

public T LastValue { get; private set; }

public ulong ErrorCount { get; private set; }
public ulong ErrorCount => errorCount;

public ulong ResultCount { get; private set; }
public ulong ResultCount => resultCount;

ulong errorCount;
ulong resultCount;

public void OnCompleted()
{
Expand All @@ -21,13 +25,13 @@ public void OnCompleted()

public void OnError(Exception error)
{
++ErrorCount;
Interlocked.Increment(ref errorCount);
LastError = error;
}

public void OnNext(T value)
{
++ResultCount;
Interlocked.Increment(ref resultCount);
LastValue = value;
}
}
Expand Down
8 changes: 8 additions & 0 deletions tests/Tgstation.Server.Tests/Live/UsersTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,16 @@ await ValueTaskExtensions.WhenAll(

var expected = new PlatformIdentifier().IsWindows ? 108U : 107U;
// wait up to 10s
var lastSeen = observer.ResultCount;
for (var i = 0; i < 10 && observer.ResultCount < expected; ++i)
{
await Task.Delay(TimeSpan.FromSeconds(1), cancellationToken);
if (observer.ResultCount > lastSeen)
{
lastSeen = observer.ResultCount;
i = -1;
}
}

Assert.AreEqual(expected, observer.ResultCount); // sys user
observer.LastValue.EnsureNoErrors();
Expand Down

0 comments on commit d24abb5

Please sign in to comment.