public void ThrowIfCancellationRequested_ThrowsOnTrueValue() { var token = new JobCancellationToken(true); Assert.Throws <OperationCanceledException>( () => token.ThrowIfCancellationRequested()); }
public void ThrowIfCancellationRequested_ThrowsOnTrueValue() { var token = new JobCancellationToken(true); Assert.Throws<OperationCanceledException>( () => token.ThrowIfCancellationRequested()); }
public void ThrowIfCancellationRequested_DoesNotThrowOnFalseValue() { var token = new JobCancellationToken(false); // Does not throw token.ThrowIfCancellationRequested(); }
/// <inheritdoc /> public Water.AutoWateringStatus TurnOnAutoWater() { try { if (string.IsNullOrEmpty(Enums.JobId)) { if (JobStorage.Current.GetMonitoringApi().JobDetails(Enums.JobId ?? "") == null) { var token = new JobCancellationToken(false); Enums.JobId = BackgroundJob.Enqueue(() => RunAutoStart(token)); return(Water.AutoWateringStatus.Running); } return(Water.AutoWateringStatus.AlreadyRunning); } return(Water.AutoWateringStatus.AlreadyRunning); } catch (Exception e) { Console.WriteLine(e); return(Water.AutoWateringStatus.Stopped); } }
public void ShutdownToken_IsInCanceledState_WhenPassingTrueValue() { var token = new JobCancellationToken(true); Assert.True(token.ShutdownToken.IsCancellationRequested); }
public override void LoadDictionaryPart() { XmlReader reader = XmlReader.Create(LoadStream); StringPointerBuilder pinyinBuilder = new StringPointerBuilder(); StringPointerBuilder translationBuilder = new StringPointerBuilder(); StringPointerBuilder tagBuilder = new StringPointerBuilder(); List <string> thumbPinyinBuilder = new List <string>(); List <string> thumbTranslationBuilder = new List <string>(); while (reader.ReadToFollowing("Word")) { JobCancellationToken.ThrowIfCancellationRequested(); string hanzi = string.Empty; string traditional = string.Empty; string radicals = string.Empty; string link = string.Empty; string thumbPinyin = string.Empty; string thumbTranslation = string.Empty; reader.ReadToFollowing("Hanzi"); hanzi = reader.ReadElementContentAsString(); Cursor cursor = Cursor.Traditional; int wordDepth = reader.Depth; while (reader.Read() && reader.Depth >= wordDepth) { if (reader.NodeType == XmlNodeType.Element) { string name = reader.Name; if (cursor <= Cursor.Traditional && name == "Traditional") { traditional = reader.ReadElementContentAsString(); cursor = Cursor.Traditional + 1; } else if (cursor <= Cursor.Radicals && name == "Radicals") { radicals = reader.ReadElementContentAsString(); cursor = Cursor.Radicals + 1; } else if (cursor <= Cursor.Link && name == "Link") { link = reader.ReadElementContentAsString(); cursor = Cursor.Link + 1; } else if (cursor <= Cursor.ThumbPinyin && name == "ThumbPinyin") { thumbPinyin = reader.ReadElementContentAsString(); cursor = Cursor.ThumbPinyin + 1; } else if (cursor <= Cursor.ThumbTranslation && name == "ThumbTranslation") { thumbTranslation = reader.ReadElementContentAsString(); cursor = Cursor.ThumbTranslation + 1; } else if (cursor <= Cursor.Meaning && name == "Meaning") { int meaningDepth = reader.Depth; cursor = Cursor.ThumbPinyin; while (reader.Read() && reader.Depth > meaningDepth) { if (reader.NodeType == XmlNodeType.Element) { if (cursor <= Cursor.MeaningPinyin && reader.Name == "Pinyin") { string pinyin = reader.ReadElementContentAsString(); pinyinBuilder.Add(pinyin); thumbPinyinBuilder.Add(pinyin); } else { cursor = Cursor.MeaningTranslation; string translation = reader.ReadElementContentAsString(); translationBuilder.Add(translation); if (MeaningBuilder.MeaningMemory.Count == MeaningBuilder.MeaningStart) { thumbTranslationBuilder.Add(translation); } } } } MeaningBuilder.Add(pinyinBuilder, translationBuilder); pinyinBuilder.Clear(); translationBuilder.Clear(); cursor = Cursor.Meaning; } else// if (cursor <= Cursor.Tag && name == "Tag") { // tag is the last element, we do not need to check cursor = Cursor.Tag; tagBuilder.Add(reader.ReadElementContentAsString()); } } } if (thumbPinyin == string.Empty) { thumbPinyin = BuildThumb(thumbPinyinBuilder); } if (thumbTranslation == string.Empty) { thumbTranslation = BuildThumb(thumbTranslationBuilder); } ulong pinyinMask = 0; foreach (string pinyin in thumbPinyinBuilder) { pinyinMask |= pinyin.LetterMask(); } Words.Add(new Word(StringBuilder, hanzi, traditional, thumbPinyin, thumbTranslation, radicals, link, MeaningBuilder, tagBuilder, pinyinMask)); tagBuilder.Clear(); MeaningBuilder.Clear(); thumbPinyinBuilder.Clear(); thumbTranslationBuilder.Clear(); } reader.Dispose(); }
public void ThrowIfCancellationRequested_DoesNotThrowOnFalseValue() { var token = new JobCancellationToken(false); Assert.DoesNotThrow(token.ThrowIfCancellationRequested); }
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); }); }
public static void Start() { IJobCancellationToken token = new JobCancellationToken(true); BackgroundJob.Enqueue <TestJobRunner>(m => m.Run(token)); }