Beispiel #1
0
        SetupHangfire(
            Action <ContainerBuilder> configureContainer = null,
            Action <Mock <Crawler>, Mock <IGrainFactory> > configureCrawlerMock = null,
            Action <Mock <ICrawlerStrategy> > configureStrategyMock             = null)
        {
            var jobStorage = new MemoryStorage(
                new MemoryStorageOptions
            {
                FetchNextJobTimeout = TimeSpan.FromMilliseconds(200)
            });
            var containerBuilder = new ContainerBuilder();
            var stateChanger     = new BackgroundJobStateChanger(JobFilterProviders.Providers);
            var jobFactory       = new BackgroundJobFactory(JobFilterProviders.Providers);
            var jobClient        = new BackgroundJobClient(
                jobStorage,
                jobFactory,
                stateChanger);

            var strategy = new Mock <ICrawlerStrategy>();

            if (configureStrategyMock != null)
            {
                configureStrategyMock(strategy);
            }
            else
            {
                strategy.Setup(s => s.ProcessRequestAsync(
                                   It.IsAny <CrawlRequestBase>(),
                                   It.IsAny <CancellationToken>()))
                .Returns(Task.CompletedTask)
                .Verifiable();
            }

            var strategyFactory = new Mock <ICrawlerStrategyFactory>();

            strategyFactory.Setup(f => f.Create(
                                      It.IsAny <CrawlRequestBase>()))
            .Returns(() => strategy.Object);

            var grainFactoryMock = new Mock <IGrainFactory>();

            grainFactoryMock.Setup(gf => gf.GetGrain <IProcessCrawlRequests>(
                                       It.IsAny <string>(),
                                       It.IsAny <string>()))
            .Returns <string, string>((grainId, _) => createCrawler(grainId));

            Crawler createCrawler(string id)
            {
                var logger      = Mock.Of <ILogger <Crawler> >();
                var crawlerMock = new Mock <Crawler>(
                    MockBehavior.Loose,
                    jobClient,
                    logger,
                    new Lazy <ICrawlerStrategyFactory>(strategyFactory.Object));

                if (configureCrawlerMock != null)
                {
                    configureCrawlerMock(crawlerMock, grainFactoryMock);
                }
                else
                {
                    crawlerMock.Setup(c => c.PersistState(It.IsAny <CrawlStateSnapshot>()))
                    .Returns(Task.CompletedTask);

                    crawlerMock.Setup(c => c.GrainFactory)
                    .Returns(() => grainFactoryMock.Object);

                    crawlerMock.Setup(c => c.GrainFactory.GetGrain <IProcessCrawlRequests>(
                                          It.IsAny <string>(),
                                          It.IsAny <string>()))
                    .Returns <string, string>((grainId, _) => createCrawler(grainId));

                    crawlerMock.Setup(c => c.Process(It.IsAny <CrawlRequestBase>()))
                    .CallBase();
                }

                return(crawlerMock.Object);
            }

            if (configureContainer != null)
            {
                configureContainer(containerBuilder);
            }
            else
            {
                containerBuilder.RegisterType <CrawlJobPerformer>()
                .InstancePerBackgroundJob();
                containerBuilder.Register(ctx => grainFactoryMock.Object)
                .As <IGrainFactory>();
            }

            var container    = containerBuilder.Build();
            var activator    = new AutofacJobActivator(container);
            var jobPerformer = new BackgroundJobPerformer(JobFilterProviders.Providers, activator);

            Func <Job, CreateContext> createContext = (Job job) =>
                                                      new CreateContext(
                jobStorage,
                jobStorage.GetConnection(),
                job,
                new EnqueuedState());

            Func <Job, BackgroundJob> createBackgroundJob = (Job job) =>
                                                            jobFactory.Create(createContext(job));

            return(
                () => new BackgroundJobServer(
                    new BackgroundJobServerOptions
            {
                Activator = activator,
                WorkerCount = Environment.ProcessorCount
            },
                    jobStorage,
                    Enumerable.Empty <IBackgroundProcess>(),
                    JobFilterProviders.Providers,
                    activator,
                    jobFactory,
                    jobPerformer,
                    stateChanger),
                createCrawler,
                grainFactoryMock.Object,
                strategy,
                jobClient,
                jobFactory,
                jobPerformer,
                activator,
                (Job job) => createBackgroundJob(job),
                (Job job) => createContext(job),
                (BackgroundJob job) =>
            {
                var token = new JobCancellationToken(false);
                var result = jobPerformer.Perform(
                    new PerformContext(
                        jobStorage.GetConnection(),
                        job,
                        token));

                return (result, token);
            });
        }