Skip to content
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

Update to xunit v3, allow running tests in release build #1437

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions .github/workflows/steamkit2-build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,16 @@ jobs:
if: matrix.os == 'windows-latest'

- name: Run Tests
if: matrix.configuration == 'Debug'
run: dotnet test --verbosity normal SteamKit2/Tests/Tests.csproj /p:CollectCoverage=true /p:CoverletOutputFormat=lcov /p:CoverletOutput='./Coverage/lcov.info' /p:Include="[SteamKit2]SteamKit*" /p:Exclude="[SteamKit2]SteamKit2*Internal*"
run: >
dotnet test
--configuration ${{ matrix.configuration }}
--verbosity normal
SteamKit2/Tests/Tests.csproj
/p:CollectCoverage=true
/p:CoverletOutputFormat=lcov
/p:CoverletOutput='./Coverage/lcov.info'
/p:Include="[SteamKit2]SteamKit*"
/p:Exclude="[SteamKit2]SteamKit2*Internal*"

- name: Upload test coverage
uses: codecov/codecov-action@v4
Expand Down
62 changes: 32 additions & 30 deletions SteamKit2/Tests/AsyncJobFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,45 @@ class Callback : CallbackMsg
}

[Fact]
public void AsyncJobCtorRegistersJob()
public void AysncJobCompletesOnCallback()
{
SteamClient client = new SteamClient();

AsyncJob<Callback> asyncJob = new AsyncJob<Callback>( client, 123 );
Task<Callback> asyncTask = asyncJob.ToTask();

Assert.True( client.jobManager.asyncJobs.ContainsKey( asyncJob ), "Async job dictionary should contain the jobid key" );
Assert.True( client.jobManager.asyncJobs.ContainsKey( 123 ), "Async job dictionary should contain jobid key as a value type" );
client.PostCallback( new Callback { JobID = 123 } );

Assert.True( asyncTask.IsCompleted, "Async job should be completed after callback is posted" );
Assert.False( asyncTask.IsCanceled, "Async job should not be canceled after callback is posted" );
Assert.False( asyncTask.IsFaulted, "Async job should not be faulted after callback is posted" );
}

[Fact]
public void AysncJobCompletesOnCallback()
public async Task AsyncJobGivesBackCallback()
{
SteamClient client = new SteamClient();

AsyncJob<Callback> asyncJob = new AsyncJob<Callback>( client, 123 );
Task<Callback> asyncTask = asyncJob.ToTask();
Task<Callback> jobTask = asyncJob.ToTask();

client.PostCallback( new Callback { JobID = 123 } );
Callback ourCallback = new Callback { JobID = 123 };

Assert.True( asyncTask.IsCompleted, "Async job should be completed after callback is posted" );
Assert.False( asyncTask.IsCanceled, "Async job should not be canceled after callback is posted" );
Assert.False( asyncTask.IsFaulted, "Async job should not be faulted after callback is posted" );
client.PostCallback( ourCallback );

Assert.Same( await jobTask, ourCallback );
}

#if DEBUG
[Fact]
public void AsyncJobCtorRegistersJob()
{
SteamClient client = new SteamClient();

AsyncJob<Callback> asyncJob = new AsyncJob<Callback>( client, 123 );

Assert.True( client.jobManager.asyncJobs.ContainsKey( asyncJob ), "Async job dictionary should contain the jobid key" );
Assert.True( client.jobManager.asyncJobs.ContainsKey( 123 ), "Async job dictionary should contain jobid key as a value type" );
}

[Fact]
Expand All @@ -60,7 +76,7 @@ public async Task AsyncJobClearsOnTimeout()
AsyncJob<Callback> asyncJob = new AsyncJob<Callback>( client, 123 );
asyncJob.Timeout = TimeSpan.FromMilliseconds( 50 );

await Task.Delay( TimeSpan.FromMilliseconds( 70 ) );
await Task.Delay( TimeSpan.FromMilliseconds( 70 ), TestContext.Current.CancellationToken );
client.jobManager.CancelTimedoutJobs();

Assert.False( client.jobManager.asyncJobs.ContainsKey( asyncJob ), "Async job dictionary should no longer contain jobid key after timeout" );
Expand All @@ -84,21 +100,6 @@ public async Task AsyncJobCancelsOnSetFailedTimeout()
await Assert.ThrowsAsync<TaskCanceledException>( async () => await asyncTask );
}

[Fact]
public async Task AsyncJobGivesBackCallback()
{
SteamClient client = new SteamClient();

AsyncJob<Callback> asyncJob = new AsyncJob<Callback>( client, 123 );
Task<Callback> jobTask = asyncJob.ToTask();

Callback ourCallback = new Callback { JobID = 123 };

client.PostCallback( ourCallback );

Assert.Same( await jobTask, ourCallback );
}

[Fact]
public async Task AsyncJobTimesout()
{
Expand All @@ -109,7 +110,7 @@ public async Task AsyncJobTimesout()

Task<Callback> asyncTask = asyncJob.ToTask();

await Task.Delay( TimeSpan.FromMilliseconds( 70 ) );
await Task.Delay( TimeSpan.FromMilliseconds( 70 ), TestContext.Current.CancellationToken );
client.jobManager.CancelTimedoutJobs();

Assert.True( asyncTask.IsCompleted, "Async job should be completed yet" );
Expand Down Expand Up @@ -206,7 +207,7 @@ public async Task AsyncJobMultipleClearsOnTimeout()
AsyncJobMultiple<Callback> asyncJob = new AsyncJobMultiple<Callback>( client, 123, ccall => true );
asyncJob.Timeout = TimeSpan.FromMilliseconds( 50 );

await Task.Delay( TimeSpan.FromMilliseconds( 70 ) );
await Task.Delay( TimeSpan.FromMilliseconds( 70 ), TestContext.Current.CancellationToken );
client.jobManager.CancelTimedoutJobs();

Assert.False( client.jobManager.asyncJobs.ContainsKey( asyncJob ), "Async job dictionary should no longer contain jobid key after timeout" );
Expand All @@ -232,7 +233,7 @@ public async Task AsyncJobMultipleExtendsTimeoutOnMessage()
asyncJob.AddResult( new Callback { JobID = 123, IsFinished = false } );

// delay for what the original timeout would have been
await Task.Delay( TimeSpan.FromMilliseconds( 70 ) );
await Task.Delay( TimeSpan.FromMilliseconds( 70 ), TestContext.Current.CancellationToken );

client.jobManager.CancelTimedoutJobs();

Expand All @@ -259,7 +260,7 @@ public async Task AsyncJobMultipleTimesout()

Task<AsyncJobMultiple<Callback>.ResultSet> asyncTask = asyncJob.ToTask();

await Task.Delay( TimeSpan.FromMilliseconds( 70 ) );
await Task.Delay( TimeSpan.FromMilliseconds( 70 ), TestContext.Current.CancellationToken );
client.jobManager.CancelTimedoutJobs();

Assert.True( asyncTask.IsCompleted, "AsyncJobMultiple should be completed after job timeout" );
Expand All @@ -286,7 +287,7 @@ public async Task AsyncJobMultipleCompletesOnIncompleteResult()
// adding a result will extend the job's timeout, but we'll cheat here and decrease it
asyncJob.Timeout = TimeSpan.FromMilliseconds( 50 );

await Task.Delay( TimeSpan.FromMilliseconds( 70 ) );
await Task.Delay( TimeSpan.FromMilliseconds( 70 ), TestContext.Current.CancellationToken );
client.jobManager.CancelTimedoutJobs();

Assert.True( asyncTask.IsCompleted, "AsyncJobMultiple should be completed on partial (timed out) result set" );
Expand Down Expand Up @@ -412,5 +413,6 @@ static void WaitForTaskWithoutRunningInline( Task task )
using var cts = new CancellationTokenSource();
task.Wait( cts.Token );
}
#endif
}
}
2 changes: 2 additions & 0 deletions SteamKit2/Tests/CDNClientFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace Tests
{
#if DEBUG
public class CDNClientFacts
{
[Fact]
Expand Down Expand Up @@ -124,4 +125,5 @@ protected override Task<HttpResponseMessage> SendAsync( HttpRequestMessage reque
=> Task.FromResult( new HttpResponseMessage( ( HttpStatusCode )418 ) );
}
}
#endif
}
3 changes: 3 additions & 0 deletions SteamKit2/Tests/CMClientFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

namespace Tests
{
#if DEBUG
[Collection( nameof( NotThreadSafeResourceCollection ) )]
public class CMClientFacts
{
[Fact]
Expand Down Expand Up @@ -78,4 +80,5 @@ static byte[] Serialize(ISteamSerializableHeader hdr)
return ms.ToArray();
}
}
#endif
}
2 changes: 1 addition & 1 deletion SteamKit2/Tests/CallbackManagerFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ void action( CallbackForTest cb )

for ( var i = 1; i <= callbacks.Length; i++ )
{
await mgr.RunWaitCallbackAsync();
await mgr.RunWaitCallbackAsync( TestContext.Current.CancellationToken );
Assert.Equal( i, numCallbacksRun );
}

Expand Down
2 changes: 2 additions & 0 deletions SteamKit2/Tests/ClientMsgFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Tests
{
#if DEBUG
public class ClientMsgFacts
{
// this test vector is a packet meant for a ClientMsg<MsgClientChatEnter>
Expand Down Expand Up @@ -67,4 +68,5 @@ static IPacketMsg BuildStructMsg()
return CMClient.GetPacketMsg( structMsgData, DebugLogContext.Instance );
}
}
#endif
}
15 changes: 10 additions & 5 deletions SteamKit2/Tests/DebugLogFacts.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using SteamKit2;
using System.Threading.Tasks;
using SteamKit2;
using Xunit;
using Xunit.Sdk;
using Xunit.v3;

namespace Tests
{
#if DEBUG
class TestListener : IDebugListener
{
public void WriteLine( string category, string msg )
Expand All @@ -15,19 +17,21 @@ public void WriteLine( string category, string msg )

class DebugLogSetupTeardownAttribute : BeforeAfterTestAttribute
{
public override void Before( System.Reflection.MethodInfo methodUnderTest )
public override ValueTask Before( System.Reflection.MethodInfo methodUnderTest, IXunitTest test )
{
DebugLog.ClearListeners();
return ValueTask.CompletedTask;
}

public override void After( System.Reflection.MethodInfo methodUnderTest )
public override ValueTask After( System.Reflection.MethodInfo methodUnderTest, IXunitTest test )
{
DebugLog.Enabled = false;
DebugLog.ClearListeners();
return ValueTask.CompletedTask;
}
}

[CollectionDefinition( nameof( DebugLogFacts ), DisableParallelization = true )]
[Collection( nameof( NotThreadSafeResourceCollection ) )]
public class DebugLogFacts
{
[Fact, DebugLogSetupTeardown]
Expand Down Expand Up @@ -163,4 +167,5 @@ public void CustomCMClientIDPrefixed()
Assert.Equal( "My 1st message", message );
}
}
#endif
}
10 changes: 5 additions & 5 deletions SteamKit2/Tests/KeyValueFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ public void KeyValueBinarySerializationIsSymmetric()
[Fact]
public void KeyValues_TryReadAsBinary_ReadsBinary()
{
var binary = Utils.DecodeHexString( TestObjectHex );
var binary = Convert.FromHexString( TestObjectHex );
var kv = new KeyValue();
bool success;
using ( var ms = new MemoryStream( binary ) )
Expand All @@ -305,7 +305,7 @@ public void KeyValues_TryReadAsBinary_ReadsBinary()
[Fact]
public void KeyValuesReadsBinaryWithLeftoverData()
{
var binary = Utils.DecodeHexString( TestObjectHex + Guid.NewGuid().ToString().Replace("-", "", StringComparison.Ordinal) );
var binary = Convert.FromHexString( TestObjectHex + Guid.NewGuid().ToString().Replace("-", "", StringComparison.Ordinal) );
var kv = new KeyValue();
bool success;
using ( var ms = new MemoryStream( binary ) )
Expand All @@ -328,7 +328,7 @@ public void KeyValuesFailsToReadTruncatedBinary()
// Test every possible truncation boundary we have.
for ( int i = 0; i < TestObjectHex.Length; i += 2 )
{
var binary = Utils.DecodeHexString( TestObjectHex[ ..i ] );
var binary = Convert.FromHexString( TestObjectHex[ ..i ] );
var kv = new KeyValue();
bool success;
using ( var ms = new MemoryStream( binary ) )
Expand All @@ -345,7 +345,7 @@ public void KeyValuesFailsToReadTruncatedBinary()
public void KeyValuesReadsBinaryWithMultipleChildren()
{
var hex = "00546573744f626a65637400016b6579310076616c75653100016b6579320076616c756532000808";
var binary = Utils.DecodeHexString( hex );
var binary = Convert.FromHexString( hex );
var kv = new KeyValue();
bool success;
using ( var ms = new MemoryStream( binary ) )
Expand Down Expand Up @@ -603,7 +603,7 @@ public void KeyValuesBinaryPreserveEmptyObjects()
public void DecodesBinaryWithFieldType10()
{
var hex = "00546573744F626A656374000A6B65790001020304050607080808";
var binary = Utils.DecodeHexString( hex );
var binary = Convert.FromHexString( hex );
var kv = new KeyValue();
using (var ms = new MemoryStream(binary))
{
Expand Down
4 changes: 3 additions & 1 deletion SteamKit2/Tests/MachineInfoFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace Tests
{
#if DEBUG
public class MachineInfoFacts
{
[Fact]
Expand Down Expand Up @@ -90,7 +91,7 @@ public void GenerationIsThreadSafe()
threads[i] = new Thread(state =>
{
var provider = (IMachineInfoProvider)state;
trigger.Wait();
trigger.Wait( TestContext.Current.CancellationToken );
HardwareUtils.Init(provider);
HardwareUtils.GetMachineID(provider);
});
Expand Down Expand Up @@ -201,4 +202,5 @@ sealed class ThrowingMachineInfoProvider : IMachineInfoProvider
public byte[] GetMachineGuid() => throw new InvalidOperationException("This provider only throws.");
}
}
#endif
}
2 changes: 2 additions & 0 deletions SteamKit2/Tests/NetHelpersFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Tests
{
#if DEBUG
public class NetHelpersFacts
{
[Fact]
Expand Down Expand Up @@ -67,4 +68,5 @@ public void TryParseIPEndPoint()
Assert.Equal( new IPEndPoint( IPAddress.IPv6Loopback, 1337 ), parsedIpv6 );
}
}
#endif
}
9 changes: 9 additions & 0 deletions SteamKit2/Tests/NotThreadSafeResourceCollection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using Xunit;

namespace Tests;

[CollectionDefinition( nameof( NotThreadSafeResourceCollection ), DisableParallelization = true )]
public class NotThreadSafeResourceCollection
{
// DebugLog is not thread-safe.
}
Loading