Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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