private static void SendReadAndRelease(IRedisTaskFunnel redisTaskFunnel, string parentPipeName, string childPipeName)
        {
            var sent = redisTaskFunnel.TrySendMessage(parentPipeName, childPipeName, new byte[1] {
                (byte)'a'
            }, Int32.MaxValue,
                                                      TimeSpan.FromMinutes(1));

            sent.sent.Should().BeTrue();
            sent.clients.Should().BeFalse();

            var read = redisTaskFunnel.TryReadMessage <byte[]>(true, parentPipeName, childPipeName, TimeSpan.FromSeconds(1));

            read.Should().NotBeNull();
            read.LockValue.Should().NotBeNull();
            read.Value.Should().NotBeNull();

            //try to release the lock without the correct key
            var redisPipeValue = new RedisPipeValue <byte[]>(PipeInfo.Create(parentPipeName, childPipeName), "", Guid.NewGuid().ToString(), true);
            var badExtend      = redisTaskFunnel.LockExtend(redisPipeValue, TimeSpan.FromSeconds(1));

            badExtend.Should().BeFalse();
            Action badRelease = () => redisTaskFunnel.LockRelease(redisPipeValue, true);

            badRelease.Should().Throw <ApplicationException>();

            var extended = redisTaskFunnel.LockExtend(read, TimeSpan.FromSeconds(1));

            extended.Should().BeTrue();

            var released = redisTaskFunnel.LockRelease(read, true);

            released.Should().BeTrue();
        }
Exemplo n.º 2
0
        public TaskExecutorTests(ITestOutputHelper output)
        {
            _output = output;
            var serviceCollection = RedisTaskFunnelsTests.CreateServiceCollection();
            var taskExecutor      = Substitute.For <ITaskExecutor>();

            taskExecutor.When(a => a.Execute(Arg.Any <PipeInfo>(), Arg.Any <RedisPipeValue>()))
            .Do(x =>
            {
                while (Block)
                {
                    Thread.Sleep(100);
                }

                Interlocked.Increment(ref _tasksCalled);
            });
            serviceCollection.AddSingleton(new Dictionary <string, ITaskExecutor> {
                { ParentPipeName, taskExecutor }
            });
            var serviceProvider = serviceCollection.BuildServiceProvider();

            _redisTaskFunnel = serviceProvider.GetRequiredService <IRedisTaskFunnel>();
            var taskReader = serviceProvider.GetRequiredService <ITaskReader>();

            _cancellationTokenSource = new CancellationTokenSource();
            _taskReaderTask          = Task.Factory.StartNew(() => taskReader.Start(TimeSpan.FromMinutes(2), _cancellationTokenSource.Token));
        }
        /// <summary>
        /// Sets up the executors by connecting a subscriber for each message queue.  The redis implementation
        /// will analyze the existing 'pipes' and begin processing any existing messages.
        /// </summary>
        public AtLeastOnceTaskReader(ILogger <AtLeastOnceTaskReader> logger, IRedisTaskFunnel taskFunnel, Dictionary <string, ITaskExecutor> taskExecutors, int maxThreads)
        {
            if (taskExecutors == null || taskExecutors.Count <= 0)
            {
                throw new ArgumentException($"{nameof(taskExecutors)} must contain values");
            }

            _logger        = logger;
            _taskFunnel    = taskFunnel;
            _taskExecutors = taskExecutors;
            _maxThreads    = maxThreads;
        }
Exemplo n.º 4
0
        private static void SendReadAndRelease(IRedisTaskFunnel redisTaskFunnel, string parentPipeName, string childPipeName)
        {
            //send a message
            var messageBody = "body";
            var sent        = redisTaskFunnel.TrySendMessage(parentPipeName, childPipeName, messageBody, Int32.MaxValue,
                                                             TimeSpan.FromMinutes(1));

            sent.sent.Should().BeTrue();
            //sent.clients.Should().BeFalse();

            //read the batch
            var read = redisTaskFunnel.TryReadMessageBatch(true, PipeInfo.Create(parentPipeName, childPipeName), TimeSpan.FromSeconds(1), 1);

            read.Should().NotBeNull();
            read.HoldingList.Should().NotBeNull();
            read.RedisValues.Should().NotBeEmpty();
            read.RedisValues.First().HasValue.Should().BeTrue();
            var actualRedisPipeValue = read.RedisPipeValues.First();

            actualRedisPipeValue.ValueString.Should().Be(messageBody);

            //try to release the lock without the wrong holdingListName
            var redisPipeValue = new RedisPipeValue(PipeInfo.Create(parentPipeName, childPipeName), "body", Guid.NewGuid().ToString(), true);
            var badExtend      = redisTaskFunnel.RetainHoldingList(redisPipeValue, TimeSpan.FromSeconds(1));

            badExtend.Should().BeFalse();
            redisTaskFunnel.AfterExecute(redisPipeValue, true);

            //retain with the correct name
            var extended = redisTaskFunnel.RetainHoldingList(read, TimeSpan.FromSeconds(1));

            extended.Should().BeTrue();

            //complete the message and the batch
            redisTaskFunnel.AfterExecute(actualRedisPipeValue, true);
            redisTaskFunnel.AfterExecuteBatch(read);

            //now check the holding list doesn't exist any more.
            extended = redisTaskFunnel.RetainHoldingList(read, TimeSpan.FromSeconds(1));
            extended.Should().BeFalse();
        }
Exemplo n.º 5
0
 public RedisDeliveryScheme(IRedisTaskFunnel redisTaskFunnel)
 {
     _redisTaskFunnel = redisTaskFunnel;
 }