@@ -21,14 +21,13 @@ internal enum GraphRandomErrorFailMode {
2121}
2222
2323public class GraphRandomErrorConfiguration {
24- public int Rate { get ; set ; } = 0 ;
2524 public List < int > AllowedErrors { get ; set ; } = new ( ) ;
2625}
2726
2827public class GraphRandomErrorPlugin : BaseProxyPlugin {
29- private readonly Option < int ? > _rate ;
3028 private readonly Option < IEnumerable < int > > _allowedErrors ;
3129 private readonly GraphRandomErrorConfiguration _configuration = new ( ) ;
30+ private IProxyConfiguration ? _proxyConfiguration ;
3231
3332 public override string Name => nameof ( GraphRandomErrorPlugin ) ;
3433
@@ -89,15 +88,6 @@ public class GraphRandomErrorPlugin : BaseProxyPlugin {
8988 private readonly Random _random ;
9089
9190 public GraphRandomErrorPlugin ( ) {
92- _rate = new Option < int ? > ( "--failure-rate" , "The percentage of requests to graph to respond with failures" ) ;
93- _rate . AddAlias ( "-f" ) ;
94- _rate . ArgumentHelpName = "failure rate" ;
95- _rate . AddValidator ( ( input ) => {
96- int ? value = input . GetValueForOption ( _rate ) ;
97- if ( value . HasValue && ( value < 0 || value > 100 ) ) {
98- input . ErrorMessage = $ "{ value } is not a valid failure rate. Specify a number between 0 and 100";
99- }
100- } ) ;
10191 _allowedErrors = new Option < IEnumerable < int > > ( "--allowed-errors" , "List of errors that the developer proxy may produce" ) ;
10292 _allowedErrors . AddAlias ( "-a" ) ;
10393 _allowedErrors . ArgumentHelpName = "allowed errors" ;
@@ -123,7 +113,7 @@ private GraphRandomErrorFailMode ShouldFail(ProxyRequestArgs e) {
123113 _throttledRequests . Remove ( key ) ;
124114 }
125115 }
126- return _random . Next ( 1 , 100 ) <= _configuration . Rate ? GraphRandomErrorFailMode . Random : GraphRandomErrorFailMode . PassThru ;
116+ return _random . Next ( 1 , 100 ) <= _proxyConfiguration ? . Rate ? GraphRandomErrorFailMode . Random : GraphRandomErrorFailMode . PassThru ;
127117 }
128118
129119 private void FailResponse ( ProxyRequestArgs e , GraphRandomErrorFailMode failMode ) {
@@ -178,19 +168,20 @@ public override void Register(IPluginEvents pluginEvents,
178168 pluginEvents . Init += OnInit ;
179169 pluginEvents . OptionsLoaded += OnOptionsLoaded ;
180170 pluginEvents . BeforeRequest += OnRequest ;
171+
172+ // needed to get the failure rate configuration
173+ // must keep reference of the whole config rather than just rate
174+ // because rate is an int and can be set through command line args
175+ // which is done after plugins have been registered
176+ _proxyConfiguration = context . Configuration ;
181177 }
182178
183179 private void OnInit ( object ? sender , InitArgs e ) {
184- e . RootCommand . AddOption ( _rate ) ;
185180 e . RootCommand . AddOption ( _allowedErrors ) ;
186181 }
187182
188183 private void OnOptionsLoaded ( object ? sender , OptionsLoadedArgs e ) {
189184 InvocationContext context = e . Context ;
190- // configure probability of failure
191- int ? rate = context . ParseResult . GetValueForOption ( _rate ) ;
192- if ( rate . HasValue )
193- _configuration . Rate = rate . Value ;
194185
195186 // Configure the allowed errors
196187 IEnumerable < int > ? allowedErrors = context . ParseResult . GetValueForOption ( _allowedErrors ) ;
@@ -212,7 +203,7 @@ private async Task OnRequest(object? sender, ProxyRequestArgs e) {
212203 && e . ShouldExecute ( _urlsToWatch ) ) {
213204 var failMode = ShouldFail ( e ) ;
214205
215- if ( failMode == GraphRandomErrorFailMode . PassThru && _configuration . Rate != 100 ) {
206+ if ( failMode == GraphRandomErrorFailMode . PassThru && _proxyConfiguration ? . Rate != 100 ) {
216207 return ;
217208 }
218209 FailResponse ( e , failMode ) ;
0 commit comments