public async Task Test4() { IServiceCollection sc = new ServiceCollection() .AddExtensions(); var sp = sc.BuildServiceProvider(); Func <IServiceProvider, string, string, string, Task> proc = async(sp, entity, state, name) => { await Task.Delay(1000); }; DefaultParallelQueueConsumer <string, string> consumer = new DefaultParallelQueueConsumer <string, string>( new ParallelQueueConsumerOptions <string, string>() { ExecutorCount = 2, ExecutorQueueCapacity = 2, ExecuteDelegate = proc }, sp, "test", "state", new Microsoft.Extensions.Logging.LoggerFactory() ); await consumer.StartAsync(); System.Threading.CancellationTokenSource cts = new System.Threading.CancellationTokenSource(); List <long> list = new List <long>(); _ = Task.Run(() => { for (int i = 0; i < 8; i++) { Stopwatch sw = new Stopwatch(); sw.Start(); // 0、1、2、3、4、5正常,6,7等待1秒(并行) consumer.TryAdd(i.ToString(), TimeSpan.FromSeconds(10)); sw.Stop(); list.Add(sw.ElapsedMilliseconds); } cts.Cancel(); }); cts.Token.WaitHandle.WaitOne(); await consumer.StopAsync(); var loadInfo = consumer.GetDetailLoadInfo().ToList(); Assert.Equal(8, consumer.ExecutedCount); Assert.Equal(4, loadInfo[1].Counter); // Assert.True(list.Sum() >= 1000 && list.Sum() < 1500); }
public async Task Test1() { IServiceCollection sc = new ServiceCollection() .AddExtensions(); var sp = sc.BuildServiceProvider(); Func <IServiceProvider, string, string, string, Task> proc = async(sp, entity, state, name) => { await Task.Delay(100); }; DefaultParallelQueueConsumer <string, string> consumer = new DefaultParallelQueueConsumer <string, string>( new ParallelQueueConsumerOptions <string, string>() { ExecutorCount = 5, ExecutorQueueCapacity = 1, ExecuteDelegate = proc }, sp, "test", "state", new Microsoft.Extensions.Logging.LoggerFactory() ); await consumer.StartAsync(); System.Threading.CancellationTokenSource cts = new System.Threading.CancellationTokenSource(); _ = Task.Run(() => { for (int i = 0; i < 10; i++) { consumer.TryAdd(i.ToString(), TimeSpan.FromSeconds(10)); } cts.Cancel(); }); cts.Token.WaitHandle.WaitOne(); await consumer.StopAsync(); var loadInfo = consumer.GetDetailLoadInfo().ToList(); Assert.Equal(10, consumer.ExecutedCount); Assert.True(loadInfo.Count(x => x.Counter > 0) > 1); }