This repository has been archived by the owner on Apr 13, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
philip pittle
committed
Jul 31, 2014
1 parent
e5c72e2
commit 604f2ec
Showing
2 changed files
with
100 additions
and
0 deletions.
There are no files selected for viewing
99 changes: 99 additions & 0 deletions
99
...tionTests/CompileTests/Interceptors/MeasureMethodExecutionTimeWithAStatefulInterceptor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters