Skip to content
This repository has been archived by the owner on Apr 13, 2020. It is now read-only.

Commit

Permalink
New test for persistent interceptor
Browse files Browse the repository at this point in the history
  • Loading branch information
philip pittle committed Jul 31, 2014
1 parent e5c72e2 commit 604f2ec
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
//-----------------------------------------------------------------------
// <copyright file="MeasureMethodExecutionTimeWithAStatefulInterceptor.cs" company="Copacetic Software">
// Copyright (c) Copacetic Software.
// <author>Philip Pittle</author>
// <date>Thursday, July 31, 2014 10:50:53 AM</date>
// Licensed under the Apache License, Version 2.0,
// you may not use this file except in compliance with this 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.
// </copyright>
//-----------------------------------------------------------------------

using CopaceticSoftware.pMixins.Interceptors;
using CopaceticSoftware.pMixins.Tests.Common.Extensions;
using NBehave.Spec.NUnit;
using NUnit.Framework;

namespace CopaceticSoftware.pMixins.CodeGenerator.Tests.IntegrationTests.CompileTests.Interceptors
{
/// <summary>
/// Tests if the same Interceptor instance is preserved
/// between <see cref="IMixinInterceptor.OnBeforeMethodInvocation"/>
/// and
/// <see cref="IMixinInterceptor.OnAfterMethodInvocation"/>
/// </summary>
[TestFixture]
public class MeasureMethodExecutionTimeWithAStatefulInterceptor : GenerateCodeAndCompileTestBase
{
protected override string SourceCode
{
get {
return @"
using System;
using System.Diagnostics;
using System.Threading;
using CopaceticSoftware.pMixins.Attributes;
using CopaceticSoftware.pMixins.Interceptors;
namespace Test{
public class PerformanceMeasurement : MixinInterceptorBase
{
private Stopwatch _stopwatch;
public override void OnBeforeMethodInvocation(object sender, MethodEventArgs eventArgs)
{
_stopwatch = Stopwatch.StartNew();
}
public override void OnAfterMethodInvocation(object sender, MethodEventArgs eventArgs)
{
if (null == _stopwatch)
throw new Exception ("" OnBeforeMethodInvocation was not called on this instance! "");
_stopwatch.Stop();
eventArgs.ReturnValue = _stopwatch.ElapsedMilliseconds;
}
}
public class Mixin
{
public long GetMethodExecutionTime()
{
//Sleep for 5 ms
Thread.Sleep(5);
return 0;
}
}
[pMixin(Mixin = typeof(Mixin), Interceptors = new []{typeof(PerformanceMeasurement)})]
public partial class Target
{
}
}
"; }
}

[Test]
public void CanGetMethodExecutionTime()
{
CompilerResults
.ExecuteMethod<long>(
"Test.Target",
"GetMethodExecutionTime")

.ShouldBeGreaterThan(0);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@
<Compile Include="IntegrationTests\CompileTests\Interceptors\InterceptVirtualMethods.cs" />
<Compile Include="IntegrationTests\CompileTests\Interceptors\InterceptVirtualProperties.cs" />
<Compile Include="IntegrationTests\CompileTests\Interceptors\ManipulateReturnValueInterceptorTest.cs" />
<Compile Include="IntegrationTests\CompileTests\Interceptors\MeasureMethodExecutionTimeWithAStatefulInterceptor.cs" />
<Compile Include="IntegrationTests\CompileTests\MixinDependency\MixinDependencyIsClass.cs" />
<Compile Include="IntegrationTests\CompileTests\MixinDependency\MixinDependencyIsImplementedByTarget.cs" />
<Compile Include="IntegrationTests\CompileTests\MixinDependency\MixinDependencyIsInheritedByTarget.cs" />
Expand Down

0 comments on commit 604f2ec

Please sign in to comment.