public void it_should_pass_when_client_markes_result_as_failure_with_different_id2() { var service = new ConcurrentExecutorServiceLib.ConcurrentExecutorService(); var result = service.ExecuteAsync <object>("1", () => Task.FromResult(new object()), (r) => false).Result; Assert.True(result.Succeeded); var result2 = service.ExecuteAsync <object>("2", () => Task.FromResult(new object())).Result; Assert.True(result2.Succeeded); }
public void it_should_fail_when_client_markes_result_as_failure() { var service = new ConcurrentExecutorServiceLib.ConcurrentExecutorService(); var result = service.ExecuteAsync <object>("1", () => Task.FromResult(new object()), (r) => true).Result; Assert.False(result.Succeeded); SimulateNormalClient(); var result2 = service.ExecuteAsync <object>("1", () => Task.FromResult(new object())).Result; Assert.True(result2.Succeeded); }
public void it_should_block_duplicate_work_id() { var service = new ConcurrentExecutorServiceLib.ConcurrentExecutorService(); var result = service.ExecuteAsync <object>("1", () => Task.FromResult(new object())).Result; Assert.True(result.Succeeded); SimulateNormalClient(); var result2 = service.ExecuteAsync <object>("1", () => Task.FromResult(new object())).Result; Assert.False(result2.Succeeded); }
public void test_max_execution_time_not_exceeded3() { var service = new ConcurrentExecutorServiceLib.ConcurrentExecutorService(); var result = service.ExecuteAsync <object>("1", async() => { await Task.Delay(TimeSpan.FromSeconds(1)); return(Task.FromResult(new object())); }, (r) => false, true, TimeSpan.FromSeconds(3)).Result; Assert.True(result.Succeeded); }
static void Main(string[] args) { var service = new ConcurrentExecutorServiceLib.ConcurrentExecutorService(TimeSpan.FromSeconds(3)); var result = service.ExecuteAsync <object>(() => { Console.WriteLine("Done"); return(Task.FromResult(new object())); }, Guid.NewGuid().ToString()).Result; Console.ReadLine(); }
public void test2() { var service = new ConcurrentExecutorServiceLib.ConcurrentExecutorService(); var t = (service.ExecuteAsync("1", () => { return(Task.FromResult(new object())); }, (finalResult) => { return(false); }, true, TimeSpan.FromSeconds(5))).Result.Result; Assert.NotNull(t); }
public void test_max_execution_time_exceeded_should_return_correct_value_when_called_again() { var now = DateTime.UtcNow; var service = new ConcurrentExecutorServiceLib.ConcurrentExecutorService(); var result = service.ExecuteAsync <object>("1", async() => { await Task.Delay(TimeSpan.FromSeconds(2)); return(await Task.FromResult(now)); }, (r) => false, true, TimeSpan.FromSeconds(1)).Result; Assert.False(result.Succeeded); Assert.NotEqual(now, result.Result); SimulateNormalClient(); SimulateNormalClient(); result = service.ExecuteAsync <object>("1", async() => { await Task.Delay(TimeSpan.FromSeconds(1)); return(await Task.FromResult(DateTime.UtcNow)); }, (r) => false, true, TimeSpan.FromSeconds(3)).Result; Assert.False(result.Succeeded); Assert.Equal(now, result.Result); }
public void it_should_run_many_operations_sequentially() { const int numberOfBaskets = 100; const int numberOfPurchaseFromOneBasketCount = 10; var maxExecutionTimePerAskCall = TimeSpan.FromSeconds(5); var service = new ConcurrentExecutorServiceLib.ConcurrentExecutorService(maxExecutionTimePerAskCall); TestHelper.TestOperationExecution(numberOfBaskets, numberOfPurchaseFromOneBasketCount, (basketIds, purchaseService) => { Parallel.ForEach(basketIds, basketId => { var result = service.ExecuteAsync(basketId, () => purchaseService.RunPurchaseServiceAsync(basketId)).Result; }); }, maxExecutionTimePerAskCall); }
public void test3() { var service = new ConcurrentExecutorServiceLib.ConcurrentExecutorService(); var t = (service.ExecuteAsync("1", DateTime.UtcNow, (d) => { return(Task.FromResult(new { time = d })); }, (finalResult) => { return(false); }, true, TimeSpan.FromSeconds(5), (executionResult) => { return(executionResult.Result); })).Result.Result; Assert.NotNull(t); var history = service.GetWorkHistoryAsync().Result; Assert.True(history.Result.WorkHistory.First().WorkerStatus.IsCompleted); }
public void it_should_run_many_operations_fast() { const int numberOfRequests = 2000; var maxExecutionTimePerAskCall = TimeSpan.FromSeconds(3); const int maxTotalExecutionTimeMs = 4000; var service = new ConcurrentExecutorServiceLib.ConcurrentExecutorService(maxExecutionTimePerAskCall); var watch = Stopwatch.StartNew(); Parallel.ForEach(Enumerable.Range(0, numberOfRequests), basket => { var result = service.ExecuteAsync <object>(basket.ToString(), () => Task.FromResult(new object())).Result; }); watch.Stop(); var elapsedMs = watch.ElapsedMilliseconds; Assert.True(elapsedMs < maxTotalExecutionTimeMs, $"Test took {elapsedMs} ms which is more than {maxTotalExecutionTimeMs}"); }