<commit_before>using System;
using System.Collections.Generic;
using System.Threading;
using Infura;
using Infura.EventSourcing;

namespace dokimi.core.Specs.ApplicationService
{
    public partial class ApplicationServiceSpecification : Specification
    {
        private GivenContext _given;
        private EventEntry _when;
        private readonly Expectations _expectations = new Expectations();
        private Func<Repository, EventStore, Router> _wireup;
        private Func<EventStore> _eventStoreFactory = () => new InMemoryEventStore();

        public ApplicationServiceSpecification UseEventStoreFacotry(Func<EventStore> es)
        {
            _eventStoreFactory = es;
            return this;
        }

        public void EnrichDescription(SpecInfo spec, MessageFormatter formatter)
        {
            _given.DescribeTo(spec, formatter);
            _when.DescribeTo(spec.ReportWhenStep, formatter);
            _expectations.DescribeTo(spec, formatter);
        }

        public SpecInfo Run(SpecInfo spec, MessageFormatter formatter)
        {
            //var givenSteps = new StepInfo[_given.Count];

            //for (int i = 0; i < _given.Count; i++)
            //{
            //    var given = _given[i];
            //    int i1 = i;
            //    given.DescribeTo(s =>
            //    {
            //        var stepInfo = new StepInfo(s);
            //        givenSteps[i1] = stepInfo;
            //        return spec.ReportGivenStep(stepInfo);
            //    }, formatter);
            //}

            bool givenDescribed=false, whenDescribed = false;

            var prepareStep = new StepInfo("Setup completed");
            spec.ReportGivenStep(prepareStep);

            //_when.DescribeTo(spec.ReportWhenStep, formatter);

            var token = new CancellationTokenSource();

            var events = new List<object>();
            

            try
            {
                //wireup
                var eventStore = _eventStoreFactory();
                eventStore.AdjustDispatcher(x => events.Add(x.EventData));
                var repo = new EventStoreRepository(eventStore);
                var router = _wireup(repo, eventStore);

                //if (_given.Count == 0)
                //{
                //    var step = new StepInfo("No history");
                //    spec.ReportGivenStep(step);
                //    step.Pass();
                //}
                //else for (int i = 0; i < _given.Count; i++)
                //{
                //    var g = _given[i];
                //    g.RunTo(router);
                //    givenSteps[i].Pass();
                //}

                _given.StoreTo(eventStore, spec.ReportGivenStep, formatter);
                givenDescribed = true;

                events.Clear();
                prepareStep.Pass();

                //run when
                _when.RunTo(router);
                _when.DescribeTo(spec.ReportWhenStep, formatter);
                spec.When.Pass();
                whenDescribed = true;

// ReSharper disable once SuspiciousTypeConversion.Global
                var store = eventStore as IDisposable;
                if(store != null)
                    store.Dispose();
            }
            catch
            {<commit_msg>better event store factory override in specs...now supports cancellation tokens<commit_after>using System;
using System.Collections.Generic;
using System.Threading;
using Infura;
using Infura.EventSourcing;

namespace dokimi.core.Specs.ApplicationService
{
    public partial class ApplicationServiceSpecification : Specification
    {
        private GivenContext _given;
        private EventEntry _when;
        private readonly Expectations _expectations = new Expectations();
        private Func<Repository, EventStore, Router> _wireup;
        private Func<CancellationToken, EventStore> _eventStoreFactory = _ => new InMemoryEventStore();

        public ApplicationServiceSpecification UseEventStoreFacotry(Func<CancellationToken, EventStore> es)
        {
            _eventStoreFactory = es;
            return this;
        }

        public void EnrichDescription(SpecInfo spec, MessageFormatter formatter)
        {
            _given.DescribeTo(spec, formatter);
            _when.DescribeTo(spec.ReportWhenStep, formatter);
            _expectations.DescribeTo(spec, formatter);
        }

        public SpecInfo Run(SpecInfo spec, MessageFormatter formatter)
        {
            //var givenSteps = new StepInfo[_given.Count];

            //for (int i = 0; i < _given.Count; i++)
            //{
            //    var given = _given[i];
            //    int i1 = i;
            //    given.DescribeTo(s =>
            //    {
            //        var stepInfo = new StepInfo(s);
            //        givenSteps[i1] = stepInfo;
            //        return spec.ReportGivenStep(stepInfo);
            //    }, formatter);
            //}

            bool givenDescribed=false, whenDescribed = false;

            var prepareStep = new StepInfo("Setup completed");
            spec.ReportGivenStep(prepareStep);

            var token = new CancellationTokenSource();

            var events = new List<object>();
            

            try
            {
                //wireup
                var eventStore = _eventStoreFactory(token.Token);
                eventStore.AdjustDispatcher(x => events.Add(x.EventData));
                var repo = new EventStoreRepository(eventStore);
                var router = _wireup(repo, eventStore);
                
                _given.StoreTo(eventStore, spec.ReportGivenStep, formatter);
                givenDescribed = true;

                events.Clear();
                prepareStep.Pass();

                //run when
                _when.RunTo(router);
                _when.DescribeTo(spec.ReportWhenStep, formatter);
                spec.When.Pass();
                whenDescribed = true;

// ReSharper disable once SuspiciousTypeConversion.Global
                var store = eventStore as IDisposable;
                if(store != null)
                    store.Dispose();
            }
            catch
            {
{'fraction_non_alphanumeric': 0.09107056353948006, 'fraction_numerical': 0.0012917810431131924, 'mean_word_length': 1.578684429641965, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2374698', 'n_tokens_mistral': 1601, 'n_tokens_neox': 1516, 'n_words': 342}