Skip to content

Commit

Permalink
Backport SteamRE#481.
Browse files Browse the repository at this point in the history
  • Loading branch information
leonard-thieu committed Oct 25, 2017
1 parent 0dcec97 commit b4f1a69
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
4 changes: 2 additions & 2 deletions SteamKit2/SteamKit2/Types/JobID.cs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ public sealed class AsyncJob<T> : AsyncJob, IAsyncJob<T>
public AsyncJob( SteamClient client, JobID jobId )
: base( client, jobId )
{
tcs = new TaskCompletionSource<T>();
tcs = new TaskCompletionSource<T>(TaskCreationOptions.RunContinuationsAsynchronously);
}


Expand Down Expand Up @@ -301,7 +301,7 @@ public sealed class ResultSet
public AsyncJobMultiple( SteamClient client, JobID jobId, Predicate<T> finishCondition )
: base( client, jobId )
{
tcs = new TaskCompletionSource<ResultSet>();
tcs = new TaskCompletionSource<ResultSet>(TaskCreationOptions.RunContinuationsAsynchronously);

this.finishCondition = finishCondition;
}
Expand Down
47 changes: 47 additions & 0 deletions SteamKit2/Tests/AsyncJobFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using SteamKit2;
using Xunit;
Expand Down Expand Up @@ -360,5 +361,51 @@ public async void AsyncJobMultipleThrowsFailureExceptionOnFailure()

await Assert.ThrowsAsync( typeof( AsyncJobFailedException ), async () => await asyncTask );
}

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

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

var continuationThreadID = -1;
var continuation = asyncTask.ContinueWith(t =>
{
continuationThreadID = Thread.CurrentThread.ManagedThreadId;
}, TaskContinuationOptions.ExecuteSynchronously);

var completionThreadID = Thread.CurrentThread.ManagedThreadId;
asyncJob.AddResult(new Callback { JobID = 123 });

await continuation;

Assert.NotEqual(-1, continuationThreadID);
Assert.NotEqual(completionThreadID, continuationThreadID);
}

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

var asyncJob = new AsyncJobMultiple<Callback>(client, 123, call => true);
var asyncTask = asyncJob.ToTask();

var continuationThreadID = -1;
var continuation = asyncTask.ContinueWith(t =>
{
continuationThreadID = Thread.CurrentThread.ManagedThreadId;
}, TaskContinuationOptions.ExecuteSynchronously);

var completionThreadID = Thread.CurrentThread.ManagedThreadId;
asyncJob.AddResult(new Callback { JobID = 123 });

await continuation;

Assert.NotEqual(-1, continuationThreadID);
Assert.NotEqual(completionThreadID, continuationThreadID);
}
}
}

0 comments on commit b4f1a69

Please sign in to comment.