public void FromWaitHandle_SignaledHandle_SynchronouslyCompletes() { var mre = new ManualResetEvent(true); var task = AsyncFactory.FromWaitHandle(mre); Assert.IsTrue(task.IsCompleted); }
public void FromWaitHandle_SignaledHandleWithCanceledToken_SynchronouslyCompletes() { var mre = new ManualResetEvent(true); var task = AsyncFactory.FromWaitHandle(mre, CancellationTokenHelpers.Canceled); Assert.IsTrue(task.IsCompleted); }
public void FromWaitHandle_UnsignaledHandleWithZeroTimeoutAndCanceledToken_SynchronouslyCompletesWithFalseResult() { var mre = new ManualResetEvent(false); var task = AsyncFactory.FromWaitHandle(mre, TimeSpan.Zero, CancellationTokenHelpers.Canceled); Assert.IsTrue(task.IsCompleted); Assert.IsFalse(task.Result); }
public void FromWaitHandle_SignaledHandleWithZeroTimeout_SynchronouslyCompletesWithTrueResult() { var mre = new ManualResetEvent(true); var task = AsyncFactory.FromWaitHandle(mre, TimeSpan.Zero); Assert.IsTrue(task.IsCompleted); Assert.IsTrue(task.Result); }
public void FromWaitHandle_TimeoutBeforeHandleSignalled_CompletesWithFalseResult() { AsyncContext.Run(() => Task.Run(async() => { var mre = new ManualResetEvent(false); var task = AsyncFactory.FromWaitHandle(mre, TimeSpan.FromMilliseconds(10)); var result = await task; Assert.IsFalse(result); })); }
public void FromWaitHandle_HandleSignalled_Completes() { AsyncContext.Run(() => Task.Run(async() => { var mre = new ManualResetEvent(false); var task = AsyncFactory.FromWaitHandle(mre); Assert.IsFalse(task.IsCompleted); mre.Set(); await task; })); }
public void FromWaitHandle_TimeoutBeforeHandleSignalledOrCanceled_CompletesWithFalseResult() { Test.Async(async() => { var mre = new ManualResetEvent(false); var cts = new CancellationTokenSource(); var task = AsyncFactory.FromWaitHandle(mre, TimeSpan.FromMilliseconds(10), cts.Token); var result = await task; Assert.IsFalse(result); }); }
public void FromWaitHandle_HandleSignalledBeforeTimeout_CompletesWithTrueResult() { AsyncContext.Run(() => Task.Run(async() => { var mre = new ManualResetEvent(false); var task = AsyncFactory.FromWaitHandle(mre, Timeout.InfiniteTimeSpan); Assert.IsFalse(task.IsCompleted); mre.Set(); var result = await task; Assert.IsTrue(result); })); }
public void FromWaitHandle_CanceledBeforeTimeoutOrHandleSignalled_CompletesCanceled() { AsyncContext.Run(() => Task.Run(async() => { var mre = new ManualResetEvent(false); var cts = new CancellationTokenSource(); var task = AsyncFactory.FromWaitHandle(mre, Timeout.InfiniteTimeSpan, cts.Token); Assert.IsFalse(task.IsCompleted); cts.Cancel(); await AssertEx.CompletesCanceledAsync(task); })); }
public void FromWaitHandle_HandleSignalledBeforeCanceled_CompletesSuccessfully() { AsyncContext.Run(() => Task.Run(async() => { var mre = new ManualResetEvent(false); var cts = new CancellationTokenSource(); var task = AsyncFactory.FromWaitHandle(mre, cts.Token); Assert.IsFalse(task.IsCompleted); mre.Set(); await task; })); }
public void FromWaitHandle_CanceledBeforeHandleSignalled_CompletesCanceled() { Test.Async(async() => { var mre = new ManualResetEvent(false); var cts = new CancellationTokenSource(); var task = AsyncFactory.FromWaitHandle(mre, cts.Token); Assert.IsFalse(task.IsCompleted); cts.Cancel(); await AssertEx.CompletesCanceledAsync(task); }); }
public void FromWaitHandle_HandleSignalledBeforeTimeoutOrCanceled_CompletesWithTrueResult() { Test.Async(async() => { var mre = new ManualResetEvent(false); var cts = new CancellationTokenSource(); var task = AsyncFactory.FromWaitHandle(mre, TaskShim.InfiniteTimeSpan, cts.Token); Assert.IsFalse(task.IsCompleted); mre.Set(); var result = await task; Assert.IsTrue(result); }); }
public static async Task RequestData <T>(T request) { if (request == null) { throw new NullReferenceException("request is null"); } if (!OpcodesBinding.RequestResponseTypes.ContainsKey(typeof(T))) { throw new ArgumentException("RequestResponseTypes doesn`t contain the response type"); } Type responseType = OpcodesBinding.RequestResponseTypes[typeof(T)]; Packet packet = PacketBuilder.Build(request); if (responseType != typeof(EmptyResponse)) { if (!_dataUpdateNotifiers.ContainsKey(responseType)) { _dataUpdateNotifiers[responseType] = new EventWaitHandle(false, EventResetMode.ManualReset); } _dataUpdateNotifiers[responseType].Reset(); } try { await _controllerClient.SendAsync(_client, packet); } catch (SocketException ex) { _connected = false; _client.Disconnect(); throw ex; } if (responseType != typeof(EmptyResponse)) { bool isSignaled = await AsyncFactory.FromWaitHandle( _dataUpdateNotifiers[responseType], TimeSpan.FromMilliseconds(Config.DataTimeout)); if (!isSignaled) { throw new TimeoutException("Server didn`t respont in time"); } } }
public static async Task RequestData(ClientOpcode opcode) { if (!OpcodesBinding.RequestResponseOpcodes.ContainsKey(opcode)) { throw new ArgumentException("RequestResponseOpcodes doesn`t contain the response type"); } Packet packet = new Packet(opcode, new byte[0]); Type responseType = OpcodesBinding.RequestResponseOpcodes[opcode]; if (responseType != typeof(EmptyResponse)) { if (!_dataUpdateNotifiers.ContainsKey(responseType)) { _dataUpdateNotifiers[responseType] = new EventWaitHandle(false, EventResetMode.ManualReset); } _dataUpdateNotifiers[responseType].Reset(); } try { await _controllerClient.SendAsync(_client, packet); } catch (SocketException ex) { _connected = false; _client.Disconnect(); throw ex; } if (responseType != typeof(EmptyResponse)) { bool isSignaled = await AsyncFactory.FromWaitHandle( _dataUpdateNotifiers[responseType], TimeSpan.FromMilliseconds(Config.DataTimeout)); if (!isSignaled) { throw new TimeoutException("Server didn`t respont in time"); } } }
private async Task <ServerResponse <TContent> > SendAsyncInternal <TContent>( HttpMethod method, string url, object content = null, bool needsEncryption = true, bool needsThreadSafe = true, CancellationToken cancellationToken = default(CancellationToken)) { if (needsEncryption) { await this.EnsureKeysInitializationAsync(); } if (needsThreadSafe) { await AsyncFactory.FromWaitHandle(ChangeKeySemaphore.AvailableWaitHandle, cancellationToken); } CountdownEvent.AddCount(); RequestProcessor requestProcessor = this.RequestProcessorBuilder.UseEncryption(needsEncryption).Build(this.ClientState); using (var request = await requestProcessor.CreateRequestAsync(method, this.Host + url, content)) { using (HttpResponseMessage response = await this.HttpClient.SendAsync(request, cancellationToken).ContinueWith(this.DecrementEvent)) { if (response.StatusCode == (HttpStatusCode)424) { await this.ChangeRoundKey(); return(await this.SendAsyncInternal <TContent>(method, url, content, needsEncryption, needsThreadSafe, cancellationToken)); } var result = await requestProcessor.ProcessResponseAsync <TContent>(response); return(result); } } }