internal void Inject(EHErrorLocation location)
 {
     if (ShouldInject(location))
     {
         throw this.error;
     }
 }
        private void SoftTransientFailures(string name, EHErrorLocation location)
        {
            EventHubsException  injectee = new EventHubsException(true, "ErrorInjector");
            OnceEHErrorInjector injector = new OnceEHErrorInjector(location, injectee);

            NoFailures("SoftTransient" + name + "Failure", injector);
        }
 internal Task InjectTask(EHErrorLocation location)
 {
     if (ShouldInject(location))
     {
         return(Task.FromException(this.error));
     }
     return(Task.CompletedTask);
 }
 internal Task <T> InjectTask <T>(EHErrorLocation location, Task <T> validResult)
 {
     if (ShouldInject(location))
     {
         return(Task.FromException <T>(this.error));
     }
     return(validResult);
 }
 internal override bool ShouldInject(EHErrorLocation location)
 {
     if (this.location == location)
     {
         bool retval = this.first;
         this.first = false;
         return(retval);
     }
     return(false);
 }
        private void GeneralStartupFailure(string name, EHErrorLocation location, bool isEventHubsException)
        {
            TestState state = new TestState();

            state.Initialize(name, 1, 0);

            ServiceFabricProcessor sfp = new ServiceFabricProcessor(
                state.ServiceUri,
                state.ServicePartitionId,
                state.StateManager,
                state.StatefulServicePartition,
                state.Processor,
                state.ConnectionString,
                "$Default",
                state.Options);

            sfp.MockMode = state.PartitionLister;
            Exception injectee = isEventHubsException ? new EventHubsException(false, "ErrorInjector") :
                                 new Exception("ErrorInjector");
            OnceEHErrorInjector injector = new OnceEHErrorInjector(location, injectee);

            sfp.EventHubClientFactory = new InjectorEventHubClientFactoryMock(1, injector);

            state.PrepareToRun();
            state.StartRun(sfp);

            // EXPECTED RESULT: RunAsync will throw (Task completed exceptionally) during startup
            // due to nontransient EventHubsException or other exception type from EH operation.
            // The Wait call bundles the exception into an AggregateException and rethrows.
            state.OuterTask.Wait();
            try
            {
                state.SFPTask.Wait();
            }
            catch (AggregateException ae)
            {
                Assert.True(ae.InnerExceptions.Count == 1, $"Unexpected number of errors {ae.InnerExceptions.Count}");
                Exception inner = ae.InnerExceptions[0];
                if (isEventHubsException)
                {
                    Assert.True(inner is EventHubsException, $"Unexpected inner exception type {inner.GetType().Name}");
                    Assert.False(((EventHubsException)inner).IsTransient, "Inner exception is transient");
                }
                else
                {
                    Assert.True(inner is Exception, $"Unexpected inner exception type {inner.GetType().Name}");
                }
                Assert.Contains("ErrorInjector", inner.Message);
            }
        }
        private void HardTransientStartupFailure(string name, EHErrorLocation location)
        {
            TestState state = new TestState();

            state.Initialize("HardTransient" + name + "Failure", 1, 0);

            ServiceFabricProcessor sfp = new ServiceFabricProcessor(
                state.ServiceUri,
                state.ServicePartitionId,
                state.StateManager,
                state.StatefulServicePartition,
                state.Processor,
                state.ConnectionString,
                "$Default",
                state.Options);

            sfp.MockMode = state.PartitionLister;
            EventHubsException    injectee = new EventHubsException(true, "ErrorInjector");
            AlwaysEHErrorInjector injector = new AlwaysEHErrorInjector(location, injectee);

            sfp.EventHubClientFactory = new InjectorEventHubClientFactoryMock(1, injector);

            state.PrepareToRun();
            state.StartRun(sfp);

            // EXPECTED RESULT: RunAsync will throw (Task completed exceptionally) during startup
            // after running out of retries on an EH operation.
            // The Wait call bundles the exception into an AggregateException and rethrows.
            state.OuterTask.Wait();
            try
            {
                state.SFPTask.Wait();
            }
            catch (AggregateException ae)
            {
                Assert.True(ae.InnerExceptions.Count == 1, $"Unexpected number of errors {ae.InnerExceptions.Count}");
                Exception inner1 = ae.InnerExceptions[0];
                Assert.True(inner1 is Exception, $"Unexpected inner exception type {inner1.GetType().Name}");
                Assert.StartsWith("Out of retries ", inner1.Message);
                Assert.NotNull(inner1.InnerException);
                Exception inner2 = inner1.InnerException;
                Assert.True(inner2 is EventHubsException, $"Unexpected inner exception type {inner2.GetType().Name}");
                Assert.True(((EventHubsException)inner2).IsTransient, "Inner exception is not transient");
                Assert.Equal("ErrorInjector", inner2.Message);
            }
        }
 internal override bool ShouldInject(EHErrorLocation location)
 {
     return(false);
 }
 internal NeverEHErrorInjector(EHErrorLocation errorAt, Exception error) : base(errorAt, error)
 {
 }
 internal override bool ShouldInject(EHErrorLocation location)
 {
     return(this.location == location);
 }
 internal AlwaysEHErrorInjector(EHErrorLocation errorAt, Exception error) : base(errorAt, error)
 {
 }
 internal abstract bool ShouldInject(EHErrorLocation location);
 internal EHErrorInjector(EHErrorLocation errorAt, Exception error)
 {
     this.location = errorAt;
     this.error    = error;
 }