Skip to content

Commit 80168d3

Browse files
committed
Implement BeforeInject callback
Closes Polly-Contrib#65
1 parent a18b123 commit 80168d3

20 files changed

+478
-10
lines changed

src/Polly.Contrib.Simmy.Specs/Behavior/InjectBehaviourAsyncWithOptionsSpecs.cs

+54
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,60 @@ public void Should_inject_behaviour_before_executing_user_delegate()
109109
injectedBehaviourExecuted.Should().BeTrue();
110110
}
111111

112+
#region BeforeInject
113+
[Fact]
114+
public async Task Should_call_before_inject_callback_before_injecting_behavior()
115+
{
116+
var beforeInjectExecuted = false;
117+
var injectedBehaviourExecuted = false;
118+
119+
var policy = MonkeyPolicy.InjectBehaviourAsync(with =>
120+
with.Behaviour(async () =>
121+
{
122+
beforeInjectExecuted.Should().BeTrue();
123+
injectedBehaviourExecuted = true;
124+
})
125+
.BeforeInject(async (context, cancellation) =>
126+
{
127+
injectedBehaviourExecuted.Should().BeFalse();
128+
beforeInjectExecuted = true;
129+
})
130+
.InjectionRate(0.6)
131+
.Enabled()
132+
);
133+
134+
await policy.ExecuteAsync(() => Task.CompletedTask);
135+
136+
beforeInjectExecuted.Should().BeTrue();
137+
injectedBehaviourExecuted.Should().BeTrue();
138+
}
139+
140+
[Fact]
141+
public async Task Should_not_call_before_inject_callback_if_not_injecting()
142+
{
143+
var beforeInjectExecuted = false;
144+
var behaviorExecuted = false;
145+
146+
var policy = MonkeyPolicy.InjectBehaviourAsync(with =>
147+
with.Behaviour(async () =>
148+
{
149+
behaviorExecuted = true;
150+
})
151+
.BeforeInject(async (context, cancellation) =>
152+
{
153+
beforeInjectExecuted = true;
154+
})
155+
.InjectionRate(0.4)
156+
.Enabled()
157+
);
158+
159+
await policy.ExecuteAsync(() => Task.CompletedTask);
160+
161+
beforeInjectExecuted.Should().BeFalse();
162+
behaviorExecuted.Should().BeFalse();
163+
}
164+
#endregion
165+
112166
#region invalid threshold on configuration and execution time
113167

114168
[Fact]

src/Polly.Contrib.Simmy.Specs/Behavior/InjectBehaviourWithOptionsSpecs.cs

+54
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,60 @@ public void Should_inject_behaviour_before_executing_user_delegate()
9191
injectedBehaviourExecuted.Should().BeTrue();
9292
}
9393

94+
#region BeforeInject
95+
[Fact]
96+
public void Should_call_before_inject_callback_before_injecting_behavior()
97+
{
98+
var beforeInjectExecuted = false;
99+
var injectedBehaviourExecuted = false;
100+
101+
var policy = MonkeyPolicy.InjectBehaviour(with =>
102+
with.Behaviour(() =>
103+
{
104+
beforeInjectExecuted.Should().BeTrue();
105+
injectedBehaviourExecuted = true;
106+
})
107+
.BeforeInject((context, cancellation) =>
108+
{
109+
injectedBehaviourExecuted.Should().BeFalse();
110+
beforeInjectExecuted = true;
111+
})
112+
.InjectionRate(0.6)
113+
.Enabled()
114+
);
115+
116+
policy.Execute(() => { });
117+
118+
beforeInjectExecuted.Should().BeTrue();
119+
injectedBehaviourExecuted.Should().BeTrue();
120+
}
121+
122+
[Fact]
123+
public void Should_not_call_before_inject_callback_if_not_injecting()
124+
{
125+
var beforeInjectExecuted = false;
126+
var behaviorExecuted = false;
127+
128+
var policy = MonkeyPolicy.InjectBehaviour(with =>
129+
with.Behaviour(() =>
130+
{
131+
behaviorExecuted = true;
132+
})
133+
.BeforeInject((context, cancellation) =>
134+
{
135+
beforeInjectExecuted = true;
136+
})
137+
.InjectionRate(0.4)
138+
.Enabled()
139+
);
140+
141+
policy.Execute(() => { });
142+
143+
beforeInjectExecuted.Should().BeFalse();
144+
behaviorExecuted.Should().BeFalse();
145+
}
146+
#endregion
147+
94148
#region invalid threshold on configuration and execution time
95149

96150
[Fact]

src/Polly.Contrib.Simmy.Specs/Latency/InjectLatencyAsyncWithOptionsSpecs.cs

+44
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,50 @@ public async Task InjectLatency_Context_Free_Should_Not_Introduce_Delay_If_Injec
105105

106106
#endregion
107107

108+
#region BeforeInject
109+
[Fact]
110+
public async Task Should_call_before_inject_callback_if_injecting()
111+
{
112+
var beforeInjectExecuted = false;
113+
var executed = false;
114+
115+
var policy = MonkeyPolicy.InjectLatencyAsync(with =>
116+
with.Latency(TimeSpan.FromMilliseconds(1))
117+
.BeforeInject(async (context, cancellation) => { beforeInjectExecuted = true; })
118+
.InjectionRate(0.6)
119+
.Enabled());
120+
121+
await policy.ExecuteAsync(async () =>
122+
{
123+
beforeInjectExecuted.Should().BeTrue();
124+
executed = true;
125+
});
126+
executed.Should().BeTrue();
127+
beforeInjectExecuted.Should().BeTrue();
128+
}
129+
130+
[Fact]
131+
public async Task Should_not_call_before_inject_callback_if_not_injecting()
132+
{
133+
var beforeInjectExecuted = false;
134+
var executed = false;
135+
136+
var policy = MonkeyPolicy.InjectLatencyAsync(with =>
137+
with.Latency(TimeSpan.FromMilliseconds(1))
138+
.BeforeInject(async (context, cancellation) => { beforeInjectExecuted = true; })
139+
.InjectionRate(0.4)
140+
.Enabled());
141+
142+
await policy.ExecuteAsync(async () =>
143+
{
144+
beforeInjectExecuted.Should().BeFalse();
145+
executed = true;
146+
});
147+
executed.Should().BeTrue();
148+
beforeInjectExecuted.Should().BeFalse();
149+
}
150+
#endregion
151+
108152
#region With Context
109153

110154
[Fact]

src/Polly.Contrib.Simmy.Specs/Latency/InjectLatencyWithOptionsSpecs.cs

+44
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,50 @@ public void InjectLatency_Context_Free_Should_Not_Introduce_Delay_If_InjectionRa
9999

100100
#endregion
101101

102+
#region BeforeInject
103+
[Fact]
104+
public void Should_call_before_inject_callback_if_injecting()
105+
{
106+
var beforeInjectExecuted = false;
107+
var executed = false;
108+
109+
var policy = MonkeyPolicy.InjectLatency(with =>
110+
with.Latency(TimeSpan.FromMilliseconds(1))
111+
.BeforeInject((context, cancellation) => { beforeInjectExecuted = true; })
112+
.InjectionRate(0.6)
113+
.Enabled());
114+
115+
policy.Execute(() =>
116+
{
117+
beforeInjectExecuted.Should().BeTrue();
118+
executed = true;
119+
});
120+
executed.Should().BeTrue();
121+
beforeInjectExecuted.Should().BeTrue();
122+
}
123+
124+
[Fact]
125+
public void Should_not_call_before_inject_callback_if_not_injecting()
126+
{
127+
var beforeInjectExecuted = false;
128+
var executed = false;
129+
130+
var policy = MonkeyPolicy.InjectLatency(with =>
131+
with.Latency(TimeSpan.FromMilliseconds(1))
132+
.BeforeInject((context, cancellation) => { beforeInjectExecuted = true; })
133+
.InjectionRate(0.4)
134+
.Enabled());
135+
136+
policy.Execute(() =>
137+
{
138+
beforeInjectExecuted.Should().BeFalse();
139+
executed = true;
140+
});
141+
executed.Should().BeTrue();
142+
beforeInjectExecuted.Should().BeFalse();
143+
}
144+
#endregion
145+
102146
#region With Context
103147

104148
[Fact]

src/Polly.Contrib.Simmy.Specs/Outcomes/InjectFaultAsyncWithOptionsSpecs.cs

+40
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,46 @@ public void InjectFault_Context_Free_Enabled_Should_execute_user_delegate_not_th
7777
}
7878
#endregion
7979

80+
#region BeforeInject
81+
[Fact]
82+
public async Task Should_call_before_inject_callback_if_injecting()
83+
{
84+
var beforeInjectExecuted = false;
85+
var executed = false;
86+
87+
var policy = MonkeyPolicy.InjectExceptionAsync(with =>
88+
with.Fault(new Exception())
89+
.BeforeInject(async (context, cancellation) => { beforeInjectExecuted = true; })
90+
.InjectionRate(0.6)
91+
.Enabled());
92+
93+
policy.Awaiting(async p => await p.ExecuteAsync(async () => { executed = true; })).ShouldThrowExactly<Exception>();
94+
executed.Should().BeFalse();
95+
beforeInjectExecuted.Should().BeTrue();
96+
}
97+
98+
[Fact]
99+
public async Task Should_not_call_before_inject_callback_if_not_injecting()
100+
{
101+
var beforeInjectExecuted = false;
102+
var executed = false;
103+
104+
var policy = MonkeyPolicy.InjectExceptionAsync(with =>
105+
with.Fault(new Exception())
106+
.BeforeInject(async (context, cancellation) => { beforeInjectExecuted = true; })
107+
.InjectionRate(0.4)
108+
.Enabled());
109+
110+
await policy.ExecuteAsync(async () =>
111+
{
112+
beforeInjectExecuted.Should().BeFalse();
113+
executed = true;
114+
});
115+
executed.Should().BeTrue();
116+
beforeInjectExecuted.Should().BeFalse();
117+
}
118+
#endregion
119+
80120
#region Basic Overload, Exception, With Context
81121
[Fact]
82122
public void InjectFault_With_Context_Should_not_execute_user_delegate_async()

src/Polly.Contrib.Simmy.Specs/Outcomes/InjectFaultTResultAsyncWithOptionsSpecs.cs

+44
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,50 @@ public void InjectFault_Context_Free_Enabled_Should_execute_user_delegate_async(
6363
}
6464
#endregion
6565

66+
#region BeforeInject
67+
[Fact]
68+
public async Task Should_call_before_inject_callback_if_injecting()
69+
{
70+
var beforeInjectExecuted = false;
71+
var executed = false;
72+
73+
var policy = MonkeyPolicy.InjectResultAsync<ResultPrimitive>(with =>
74+
with.Fault<ResultPrimitive>(new Exception())
75+
.BeforeInject(async (context, cancellation) => { beforeInjectExecuted = true; })
76+
.InjectionRate(0.6)
77+
.Enabled());
78+
79+
policy.Awaiting(async p => await p.ExecuteAsync(async () => { executed = true; return ResultPrimitive.Good; }))
80+
.ShouldThrowExactly<Exception>();
81+
82+
executed.Should().BeFalse();
83+
beforeInjectExecuted.Should().BeTrue();
84+
}
85+
86+
[Fact]
87+
public async Task Should_not_call_before_inject_callback_if_not_injecting()
88+
{
89+
var beforeInjectExecuted = false;
90+
var executed = false;
91+
92+
var policy = MonkeyPolicy.InjectResultAsync<ResultPrimitive>(with =>
93+
with.Fault<ResultPrimitive>(new Exception())
94+
.BeforeInject(async (context, cancellation) => { beforeInjectExecuted = true; })
95+
.InjectionRate(0.4)
96+
.Enabled());
97+
98+
await policy.ExecuteAsync(async () =>
99+
{
100+
beforeInjectExecuted.Should().BeFalse();
101+
executed = true;
102+
return ResultPrimitive.Good;
103+
});
104+
executed.Should().BeTrue();
105+
beforeInjectExecuted.Should().BeFalse();
106+
}
107+
#endregion
108+
109+
66110
#region Basic Overload, Exception, With Context
67111
[Fact]
68112
public void InjectFault_With_Context_Should_not_execute_user_delegate_async()

src/Polly.Contrib.Simmy.Specs/Outcomes/InjectFaultTResultWithOptionsSpecs.cs

+43
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,49 @@ public void InjectFaultContext_Free_Enabled_Should_execute_user_delegate()
6363
}
6464
#endregion
6565

66+
#region BeforeInject
67+
[Fact]
68+
public void Should_call_before_inject_callback_if_injecting()
69+
{
70+
var beforeInjectExecuted = false;
71+
var executed = false;
72+
73+
var policy = MonkeyPolicy.InjectResult<ResultPrimitive>(with =>
74+
with.Fault<ResultPrimitive>(new Exception())
75+
.BeforeInject((context, cancellation) => { beforeInjectExecuted = true; })
76+
.InjectionRate(0.6)
77+
.Enabled());
78+
79+
policy.Invoking(p => p.Execute(() => { executed = true; return ResultPrimitive.Good; }))
80+
.ShouldThrowExactly<Exception>();
81+
82+
executed.Should().BeFalse();
83+
beforeInjectExecuted.Should().BeTrue();
84+
}
85+
86+
[Fact]
87+
public void Should_not_call_before_inject_callback_if_not_injecting()
88+
{
89+
var beforeInjectExecuted = false;
90+
var executed = false;
91+
92+
var policy = MonkeyPolicy.InjectResult<ResultPrimitive>(with =>
93+
with.Fault<ResultPrimitive>(new Exception())
94+
.BeforeInject((context, cancellation) => { beforeInjectExecuted = true; })
95+
.InjectionRate(0.4)
96+
.Enabled());
97+
98+
policy.Execute(() =>
99+
{
100+
beforeInjectExecuted.Should().BeFalse();
101+
executed = true;
102+
return ResultPrimitive.Good;
103+
});
104+
executed.Should().BeTrue();
105+
beforeInjectExecuted.Should().BeFalse();
106+
}
107+
#endregion
108+
66109
#region Basic Overload, Result as Fault, With Context
67110
[Fact]
68111
public void InjectFaultWith_Context_Should_not_execute_user_delegate()

0 commit comments

Comments
 (0)