Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement BeforeInject callback #66

Open
wants to merge 1 commit into
base: v1
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make sure to either remove the async keyword from these delegates or await an EmptyTask so that the compiler doesn't complain about the lack of the await keyword:

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

Note: this applies to all the tests you added, you can see the warnings in the build: https://ci.appveyor.com/project/Polly-Contrib/simmy/builds/42270091

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you. I did see the warnings, but haven't had time to address them yet.

})
.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