public void DefaultState()
        {
            Func <IServiceProvider, string, object, string, Task> proc1 = async(sp, entity, state, name) =>
            {
                await Task.Delay(100);
            };
            Func <IServiceProvider, string, object, string, Task> testProc1 = async(sp, entity, state, name) =>
            {
                await Task.Delay(100);
            };
            Func <IServiceProvider, int, object, string, Task> proc2 = async(sp, entity, state, name) =>
            {
                await Task.Delay(100);
            };
            IServiceCollection sc = new ServiceCollection()
                                    .AddLogging();

            sc.AddParallelQueue <string>().ConfigConsumer(proc1);
            sc.AddParallelQueue <string>("test").ConfigConsumer(testProc1);
            sc.AddParallelQueue <string>("test").ConfigConsumer(options =>
            {
                options.ExecutorQueueCapacity = 10;
                options.ExecutorCount         = 10;
            });
            sc.AddParallelQueue <string>("test2").ConfigConsumer(10, 10, proc1);
            sc.AddParallelQueue <string>("test2").ConfigConsumer(options =>
            {
                // 覆盖
                options.ExecutorCount = 8;
            });
            sc.AddParallelQueue <int>().ConfigConsumer(proc2);

            var sp = sc.BuildServiceProvider();

            IParallelQueueConsumerFactory factory = sp.GetRequiredService <IParallelQueueConsumerFactory>();
            var c1 = factory.CreateConsumer <string>() as DefaultParallelQueueConsumer <string, object>;
            var c2 = factory.CreateConsumer <int>() as DefaultParallelQueueConsumer <int, object>;

            Assert.Equal(proc1, c1.Options.ExecuteDelegate);
            Assert.Equal(proc2, c2.Options.ExecuteDelegate);

            Assert.Equal(5, c1.Options.ExecutorCount);
            Assert.Equal(1, c1.Options.ExecutorQueueCapacity);

            var c11 = factory.CreateConsumer <string>();

            Assert.Equal(c1, c11);

            var c1_name = factory.CreateConsumer <string>("test") as DefaultParallelQueueConsumer <string, object>;

            Assert.Equal(testProc1, c1_name.Options.ExecuteDelegate);
            Assert.Equal(10, c1_name.Options.ExecutorCount);
            Assert.Equal(10, c1_name.Options.ExecutorQueueCapacity);

            var test2 = factory.CreateConsumer <string>("test2") as DefaultParallelQueueConsumer <string, object>;

            Assert.Equal(proc1, test2.Options.ExecuteDelegate);
            Assert.Equal(8, test2.Options.ExecutorCount);
            Assert.Equal(10, test2.Options.ExecutorQueueCapacity);
        }
        public async Task StartAsync(CancellationToken cancellationToken)
        {
            if (_producer == null)
            {
                _producer = _producerFactory.CreateProducer <TEntity>(_queueName);
            }
            if (_consumer == null)
            {
                _consumer = _consumerFactory.CreateConsumer <TEntity, TState>(_queueName, _state);
            }

            await _consumer.StartAsync();

            _stopToken = new CancellationTokenSource();
            _          = Task.Run(async() =>
            {
                await QueueReciver();
            });
        }
        public void Invalid()
        {
            Func <IServiceProvider, string, string, string, Task> proc1 = async(sp, entity, state, name) =>
            {
                await Task.Delay(100);
            };

            IServiceCollection sc = new ServiceCollection()
                                    .AddLogging();

            sc.AddParallelQueue <string, string>().ConfigConsumer(proc1);


            var sp = sc.BuildServiceProvider();

            IParallelQueueConsumerFactory factory = sp.GetRequiredService <IParallelQueueConsumerFactory>();

            Assert.Throws <InvalidOperationException>(() =>
            {
                var abc = factory.CreateConsumer <int, int>(0);
            });
        }
Ejemplo n.º 4
0
 public static IParallelQueueConsumer <TEntity, object> CreateConsumer <TEntity>(this IParallelQueueConsumerFactory factory, string name)
 {
     return(factory.CreateConsumer <TEntity, object>(name, null));
 }
Ejemplo n.º 5
0
 public static IParallelQueueConsumer <TEntity, TState> CreateConsumer <TEntity, TState>(this IParallelQueueConsumerFactory factory, TState state)
 {
     return(factory.CreateConsumer <TEntity, TState>(string.Empty, state));
 }