public async void MockServerProcessorCancelTokenAfterRequest()
        {
            _mockServerProcessor = new MockServerProcessor(new AssertFactory(), new RuleMatcher(), new TemplateContext());

            var scenarios = GenerateRandomScenarios(out var httpContext);

            var input  = new MessageProcessorInput(httpContext.Request, scenarios);
            var Target = _mockServerProcessor;

            var cancelledTokenSource = new CancellationTokenSource();

            Target.Start();

            // fill the pipeline with tasks
            var fillPipeline = new Task(() =>
            {
                while (true)
                {
                    _ = Target.Push(input, cancelledTokenSource.Token);
                }
            });

            fillPipeline.Start();
            await Task.Delay(1000);

            cancelledTokenSource.Cancel();

            var Actual = Target.PipelineIsRunning;

            // should not accept anymore data after it has been cancelled
            _ = Target.Push(input, cancelledTokenSource.Token);

            Assert.False(Actual);
        }
        public MockServerProcessorCancelTokenTests()
        {
            // these have been adapted from the MockServerProcessor test file
            Randomizer.Seed = new Random(FilterTestHelpers.Seed);
            var fakerJObject = new Faker <JObject>()
                               .CustomInstantiator(f => JObject.FromObject(new { Value = f.Random.AlphaNumeric(TestUtils.GetRandomStringLength()) }));
            var fakerBodyRule = new Faker <BodyRule>()
                                .CustomInstantiator(f => new BodyRule(f.PickRandom <ComparerType>(), fakerJObject.Generate()));
            var fakerHeaderQueryRule = new Faker <KeyValuePairRule>()
                                       .CustomInstantiator(f => new KeyValuePairRule()
            {
                Type = f.PickRandom <ComparerType>(), RuleValue = new KeyValuePair <string, string>(f.Random.String(), f.Random.String())
            });
            var fakerResponse = new Faker <MockResponse>()
                                .CustomInstantiator(f => new MockResponse(
                                                        (int)f.PickRandom <HttpStatusCode>(),
                                                        f.Lorem.Paragraph()
                                                        ));
            var fakerRequestMatchRules = new Faker <RequestMatchRules>()
                                         .RuleFor(m => m.BodyRules, _ => fakerBodyRule.Generate(3))
                                         .RuleFor(m => m.HeaderRules, f => fakerHeaderQueryRule.Generate(5))
                                         .RuleFor(m => m.QueryRules, f => fakerHeaderQueryRule.Generate(5));

            _fakerScenario = new Faker <Scenario>()
                             .RuleFor(m => m.Id, f => f.Random.Guid().ToString())
                             .RuleFor(m => m.Response, f => fakerResponse.Generate())
                             .RuleFor(m => m.RequestMatchRules, f => fakerRequestMatchRules.Generate())
                             .RuleFor(m => m.Path, f => $"/{f.Random.Word().Replace(" ", "")}")
                             .RuleFor(m => m.Verb, f => f.PickRandom(_validMethods));
            _mockServerProcessor = new MockServerProcessor(new AssertFactory(), new RuleMatcher(), new TemplateContext());
        }
        public async void MockServerProcessorCancelTokenAfterRequestEnsuringInterpipelineEventsAreProcessed()
        {
            _mockServerProcessor = new MockServerProcessor(new AssertFactory(), new RuleMatcher(), new TemplateContext());

            var Target = _mockServerProcessor;

            var pipelineCancelTokenSource       = new CancellationTokenSource();
            var pipelineFillerCancelTokenSource = new CancellationTokenSource();

            Target.Start();

            // fill the pipeline with tasks as fast as possible (so that they queue)
            var fillPipeline = Task <List <Task <MockResponse> > > .Factory.StartNew(() =>
            {
                var itemsPushedIntoPipeline = new List <Task <MockResponse> >();
                while (!pipelineFillerCancelTokenSource.Token.IsCancellationRequested)
                {
                    var scenarios = GenerateRandomScenarios(out var httpContext);

                    // must be valid JSON so it can propagate through the pipeline (and use up more CPU processing
                    // the JSON because it will allow it to simulate real-world conditions)
                    JObject sampleResponse = new JObject {
                        ["test"] = "message"
                    };

                    httpContext.Request.Body = GenerateStreamFromString(sampleResponse.ToString());

                    var input = new MessageProcessorInput(httpContext.Request, scenarios);
                    itemsPushedIntoPipeline.Add(Target.Push(input, pipelineCancelTokenSource.Token));
                }

                return(itemsPushedIntoPipeline);
            }, pipelineFillerCancelTokenSource.Token);

            // allow the pipeline to be sufficiently full (must be a long time to make the queue catch up)
            await Task.Delay(10000);

            pipelineFillerCancelTokenSource.Cancel();
            pipelineCancelTokenSource.Cancel();

            fillPipeline.Wait();

            fillPipeline.Result.ForEach(x => x.Wait());
        }
        public MockServerProcessorTests()
        {
            Randomizer.Seed = new Random(FilterTestHelpers.Seed);
            var fakerJObject = new Faker <JObject>()
                               .CustomInstantiator(f => JObject.FromObject(new { Value = f.Random.AlphaNumeric(TestUtils.GetRandomStringLength()) }));
            var fakerBodyRule = new Faker <BodyRule>()
                                .CustomInstantiator(f => new BodyRule(f.PickRandomWithout <ComparerType>(ComparerType.JSONSCHEMA, ComparerType.JSONPATH, ComparerType.TEXTCONTAINS, ComparerType.TEXTENDSWITH, ComparerType.TEXTEQUALS, ComparerType.TEXTSTARTSWITH, ComparerType.REGEX), fakerJObject.Generate()));
            var fakerHeaderQueryRule = new Faker <KeyValuePairRule>()
                                       .CustomInstantiator(f => new KeyValuePairRule()
            {
                Type = f.PickRandomWithout <ComparerType>(ComparerType.JSONCONTAINS, ComparerType.JSONEQUALITY, ComparerType.JSONPATH, ComparerType.JSONSCHEMA), RuleValue = new KeyValuePair <string, string>(f.Random.String(), f.Random.String())
            });
            var fakerUrlRule = new Faker <KeyValuePairRule>()
                               .CustomInstantiator(f => new KeyValuePairRule()
            {
                Type = f.PickRandomWithout <ComparerType>(ComparerType.JSONCONTAINS, ComparerType.JSONEQUALITY, ComparerType.JSONPATH, ComparerType.JSONSCHEMA, ComparerType.TEXTCONTAINS, ComparerType.TEXTENDSWITH, ComparerType.TEXTSTARTSWITH, ComparerType.REGEX), RuleValue = new KeyValuePair <string, string>(f.Random.String(), f.Random.String())
            });
            var fakerResponse = new Faker <MockResponse>()
                                .CustomInstantiator(f => new MockResponse(
                                                        (int)f.PickRandom <HttpStatusCode>(),
                                                        f.Lorem.Paragraph()
                                                        ));
            var fakerRequestMatchRules = new Faker <RequestMatchRules>()
                                         .RuleFor(m => m.BodyRules, _ => fakerBodyRule.Generate(3))
                                         .RuleFor(m => m.HeaderRules, f => fakerHeaderQueryRule.Generate(3))
                                         .RuleFor(m => m.QueryRules, f => fakerHeaderQueryRule.Generate(3))
                                         .RuleFor(m => m.UrlRules, f => fakerUrlRule.Generate(3));

            this.fakerScenario = new Faker <Scenario>()
                                 .RuleFor(m => m.Id, f => f.Random.Guid().ToString())
                                 .RuleFor(m => m.Response, f => fakerResponse.Generate())
                                 .RuleFor(m => m.RequestMatchRules, f => fakerRequestMatchRules.Generate())
                                 .RuleFor(m => m.Path, f => $"/{f.Random.Word().Replace(" ", "")}")
                                 .RuleFor(m => m.Verb, f => f.PickRandom(validMethods));
            this.mockServerProcessor = new MockServerProcessor(new AssertFactory(), new RuleMatcher(), new TemplateContext());
        }