Last active
August 29, 2015 14:19
-
-
Save theburningmonk/384acf8bd55f3bdcbdfb to your computer and use it in GitHub Desktop.
PostSharp attribute to inject random, configurable latency
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
[Serializable] | |
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class | AttributeTargets.Assembly, | |
AllowMultiple = false)] | |
[MulticastAttributeUsage(MulticastTargets.Method)] | |
public class InjectLatencyAttribute : OnMethodBoundaryAspect | |
{ | |
Random random = new Random((int)DateTime.UtcNow.Ticks); | |
public override void OnEntry(MethodExecutionArgs args) | |
{ | |
// you need a mechanism to turn latency injection, preferably via a | |
// configuration mechanism that allows you to update on the fly | |
if (LatencyInjectionConfig.Enabled) | |
{ | |
var lagSeconds = random.Next(LatencyInjectionConfig.MaxLatencyLagSeconds); | |
var latencyLag = TimeSpan.FromSeconds(lagSeconds); | |
Thread.Sleep(latencyLag); | |
} | |
// execute the underlying method | |
base.OnEntry(args); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment