public void ThrowsInvalidOperationExceptionWhenTransmissionIsAlreadySending() { AsyncTest.Run(async() => { Transmission transmission = new Transmission(new Uri("http://uri"), new byte[] { 1, 2, 3, 4, 5 }, new HttpClient(), string.Empty, string.Empty); FieldInfo isSendingField = typeof(Transmission).GetField("isSending", BindingFlags.NonPublic | BindingFlags.Instance); isSendingField.SetValue(transmission, 1); await AssertEx.ThrowsAsync <InvalidOperationException>(() => transmission.SendAsync()); }); }
public void ThrowsInvalidOperationExceptionWhenTransmissionIsAlreadySending() { AsyncTest.Run(async() => { var transmission = new TestableTransmission(); FieldInfo isSendingField = typeof(Transmission).GetField("isSending", BindingFlags.NonPublic | BindingFlags.Instance); isSendingField.SetValue(transmission, 1, BindingFlags.SetField | BindingFlags.NonPublic | BindingFlags.Instance, null, null); await AssertEx.ThrowsAsync <InvalidOperationException>(() => transmission.SendAsync()); }); }
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()); }); }
public void AbortsPreviousAction() { AsyncTest.Run(async() => { var timer = new TaskTimer { Delay = TimeSpan.FromMilliseconds(1) }; bool actionInvoked = false; timer.Start(() => Task.Factory.StartNew(() => actionInvoked = true)); timer.Cancel(); await Task.Delay(20); Assert.False(actionInvoked); }); }
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 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 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); }); }