public void TestError() { var tokenSource = new CancellationTokenSource(); const int initialState = 42; const int totalIterations = 10; var ibw = RecursiveBackgroundWorker.Make ( initialState, (i, c) => { Thread.Sleep(10); return(IterationResult.Make(i + 1, ProgressStatus.Error)); }, totalIterations, tokenSource ); var nextResult = initialState; ibw.OnIterationResult.Subscribe( r => { nextResult = r.Data; }); ibw.Start(); Thread.Sleep(100); Assert.AreEqual(ibw.CurrentState, nextResult); Assert.AreEqual(ibw.CurrentState, initialState); Assert.AreEqual(ibw.CurrentIteration, 0); }
public async Task OnRunAsync(CancellationTokenSource cancellationTokenSource) { Busy = true; _cancellationTokenSource = cancellationTokenSource; var rando = Rando.Fast(ScpParamsVm.Seed); var rbw = RecursiveBackgroundWorker.Make ( initialState: ScpWorkflow.Make ( sorterLayer: SorterLayer.Make( sorterGenomes: _sorterGenomeEvals.Select(e => e.Value.SorterGenome), generation: ScpParamsVm.CurrentGeneration ), scpParams: ScpParamsVm.GetParams, generation: ScpParamsVm.CurrentGeneration ), recursion: (i, c) => { var nextStep = i; while (true) { if (_cancellationTokenSource.IsCancellationRequested) { return(IterationResult.Make <IScpWorkflow>(null, ProgressStatus.StepIncomplete)); } nextStep = nextStep.Step(rando.NextInt()); if (nextStep.CompWorkflowState == CompWorkflowState.ReproGenomes) { return(IterationResult.Make(nextStep, ProgressStatus.StepComplete)); } } }, totalIterations: ScpParamsVm.TotalGenerations - ScpParamsVm.CurrentGeneration, cancellationTokenSource: _cancellationTokenSource ); rbw.OnIterationResult.Subscribe(UpdateSorterTuneResults); await rbw.Start(); Busy = false; }
public void TestCtor() { var tokenSource = new CancellationTokenSource(); const int initialState = 42; const int totalIterations = 10; var ibw = RecursiveBackgroundWorker.Make ( initialState, (i, c) => IterationResult.Make(i + 1, ProgressStatus.StepComplete), totalIterations, tokenSource ); Assert.AreEqual(ibw.CurrentState, initialState); Assert.AreEqual(ibw.CurrentIteration, 0); Assert.AreEqual(ibw.TotalIterations, totalIterations); }
public void TestUpdate() { var tokenSource = new CancellationTokenSource(); const int initialState = 42; const int totalIterations = 10; var ibw = RecursiveBackgroundWorker.Make ( initialState, (i, c) => IterationResult.Make(i + 1, ProgressStatus.StepComplete), totalIterations, tokenSource ); var nextResult = 0; ibw.OnIterationResult.Subscribe(r => nextResult = r.Data); ibw.Start(); Thread.Sleep(100); Assert.AreEqual(ibw.CurrentState, nextResult); Assert.AreEqual(ibw.CurrentIteration, totalIterations); }