Skip to content

Instantly share code, notes, and snippets.

@theburningmonk
Last active August 29, 2015 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save theburningmonk/384acf8bd55f3bdcbdfb to your computer and use it in GitHub Desktop.
Save theburningmonk/384acf8bd55f3bdcbdfb to your computer and use it in GitHub Desktop.
PostSharp attribute to inject random, configurable latency
[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