public void AbortsWebRequestWhenBeginGetRequestStreamTimesOut()
            {
                var requestAborted = new ManualResetEventSlim();
                var finishBeginGetRequestStream = new ManualResetEventSlim();
                var request = new StubWebRequest();
                request.OnAbort = () => requestAborted.Set();
                request.OnBeginGetRequestStream = (callback, state) => TaskEx.Run(() => finishBeginGetRequestStream.Wait()).AsAsyncResult(callback, request);
                var transmission = new TestableTransmission(timeout: TimeSpan.FromTicks(1));
                transmission.OnCreateRequest = uri => request;

                Task sendAsync = transmission.SendAsync();

                Assert.True(requestAborted.Wait(1000));
                finishBeginGetRequestStream.Set();
            }
     public void DoesNotAbortRequestThatWasSentSuccessfully()
     {
         AsyncTest.Run(async () =>
         {
             bool requestAborted = false;
             var request = new StubWebRequest { OnAbort = () => requestAborted = true };
 
             var transmission = new TestableTransmission(timeout: TimeSpan.FromMilliseconds(50));
             transmission.OnCreateRequest = uri => request;
 
             await transmission.SendAsync();
 
             await TaskEx.Delay(50); // Let timout detector finish
 
             Assert.False(requestAborted);
         });
     }
     public void DisposesHttpWebResponseToReleaseResources()
     {
         AsyncTest.Run(async () =>
         {
             bool responseDisposed = false;
             var response = new StubWebResponse { OnDispose = () => responseDisposed = true };
             var request = new StubWebRequest { OnEndGetResponse = asyncResult => response };
             var transmission = new TestableTransmission { OnCreateRequest = uri => request };
 
             await transmission.SendAsync();
 
             Assert.True(responseDisposed);
         });
     }
     public void AsynchronouslyFinishesGettingResponse()
     {
         AsyncTest.Run(async () =>
         {
             int endGetResponseCount = 0;
             var request = new StubWebRequest();
             request.OnEndGetResponse = asyncResult =>
             {
                 endGetResponseCount++;
                 return new StubWebResponse();
             };
 
             var transmission = new TestableTransmission { OnCreateRequest = uri => request };
 
             await transmission.SendAsync();
 
             Assert.Equal(1, endGetResponseCount);
         });
     }
     public void WritesTransmissionContentToRequestStream()
     {
         AsyncTest.Run(async () =>
         {
             var requestStream = new MemoryStream();
 
             var request = new StubWebRequest();
             request.OnEndGetRequestStream = asyncResult => requestStream;
 
             byte[] transmissionContent = new byte[] { 1, 2, 3, 4, 5 };
             var transmission = new TestableTransmission(new Uri("http://test.uri"), transmissionContent);
             transmission.OnCreateRequest = uri => request;
 
             await transmission.SendAsync();
 
             Assert.Equal(transmissionContent, requestStream.ToArray());
         });
     }
     public void BeginsAsynchronouslyGettingRequestStream()
     {
         AsyncTest.Run(async () =>
         {
             int beginGetRequestStreamCount = 0;
             var request = new StubWebRequest();
             request.OnBeginGetRequestStream = (callback, state) =>
             {
                 beginGetRequestStreamCount++;
                 return TaskEx.FromResult<object>(null).AsAsyncResult(callback, request);
             };
 
             var transmission = new TestableTransmission { OnCreateRequest = uri => request };
 
             await transmission.SendAsync();
 
             Assert.Equal(1, beginGetRequestStreamCount);
         });
     }
     public void ThrowsInvalidOperationExceptionWhenTransmissionIsAlreadySending()
     {
         AsyncTest.Run(async () =>
         {
             var request = new StubWebRequest();
             request.OnBeginGetRequestStream = (callback, state) => TaskEx.Delay(TimeSpan.FromMilliseconds(10)).AsAsyncResult(callback, request);
 
             var transmission = new TestableTransmission();
             transmission.OnCreateRequest = uri => request;
 
             Task dontWait = transmission.SendAsync();
             await AssertEx.ThrowsAsync<InvalidOperationException>(() => transmission.SendAsync());
         });
     }