public void CancellationWorks() { var gurobiEnvironment = new GRBEnv(); var runnerConfiguration = new GurobiRunnerConfiguration.GurobiRunnerConfigBuilder().Build(TimeSpan.FromSeconds(30)); var timer = new Stopwatch(); timer.Start(); // Run Gurobi. var gurobiRunner = new GurobiRunner(gurobiEnvironment, runnerConfiguration); var runner = gurobiRunner.Run( new InstanceSeedFile(GurobiRunnerTests.PathToTestInstance, GurobiRunnerTests.TestInstanceSeed), this._cancellationTokenSource.Token); // Cancel task and expect it to be cancelled. Thread.Sleep(100); this._cancellationTokenSource.Cancel(); runner.Wait(); timer.Stop(); var result = runner.Result; result.IsCancelled.ShouldBeTrue(); timer.Elapsed.ShouldBeLessThan(TimeSpan.FromSeconds(1)); gurobiEnvironment.Dispose(); }
public void CancellationWorks() { var gurobiEnvironment = new GRBEnv(); var runnerConfiguration = new GurobiRunnerConfiguration.GurobiRunnerConfigBuilder().Build(TimeSpan.FromSeconds(1)); var tunerConfiguration = new AlgorithmTunerConfiguration(); // Note, that this cancellation token source is never used in GurobiRunner.Run(). var cancellationTokenSource = new CancellationTokenSource(500); var timer = new Stopwatch(); timer.Start(); var gurobiRunner = new GurobiRunner(gurobiEnvironment, runnerConfiguration, tunerConfiguration); var runner = gurobiRunner.Run( new InstanceSeedFile(GurobiRunnerTests.PathToTestInstance, GurobiRunnerTests.TestInstanceSeed), cancellationTokenSource.Token); runner.Wait(); timer.Stop(); var result = runner.Result; result.IsCancelled.ShouldBeTrue(); timer.Elapsed.ShouldBeGreaterThan(TimeSpan.FromMilliseconds(1000)); timer.Elapsed.ShouldBeLessThan(TimeSpan.FromMilliseconds(1900)); gurobiEnvironment.Dispose(); }