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" ); }
public void AsyncJobClearsOnCompletion() { SteamClient client = new SteamClient(); AsyncJob<Callback> asyncJob = new AsyncJob<Callback>( client, 123 ); client.PostCallback( new Callback { JobID = 123 } ); Assert.False( client.jobManager.asyncJobs.ContainsKey( asyncJob ), "Async job dictionary should no longer contain jobid key after callback is posted" ); Assert.False( client.jobManager.asyncJobs.ContainsKey( 123 ), "Async job dictionary should no longer contain jobid key (as value type) after callback is posted" ); }
public void AysncJobCompletesOnCallback() { SteamClient client = new SteamClient(); AsyncJob<Callback> asyncJob = new AsyncJob<Callback>( client, 123 ); Task<Callback> asyncTask = asyncJob.ToTask(); 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" ); }
public async void AsyncJobClearsOnTimeout() { SteamClient client = new SteamClient(); client.jobManager.SetTimeoutsEnabled( true ); AsyncJob<Callback> asyncJob = new AsyncJob<Callback>( client, 123 ); asyncJob.Timeout = TimeSpan.FromSeconds( 1 ); await Task.Delay( TimeSpan.FromSeconds( 5 ) ); Assert.False( client.jobManager.asyncJobs.ContainsKey( asyncJob ), "Async job dictionary should no longer contain jobid key after timeout" ); Assert.False( client.jobManager.asyncJobs.ContainsKey( 123 ), "Async job dictionary should no longer contain jobid key (as value type) after timeout" ); }
public void AsyncJobClearsOnCompletion() { SteamClient client = new SteamClient(); AsyncJob <Callback> asyncJob = new AsyncJob <Callback>(client, 123); client.PostCallback(new Callback { JobID = 123 }); Assert.False(client.jobManager.asyncJobs.ContainsKey(asyncJob), "Async job dictionary should no longer contain jobid key after callback is posted"); Assert.False(client.jobManager.asyncJobs.ContainsKey(123), "Async job dictionary should no longer contain jobid key (as value type) after callback is posted"); }
public void 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(jobTask.Result, ourCallback); }
public async Task AsyncJobThrowsFailureExceptionOnFailure() { SteamClient client = new SteamClient(); AsyncJob <Callback> asyncJob = new AsyncJob <Callback>(client, 123); Task <Callback> asyncTask = asyncJob.ToTask(); asyncJob.SetFailed(dueToRemoteFailure: true); Assert.True(asyncTask.IsCompleted, "Async job should be completed after job failure"); Assert.False(asyncTask.IsCanceled, "Async job should not be canceled after job failure"); Assert.True(asyncTask.IsFaulted, "Async job should be faulted after job failure"); await Assert.ThrowsAsync(typeof(AsyncJobFailedException), async() => await asyncTask); }
public async void AsyncJobCancelsOnSetFailedTimeout() { SteamClient client = new SteamClient(); AsyncJob<Callback> asyncJob = new AsyncJob<Callback>( client, 123 ); Task<Callback> asyncTask = asyncJob.ToTask(); asyncJob.SetFailed( dueToRemoteFailure: false ); Assert.True( asyncTask.IsCompleted, "Async job should be completed on message timeout" ); Assert.True( asyncTask.IsCanceled, "Async job should be canceled on message timeout" ); Assert.False( asyncTask.IsFaulted, "Async job should not be faulted on message timeout" ); await Assert.ThrowsAsync( typeof( TaskCanceledException ), async () => await asyncTask ); }
public async Task AsyncJobClearsOnTimeout() { SteamClient client = new SteamClient(); client.jobManager.SetTimeoutsEnabled(true); AsyncJob <Callback> asyncJob = new AsyncJob <Callback>(client, 123); asyncJob.Timeout = TimeSpan.FromSeconds(1); await Task.Delay(TimeSpan.FromSeconds(5)); Assert.False(client.jobManager.asyncJobs.ContainsKey(asyncJob), "Async job dictionary should no longer contain jobid key after timeout"); Assert.False(client.jobManager.asyncJobs.ContainsKey(123), "Async job dictionary should no longer contain jobid key (as value type) after timeout"); }
public void AysncJobCompletesOnCallback() { SteamClient client = new SteamClient(); AsyncJob <Callback> asyncJob = new AsyncJob <Callback>(client, 123); Task <Callback> asyncTask = asyncJob.ToTask(); 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"); }
public async Task AsyncJobCancelsOnSetFailedTimeout() { SteamClient client = new SteamClient(); AsyncJob <Callback> asyncJob = new AsyncJob <Callback>(client, 123); Task <Callback> asyncTask = asyncJob.ToTask(); asyncJob.SetFailed(dueToRemoteFailure: false); Assert.True(asyncTask.IsCompleted, "Async job should be completed on message timeout"); Assert.True(asyncTask.IsCanceled, "Async job should be canceled on message timeout"); Assert.False(asyncTask.IsFaulted, "Async job should not be faulted on message timeout"); await Assert.ThrowsAsync(typeof(TaskCanceledException), async() => await asyncTask); }
public static TResult RunSync <TResult>(Func <Task <TResult> > func) { var job = new AsyncJob <TResult>() { JobFunc = func, CompletedSignal = new ManualResetEventSlim(false) }; var thread = new Thread(ExecuteJob <TResult>); thread.Start(job); job.CompletedSignal.Wait(); if (job.Exception != null) { throw job.Exception; } return(job.Result); }
public static int RegisterAsyncJob <T>(AsyncJob <T> job, int callback_id, int user_id) where T : CallbackMsg { lock (call_id_lock) { last_call_id += 1; registered_calls[last_call_id] = new AsyncJobResult() { job_id = last_call_id, internal_job_id = job.JobID, finished = false, result = null, }; } return(last_call_id); }
private static void OnceInvoke(object state) { AsyncJob tme = state as AsyncJob; if (tme.WaitForNextTask()) { return; } try { tme.RunTask(); } catch (Exception ex) { GA.OnGlobalError(tme, ex); throw; } }
public async Task AsyncJobTimesout() { SteamClient client = new SteamClient(); client.jobManager.SetTimeoutsEnabled(true); AsyncJob <Callback> asyncJob = new AsyncJob <Callback>(client, 123); asyncJob.Timeout = TimeSpan.FromSeconds(1); Task <Callback> asyncTask = asyncJob.ToTask(); await Task.Delay(TimeSpan.FromSeconds(5)); Assert.True(asyncTask.IsCompleted, "Async job should be completed after 5 seconds of a 1 second job timeout"); Assert.True(asyncTask.IsCanceled, "Async job should be canceled after 5 seconds of a 1 second job timeout"); Assert.False(asyncTask.IsFaulted, "Async job should not be faulted after 5 seconds of a 1 second job timeout"); await Assert.ThrowsAsync(typeof(TaskCanceledException), async() => await asyncTask); }
public void 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 = Environment.CurrentManagedThreadId; }, TaskContinuationOptions.ExecuteSynchronously); var completionThreadID = Environment.CurrentManagedThreadId; asyncJob.AddResult(new Callback { JobID = 123 }); WaitForTaskWithoutRunningInline(continuation); Assert.NotEqual(-1, continuationThreadID); Assert.NotEqual(completionThreadID, continuationThreadID); }
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); }
public void 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( jobTask.Result, ourCallback ); }
public void AsyncJobThrowsExceptionOnNullCallback() { SteamClient client = new SteamClient(); AsyncJob<Callback> asyncJob = new AsyncJob<Callback>( client, 123 ); Assert.Throws<ArgumentNullException>( () => asyncJob.AddResult( null ) ); }
public async void AsyncJobThrowsFailureExceptionOnFailure() { SteamClient client = new SteamClient(); AsyncJob<Callback> asyncJob = new AsyncJob<Callback>( client, 123 ); Task<Callback> asyncTask = asyncJob.ToTask(); asyncJob.SetFailed( dueToRemoteFailure: true ); Assert.True( asyncTask.IsCompleted, "Async job should be completed after job failure" ); Assert.False( asyncTask.IsCanceled, "Async job should not be canceled after job failure" ); Assert.True( asyncTask.IsFaulted, "Async job should be faulted after job failure" ); await Assert.ThrowsAsync( typeof( AsyncJobFailedException ), async () => await asyncTask ); }
public static StageSetup <TParam, TResult> Stage <TParam, TResult>(AsyncJob <TParam, TResult> job) { return(new StageSetup <TParam, TResult>(new Stage(job))); }
private void OnJobSchedulerStateChange(object _, AsyncJob __) { _UpdateDiagInfo = true; }
private void OnAsyncJobSchedulerEvent(object _, AsyncJob __) { _UpdateText = true; }
public async void AsyncJobTimesout() { SteamClient client = new SteamClient(); client.jobManager.SetTimeoutsEnabled( true ); AsyncJob<Callback> asyncJob = new AsyncJob<Callback>( client, 123 ); asyncJob.Timeout = TimeSpan.FromSeconds( 1 ); Task<Callback> asyncTask = asyncJob.ToTask(); await Task.Delay( TimeSpan.FromSeconds( 5 ) ); Assert.True( asyncTask.IsCompleted, "Async job should be completed after 5 seconds of a 1 second job timeout" ); Assert.True( asyncTask.IsCanceled, "Async job should be canceled after 5 seconds of a 1 second job timeout" ); Assert.False( asyncTask.IsFaulted, "Async job should not be faulted after 5 seconds of a 1 second job timeout" ); await Assert.ThrowsAsync( typeof( TaskCanceledException ), async () => await asyncTask ); }
/// <summary> /// Tracks a job with this manager. /// </summary> /// <param name="asyncJob">The asynchronous job to track.</param> public void StartJob( AsyncJob asyncJob ) { asyncJobs.TryAdd( asyncJob, asyncJob ); }