Files
data
debian
docs
eglib
external
Lucene.Net
Newtonsoft.Json
aspnetwebstack
packages
src
test
Microsoft.TestCommon
Microsoft.Web.Helpers.Test
Microsoft.Web.Http.Data.Test
Microsoft.Web.Mvc.Test
Microsoft.Web.WebPages.OAuth.Test
SPA.Test
System.Json.Test.Integration
System.Json.Test.Unit
System.Net.Http.Formatting.Test.Integration
System.Net.Http.Formatting.Test.Unit
System.Web.Helpers.Test
System.Web.Http.Integration.Test
System.Web.Http.SelfHost.Test
System.Web.Http.Test
System.Web.Http.WebHost.Test
System.Web.Mvc.Test
Ajax
Async
Test
AsyncActionDescriptorTest.cs
AsyncActionMethodSelectorTest.cs
AsyncControllerActionInvokerTest.cs
AsyncManagerTest.cs
AsyncResultWrapperTest.cs
AsyncUtilTest.cs
MockAsyncResult.cs
OperationCounterTest.cs
ReflectedAsyncActionDescriptorTest.cs
ReflectedAsyncControllerDescriptorTest.cs
SignalContainer.cs
SimpleAsyncResultTest.cs
SingleEntryGateTest.cs
SynchronizationContextUtilTest.cs
SynchronousOperationExceptionTest.cs
TaskAsyncActionDescriptorTest.cs
TaskWrapperAsyncResultTest.cs
TriggerListenerTest.cs
ExpressionUtil
Html
Properties
Razor
Test
Util
System.Web.Mvc.Test.csproj
packages.config
System.Web.Razor.Test
System.Web.WebPages.Administration.Test
System.Web.WebPages.Deployment.Test
System.Web.WebPages.Razor.Test
System.Web.WebPages.Test
WebMatrix.Data.Test
WebMatrix.WebData.Test
Settings.StyleCop
tools
.gitattributes
.gitignore
License.txt
README.md
Runtime.msbuild
Runtime.sln
Runtime.xunit
Settings.StyleCop
build.cmd
binary-reference-assemblies
cecil
debian-snapshot
ikdasm
ikvm
referencesource
rx
ikvm-native
libgc
m4
man
mcs
mono
msvc
po
runtime
samples
scripts
support
tools
AUTHORS
COPYING.LIB
ChangeLog.REMOVED.git-id
LICENSE
Makefile.am
Makefile.in
NEWS
README.md
acinclude.m4
aclocal.m4
autogen.sh
build-mingw32.sh
compile
config.guess
config.h.in
config.rpath
config.sub
configure.REMOVED.git-id
configure.ac.REMOVED.git-id
depcomp
install-sh
ltmain.sh.REMOVED.git-id
missing
mkinstalldirs
mono-core.spec
mono-core.spec.in
mono-uninstalled.pc.in
test-driver
winconfig.h
linux-packaging-mono/external/aspnetwebstack/test/System.Web.Mvc.Test/Async/Test/AsyncResultWrapperTest.cs
Jo Shields a575963da9 Imported Upstream version 3.6.0
Former-commit-id: da6be194a6b1221998fc28233f2503bd61dd9d14
2014-08-13 10:39:27 +01:00

231 lines
7.3 KiB
C#

// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using System.Threading;
using Moq;
using Xunit;
using Assert = Microsoft.TestCommon.AssertEx;
namespace System.Web.Mvc.Async.Test
{
public class AsyncResultWrapperTest
{
[Fact]
public void Begin_AsynchronousCompletion()
{
// Arrange
AsyncCallback capturedCallback = null;
IAsyncResult resultGivenToCallback = null;
IAsyncResult innerResult = new MockAsyncResult();
// Act
IAsyncResult outerResult = AsyncResultWrapper.Begin(
ar => { resultGivenToCallback = ar; },
null,
(callback, state) =>
{
capturedCallback = callback;
return innerResult;
},
ar => { });
capturedCallback(innerResult);
// Assert
Assert.Equal(outerResult, resultGivenToCallback);
}
[Fact]
public void Begin_ReturnsAsyncResultWhichWrapsInnerResult()
{
// Arrange
IAsyncResult innerResult = new MockAsyncResult()
{
AsyncState = "inner state",
CompletedSynchronously = true,
IsCompleted = true
};
// Act
IAsyncResult outerResult = AsyncResultWrapper.Begin(
null, "outer state",
(callback, state) => innerResult,
ar => { });
// Assert
Assert.Equal(innerResult.AsyncState, outerResult.AsyncState);
Assert.Equal(innerResult.AsyncWaitHandle, outerResult.AsyncWaitHandle);
Assert.Equal(innerResult.CompletedSynchronously, outerResult.CompletedSynchronously);
Assert.Equal(innerResult.IsCompleted, outerResult.IsCompleted);
}
[Fact]
public void Begin_SynchronousCompletion()
{
// Arrange
IAsyncResult resultGivenToCallback = null;
IAsyncResult innerResult = new MockAsyncResult();
// Act
IAsyncResult outerResult = AsyncResultWrapper.Begin(
ar => { resultGivenToCallback = ar; },
null,
(callback, state) =>
{
callback(innerResult);
return innerResult;
},
ar => { });
// Assert
Assert.Equal(outerResult, resultGivenToCallback);
}
[Fact]
public void Begin_AsynchronousButAlreadyCompleted()
{
// Arrange
Mock<IAsyncResult> innerResultMock = new Mock<IAsyncResult>();
innerResultMock.Setup(ir => ir.CompletedSynchronously).Returns(false);
innerResultMock.Setup(ir => ir.IsCompleted).Returns(true);
// Act
IAsyncResult outerResult = AsyncResultWrapper.Begin(
null,
null,
(callback, state) =>
{
callback(innerResultMock.Object);
return innerResultMock.Object;
},
ar => { });
// Assert
Assert.True(outerResult.CompletedSynchronously);
}
[Fact]
public void BeginSynchronous_Action()
{
// Arrange
bool actionCalled = false;
// Act
IAsyncResult asyncResult = AsyncResultWrapper.BeginSynchronous(null, null, delegate { actionCalled = true; });
AsyncResultWrapper.End(asyncResult);
// Assert
Assert.True(actionCalled);
Assert.True(asyncResult.IsCompleted);
Assert.True(asyncResult.CompletedSynchronously);
}
[Fact]
public void BeginSynchronous_Func()
{
// Act
IAsyncResult asyncResult = AsyncResultWrapper.BeginSynchronous(null, null, () => 42);
int retVal = AsyncResultWrapper.End<int>(asyncResult);
// Assert
Assert.Equal(42, retVal);
Assert.True(asyncResult.IsCompleted);
Assert.True(asyncResult.CompletedSynchronously);
}
[Fact]
public void End_ExecutesStoredDelegateAndReturnsValue()
{
// Arrange
IAsyncResult asyncResult = AsyncResultWrapper.Begin(
null, null,
(callback, state) => new MockAsyncResult(),
ar => 42);
// Act
int returned = AsyncResultWrapper.End<int>(asyncResult);
// Assert
Assert.Equal(42, returned);
}
[Fact]
public void End_ThrowsIfAsyncResultIsIncorrectType()
{
// Arrange
IAsyncResult asyncResult = AsyncResultWrapper.Begin(
null, null,
(callback, state) => new MockAsyncResult(),
ar => { });
// Act & assert
Assert.Throws<ArgumentException>(
delegate { AsyncResultWrapper.End<int>(asyncResult); },
@"The provided IAsyncResult is not valid for this method.
Parameter name: asyncResult");
}
[Fact]
public void End_ThrowsIfAsyncResultIsNull()
{
// Act & assert
Assert.ThrowsArgumentNull(
delegate { AsyncResultWrapper.End(null); }, "asyncResult");
}
[Fact]
public void End_ThrowsIfAsyncResultTagMismatch()
{
// Arrange
IAsyncResult asyncResult = AsyncResultWrapper.Begin(
null, null,
(callback, state) => new MockAsyncResult(),
ar => { },
"some tag");
// Act & assert
Assert.Throws<ArgumentException>(
delegate { AsyncResultWrapper.End(asyncResult, "some other tag"); },
@"The provided IAsyncResult is not valid for this method.
Parameter name: asyncResult");
}
[Fact]
public void End_ThrowsIfCalledTwiceOnSameAsyncResult()
{
// Arrange
IAsyncResult asyncResult = AsyncResultWrapper.Begin(
null, null,
(callback, state) => new MockAsyncResult(),
ar => { });
// Act & assert
AsyncResultWrapper.End(asyncResult);
Assert.Throws<InvalidOperationException>(
delegate { AsyncResultWrapper.End(asyncResult); },
@"The provided IAsyncResult has already been consumed.");
}
[Fact]
public void TimedOut()
{
// Arrange
ManualResetEvent waitHandle = new ManualResetEvent(false /* initialState */);
AsyncCallback callback = ar => { waitHandle.Set(); };
// Act & assert
IAsyncResult asyncResult = AsyncResultWrapper.Begin(
callback, null,
(innerCallback, innerState) => new MockAsyncResult(),
ar => { Assert.True(false, "This callback should never execute since we timed out."); },
null, 0);
// wait for the timeout
waitHandle.WaitOne();
Assert.Throws<TimeoutException>(
delegate { AsyncResultWrapper.End(asyncResult); });
}
}
}