Пример #1
0
        public void FromWaitHandle_SignaledHandle_SynchronouslyCompletes()
        {
            var mre  = new ManualResetEvent(true);
            var task = AsyncFactory.FromWaitHandle(mre);

            Assert.IsTrue(task.IsCompleted);
        }
Пример #2
0
        public void FromWaitHandle_SignaledHandleWithCanceledToken_SynchronouslyCompletes()
        {
            var mre  = new ManualResetEvent(true);
            var task = AsyncFactory.FromWaitHandle(mre, CancellationTokenHelpers.Canceled);

            Assert.IsTrue(task.IsCompleted);
        }
Пример #3
0
        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);
        }
Пример #4
0
        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);
        }
Пример #5
0
 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);
     }));
 }
Пример #6
0
 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;
     }));
 }
Пример #7
0
 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);
     });
 }
Пример #8
0
 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);
     }));
 }
Пример #9
0
 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);
     }));
 }
Пример #10
0
 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;
     }));
 }
Пример #11
0
 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);
     });
 }
Пример #12
0
 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);
     });
 }
Пример #13
0
        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");
                }
            }
        }
Пример #14
0
        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");
                }
            }
        }
Пример #15
0
        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);
                }
            }
        }