Skip to content

Commit

Permalink
Implement BeforeInject callback
Browse files Browse the repository at this point in the history
  • Loading branch information
iCodeSometime committed Jan 20, 2022
1 parent a18b123 commit 80168d3
Show file tree
Hide file tree
Showing 20 changed files with 478 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,60 @@ public void Should_inject_behaviour_before_executing_user_delegate()
injectedBehaviourExecuted.Should().BeTrue();
}

#region BeforeInject
[Fact]
public async Task Should_call_before_inject_callback_before_injecting_behavior()
{
var beforeInjectExecuted = false;
var injectedBehaviourExecuted = false;

var policy = MonkeyPolicy.InjectBehaviourAsync(with =>
with.Behaviour(async () =>
{
beforeInjectExecuted.Should().BeTrue();
injectedBehaviourExecuted = true;
})
.BeforeInject(async (context, cancellation) =>
{
injectedBehaviourExecuted.Should().BeFalse();
beforeInjectExecuted = true;
})
.InjectionRate(0.6)
.Enabled()
);

await policy.ExecuteAsync(() => Task.CompletedTask);

beforeInjectExecuted.Should().BeTrue();
injectedBehaviourExecuted.Should().BeTrue();
}

[Fact]
public async Task Should_not_call_before_inject_callback_if_not_injecting()
{
var beforeInjectExecuted = false;
var behaviorExecuted = false;

var policy = MonkeyPolicy.InjectBehaviourAsync(with =>
with.Behaviour(async () =>
{
behaviorExecuted = true;
})
.BeforeInject(async (context, cancellation) =>
{
beforeInjectExecuted = true;
})
.InjectionRate(0.4)
.Enabled()
);

await policy.ExecuteAsync(() => Task.CompletedTask);

beforeInjectExecuted.Should().BeFalse();
behaviorExecuted.Should().BeFalse();
}
#endregion

#region invalid threshold on configuration and execution time

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,60 @@ public void Should_inject_behaviour_before_executing_user_delegate()
injectedBehaviourExecuted.Should().BeTrue();
}

#region BeforeInject
[Fact]
public void Should_call_before_inject_callback_before_injecting_behavior()
{
var beforeInjectExecuted = false;
var injectedBehaviourExecuted = false;

var policy = MonkeyPolicy.InjectBehaviour(with =>
with.Behaviour(() =>
{
beforeInjectExecuted.Should().BeTrue();
injectedBehaviourExecuted = true;
})
.BeforeInject((context, cancellation) =>
{
injectedBehaviourExecuted.Should().BeFalse();
beforeInjectExecuted = true;
})
.InjectionRate(0.6)
.Enabled()
);

policy.Execute(() => { });

beforeInjectExecuted.Should().BeTrue();
injectedBehaviourExecuted.Should().BeTrue();
}

[Fact]
public void Should_not_call_before_inject_callback_if_not_injecting()
{
var beforeInjectExecuted = false;
var behaviorExecuted = false;

var policy = MonkeyPolicy.InjectBehaviour(with =>
with.Behaviour(() =>
{
behaviorExecuted = true;
})
.BeforeInject((context, cancellation) =>
{
beforeInjectExecuted = true;
})
.InjectionRate(0.4)
.Enabled()
);

policy.Execute(() => { });

beforeInjectExecuted.Should().BeFalse();
behaviorExecuted.Should().BeFalse();
}
#endregion

#region invalid threshold on configuration and execution time

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,50 @@ public async Task InjectLatency_Context_Free_Should_Not_Introduce_Delay_If_Injec

#endregion

#region BeforeInject
[Fact]
public async Task Should_call_before_inject_callback_if_injecting()
{
var beforeInjectExecuted = false;
var executed = false;

var policy = MonkeyPolicy.InjectLatencyAsync(with =>
with.Latency(TimeSpan.FromMilliseconds(1))
.BeforeInject(async (context, cancellation) => { beforeInjectExecuted = true; })
.InjectionRate(0.6)
.Enabled());

await policy.ExecuteAsync(async () =>
{
beforeInjectExecuted.Should().BeTrue();
executed = true;
});
executed.Should().BeTrue();
beforeInjectExecuted.Should().BeTrue();
}

[Fact]
public async Task Should_not_call_before_inject_callback_if_not_injecting()
{
var beforeInjectExecuted = false;
var executed = false;

var policy = MonkeyPolicy.InjectLatencyAsync(with =>
with.Latency(TimeSpan.FromMilliseconds(1))
.BeforeInject(async (context, cancellation) => { beforeInjectExecuted = true; })
.InjectionRate(0.4)
.Enabled());

await policy.ExecuteAsync(async () =>
{
beforeInjectExecuted.Should().BeFalse();
executed = true;
});
executed.Should().BeTrue();
beforeInjectExecuted.Should().BeFalse();
}
#endregion

#region With Context

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,50 @@ public void InjectLatency_Context_Free_Should_Not_Introduce_Delay_If_InjectionRa

#endregion

#region BeforeInject
[Fact]
public void Should_call_before_inject_callback_if_injecting()
{
var beforeInjectExecuted = false;
var executed = false;

var policy = MonkeyPolicy.InjectLatency(with =>
with.Latency(TimeSpan.FromMilliseconds(1))
.BeforeInject((context, cancellation) => { beforeInjectExecuted = true; })
.InjectionRate(0.6)
.Enabled());

policy.Execute(() =>
{
beforeInjectExecuted.Should().BeTrue();
executed = true;
});
executed.Should().BeTrue();
beforeInjectExecuted.Should().BeTrue();
}

[Fact]
public void Should_not_call_before_inject_callback_if_not_injecting()
{
var beforeInjectExecuted = false;
var executed = false;

var policy = MonkeyPolicy.InjectLatency(with =>
with.Latency(TimeSpan.FromMilliseconds(1))
.BeforeInject((context, cancellation) => { beforeInjectExecuted = true; })
.InjectionRate(0.4)
.Enabled());

policy.Execute(() =>
{
beforeInjectExecuted.Should().BeFalse();
executed = true;
});
executed.Should().BeTrue();
beforeInjectExecuted.Should().BeFalse();
}
#endregion

#region With Context

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,46 @@ public void InjectFault_Context_Free_Enabled_Should_execute_user_delegate_not_th
}
#endregion

#region BeforeInject
[Fact]
public async Task Should_call_before_inject_callback_if_injecting()
{
var beforeInjectExecuted = false;
var executed = false;

var policy = MonkeyPolicy.InjectExceptionAsync(with =>
with.Fault(new Exception())
.BeforeInject(async (context, cancellation) => { beforeInjectExecuted = true; })
.InjectionRate(0.6)
.Enabled());

policy.Awaiting(async p => await p.ExecuteAsync(async () => { executed = true; })).ShouldThrowExactly<Exception>();
executed.Should().BeFalse();
beforeInjectExecuted.Should().BeTrue();
}

[Fact]
public async Task Should_not_call_before_inject_callback_if_not_injecting()
{
var beforeInjectExecuted = false;
var executed = false;

var policy = MonkeyPolicy.InjectExceptionAsync(with =>
with.Fault(new Exception())
.BeforeInject(async (context, cancellation) => { beforeInjectExecuted = true; })
.InjectionRate(0.4)
.Enabled());

await policy.ExecuteAsync(async () =>
{
beforeInjectExecuted.Should().BeFalse();
executed = true;
});
executed.Should().BeTrue();
beforeInjectExecuted.Should().BeFalse();
}
#endregion

#region Basic Overload, Exception, With Context
[Fact]
public void InjectFault_With_Context_Should_not_execute_user_delegate_async()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,50 @@ public void InjectFault_Context_Free_Enabled_Should_execute_user_delegate_async(
}
#endregion

#region BeforeInject
[Fact]
public async Task Should_call_before_inject_callback_if_injecting()
{
var beforeInjectExecuted = false;
var executed = false;

var policy = MonkeyPolicy.InjectResultAsync<ResultPrimitive>(with =>
with.Fault<ResultPrimitive>(new Exception())
.BeforeInject(async (context, cancellation) => { beforeInjectExecuted = true; })
.InjectionRate(0.6)
.Enabled());

policy.Awaiting(async p => await p.ExecuteAsync(async () => { executed = true; return ResultPrimitive.Good; }))
.ShouldThrowExactly<Exception>();

executed.Should().BeFalse();
beforeInjectExecuted.Should().BeTrue();
}

[Fact]
public async Task Should_not_call_before_inject_callback_if_not_injecting()
{
var beforeInjectExecuted = false;
var executed = false;

var policy = MonkeyPolicy.InjectResultAsync<ResultPrimitive>(with =>
with.Fault<ResultPrimitive>(new Exception())
.BeforeInject(async (context, cancellation) => { beforeInjectExecuted = true; })
.InjectionRate(0.4)
.Enabled());

await policy.ExecuteAsync(async () =>
{
beforeInjectExecuted.Should().BeFalse();
executed = true;
return ResultPrimitive.Good;
});
executed.Should().BeTrue();
beforeInjectExecuted.Should().BeFalse();
}
#endregion


#region Basic Overload, Exception, With Context
[Fact]
public void InjectFault_With_Context_Should_not_execute_user_delegate_async()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,49 @@ public void InjectFaultContext_Free_Enabled_Should_execute_user_delegate()
}
#endregion

#region BeforeInject
[Fact]
public void Should_call_before_inject_callback_if_injecting()
{
var beforeInjectExecuted = false;
var executed = false;

var policy = MonkeyPolicy.InjectResult<ResultPrimitive>(with =>
with.Fault<ResultPrimitive>(new Exception())
.BeforeInject((context, cancellation) => { beforeInjectExecuted = true; })
.InjectionRate(0.6)
.Enabled());

policy.Invoking(p => p.Execute(() => { executed = true; return ResultPrimitive.Good; }))
.ShouldThrowExactly<Exception>();

executed.Should().BeFalse();
beforeInjectExecuted.Should().BeTrue();
}

[Fact]
public void Should_not_call_before_inject_callback_if_not_injecting()
{
var beforeInjectExecuted = false;
var executed = false;

var policy = MonkeyPolicy.InjectResult<ResultPrimitive>(with =>
with.Fault<ResultPrimitive>(new Exception())
.BeforeInject((context, cancellation) => { beforeInjectExecuted = true; })
.InjectionRate(0.4)
.Enabled());

policy.Execute(() =>
{
beforeInjectExecuted.Should().BeFalse();
executed = true;
return ResultPrimitive.Good;
});
executed.Should().BeTrue();
beforeInjectExecuted.Should().BeFalse();
}
#endregion

#region Basic Overload, Result as Fault, With Context
[Fact]
public void InjectFaultWith_Context_Should_not_execute_user_delegate()
Expand Down
Loading

0 comments on commit 80168d3

Please sign in to comment.