Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright 2004-2016 Castle Project - http://www.castleproject.org/
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace Castle.DynamicProxy.Tests
{
using System;
using System.Linq;
using System.Threading.Tasks;

using Castle.DynamicProxy.Tests.Classes;
using Castle.DynamicProxy.Tests.Interfaces;
using Castle.DynamicProxy.Tests.Interceptors;

using NUnit.Framework;

[TestFixture]
public class AsyncInterceptorTestCase : BasePEVerifyTestCase
{
[Test]
public async Task Should_Intercept_Asynchronous_Methods_With_An_Async_Operations_Prior_To_Calling_Proceed()
{
// Arrange
IInterfaceWithAsynchronousMethod target = new ClassWithAsynchronousMethod();
IInterceptor interceptor = new AsyncInterceptor();

IInterfaceWithAsynchronousMethod proxy =
generator.CreateInterfaceProxyWithTargetInterface(target, interceptor);

// Act
await proxy.Method().ConfigureAwait(false);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ public void Finalize_method_is_proxied_even_though_its_not_the_best_idea_ever()

public class ResultModifierInterceptor : StandardInterceptor
{
protected override void PostProceed(IInvocation invocation)
protected override void PostProceed(IInvocation invocation, InvocationDelegate proceed)
{
object returnValue = invocation.ReturnValue;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ public ChangeProxyTargetInterceptor(object target)
this.target = target;
}

public void Intercept(IInvocation invocation)
public void Intercept(IInvocation invocation, InvocationDelegate proceed)
{
var targetAccessor = invocation.Proxy as IProxyTargetAccessor;
Assert.IsNotNull(targetAccessor);
targetAccessor.DynProxySetTarget(target);
invocation.Proceed();
proceed(invocation);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public LazyInterceptorV1(Lazy<T> lazyTarget)

private Lazy<T> LazyTarget { get; }

public void Intercept(IInvocation invocation)
public void Intercept(IInvocation invocation, InvocationDelegate proceed)
{
var target = invocation.InvocationTarget as T;
if (target == null)
Expand All @@ -37,7 +37,7 @@ public void Intercept(IInvocation invocation)
((IProxyTargetAccessor)invocation.Proxy).DynProxySetTarget(LazyTarget.Value);
}

invocation.Proceed();
proceed(invocation);
}
}

Expand All @@ -51,7 +51,7 @@ public LazyInterceptorV2(Lazy<T> lazyTarget)

private Lazy<T> LazyTarget { get; }

public void Intercept(IInvocation invocation)
public void Intercept(IInvocation invocation, InvocationDelegate proceed)
{
var target = invocation.InvocationTarget as T;
if (target == null)
Expand All @@ -62,7 +62,7 @@ public void Intercept(IInvocation invocation)
#pragma warning restore CS0618 // obsolete
}

invocation.Proceed();
proceed(invocation);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright 2004-2010 Castle Project - http://www.castleproject.org/
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace Castle.DynamicProxy.Tests.Classes
{
using System;
using System.Threading;
using System.Threading.Tasks;

using Castle.DynamicProxy.Tests.Interfaces;

public class ClassWithAsynchronousMethod : IInterfaceWithAsynchronousMethod
{
public async Task Method()
{
Console.WriteLine(
$"Before Await ClassWithAsynchronousMethod:Method ThreadId='{Thread.CurrentThread.ManagedThreadId}'.",
Thread.CurrentThread.ManagedThreadId);

await Task.Delay(10).ConfigureAwait(false);

Console.WriteLine(
$"After Await ClassWithAsynchronousMethod:Method ThreadId='{Thread.CurrentThread.ManagedThreadId}'.",
Thread.CurrentThread.ManagedThreadId);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ public sealed class Interceptor : IInterceptor
{
public Type ReceivedTargetType { get; private set; }

public void Intercept(IInvocation invocation)
public void Intercept(IInvocation invocation, InvocationDelegate proceed)
{
ReceivedTargetType = invocation.TargetType;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ public class AddTwoInterceptor : IInterceptor
{
#region IInterceptor Members

public void Intercept(IInvocation invocation)
public void Intercept(IInvocation invocation, InvocationDelegate proceed)
{
invocation.Proceed();
proceed(invocation);
var ret = (int) invocation.ReturnValue;
ret += 2;
invocation.ReturnValue = ret;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ public class AssertCanChangeTargetInterceptor : IInterceptor
{
#region IInterceptor Members

public void Intercept(IInvocation invocation)
public void Intercept(IInvocation invocation, InvocationDelegate proceed)
{
Assert.IsInstanceOf(typeof (IChangeProxyTarget), invocation);
invocation.Proceed();
proceed(invocation);
}

#endregion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ namespace Castle.DynamicProxy.Tests.Interceptors

public class AssertCannotChangeTargetInterceptor : IInterceptor
{
public void Intercept(IInvocation invocation)
public void Intercept(IInvocation invocation, InvocationDelegate proceed)
{
Assert.IsNotInstanceOf<IChangeProxyTarget>(invocation);
invocation.Proceed();
proceed(invocation);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright 2004-2010 Castle Project - http://www.castleproject.org/
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace Castle.DynamicProxy.Tests.Interceptors
{
using System.Threading.Tasks;

public class AsyncInterceptor : IInterceptor
{
public void Intercept(IInvocation invocation, InvocationDelegate proceed)
{
invocation.ReturnValue = InterceptAsyncMethod(invocation, proceed);
}

private static async Task InterceptAsyncMethod(IInvocation invocation, InvocationDelegate proceed)
{
// It all falls down when executing async before calling Proceed().
await Task.Delay(10).ConfigureAwait(false);

proceed(invocation);

// Hmmmmm, now with it simplified down to this, I see the glaring hole that is the return value being set
// in two situations.
Task returnValue = (Task)invocation.ReturnValue;

await returnValue.ConfigureAwait(false);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ public int Count

#region IInterceptor Members

public void Intercept(IInvocation invocation)
public void Intercept(IInvocation invocation, InvocationDelegate proceed)
{
count++;
invocation.Proceed();
proceed(invocation);
}

#endregion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ public ChangeTargetInterceptor(object target)
this.target = target;
}

public void Intercept(IInvocation invocation)
public void Intercept(IInvocation invocation, InvocationDelegate proceed)
{
IChangeProxyTarget changeTarget = (IChangeProxyTarget) invocation;
changeTarget.ChangeInvocationTarget(target);
invocation.Proceed();
proceed(invocation);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace Castle.DynamicProxy.Tests.Interceptors
{
public class DoNothingInterceptor : IInterceptor
{
public void Intercept(IInvocation invocation)
public void Intercept(IInvocation invocation, InvocationDelegate proceed)
{
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ public IInvocation Invocation
get { return invocation; }
}

public void Intercept(IInvocation invocation)
public void Intercept(IInvocation invocation, InvocationDelegate proceed)
{
this.invocation = invocation;
var concreteMethod = invocation.GetConcreteMethod();

if (invocation.MethodInvocationTarget != null)
{
invocation.Proceed();
proceed(invocation);
}
else if (concreteMethod.ReturnType.GetTypeInfo().IsValueType && !concreteMethod.ReturnType.Equals(typeof(void)))
// ensure valid return value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,18 @@ public class LogInvocationInterceptor : StandardInterceptor

public bool Proceed = true;

protected override void PreProceed(IInvocation invocation)
protected override void PreProceed(IInvocation invocation, InvocationDelegate proceed)
{
invocations.Add(invocation.Method.Name);

sb.Append(String.Format("{0} ", invocation.Method.Name));
}

protected override void PerformProceed (IInvocation invocation)
protected override void PerformProceed (IInvocation invocation, InvocationDelegate proceed)
{
if (Proceed)
{
base.PerformProceed (invocation);
base.PerformProceed (invocation, proceed);
}
else if (invocation.Method.ReturnType.GetTypeInfo().IsValueType && invocation.Method.ReturnType != typeof (void))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ public ProceedNTimesInterceptor(int retries)
this.retries = retries;
}

public void Intercept(IInvocation invocation)
public void Intercept(IInvocation invocation, InvocationDelegate proceed)
{
for (var i = 0; i < retries; i++)
{
try
{
invocation.Proceed();
proceed(invocation);
}
catch
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ public ProceedOnTypeInterceptor(Type type)
this.type = type;
}

public void Intercept(IInvocation invocation)
public void Intercept(IInvocation invocation, InvocationDelegate proceed)
{
type = typeof(IBarFoo);
if (invocation.Method.DeclaringType != type)
{
invocation.Proceed();
proceed(invocation);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace Castle.DynamicProxy.Tests.Interceptors

public class RequiredParamInterceptor : IInterceptor
{
public void Intercept(IInvocation invocation)
public void Intercept(IInvocation invocation, InvocationDelegate proceed)
{
ParameterInfo[] parameters = invocation.Method.GetParameters();

Expand All @@ -42,7 +42,7 @@ public void Intercept(IInvocation invocation)
}
}

invocation.Proceed();
proceed(invocation);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public SetArgumentValueInterceptor(int index, object value)
this.value = value;
}

public void Intercept(IInvocation invocation)
public void Intercept(IInvocation invocation, InvocationDelegate proceed)
{
invocation.SetArgumentValue(index, value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public SetReturnValueInterceptor(object value)
}


public void Intercept(IInvocation invocation)
public void Intercept(IInvocation invocation, InvocationDelegate proceed)
{
invocation.ReturnValue = value;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace Castle.DynamicProxy.Tests.Interceptors
{
public class ThrowingInterceptor : IInterceptor
{
public void Intercept(IInvocation invocation)
public void Intercept(IInvocation invocation, InvocationDelegate proceed)
{
throw new ThrowingInterceptorException("Because I feel like it");
}
Expand Down
Loading