public void StopConsumingShouldNotLogWarnWhenAlreadyClosedConnectionTest()
        {
            var model    = new Mock <IModel>();
            var consumer = new Mock <IConsumer>();
            var actor    = new Mock <IActorRef>();
            var logger   = new Mock <ILog>();

            consumer.SetupGet(c => c.Id).Returns("Consumer1Id");

            var test = new StreamSubscriber(model.Object, consumer.Object, actor.Object);

            var loggerField = test.GetType().GetField("_logger", BindingFlags.Instance | BindingFlags.NonPublic);

            loggerField.SetValue(test, logger.Object);

            model.Setup(x => x.BasicConsume("test", true, consumer.Object.Id, false, false, null, test)).Callback(() => { });
            test.StartConsuming("test");

            model.SetupGet(m => m.IsClosed).Returns(true);
            model.Setup(x => x.BasicCancel(consumer.Object.Id)).Callback(() =>
            {
                throw new AlreadyClosedException(
                    new ShutdownEventArgs(ShutdownInitiator.Application, 0, "Already closed"));
            });

            test.StopConsuming();

            logger.Verify(
                l => l.Warn(It.Is <string>(
                                msg => msg.StartsWith($"Connection already closed for consumerId={consumer.Object.Id}"))),
                Times.Never, "Connection already closed warning should not be logged.");
        }
Esempio n. 2
0
        /// <summary>
        /// Create a reference to a grain that we expect to support the stream consumer extension.
        /// </summary>
        /// <param name="grainFactory">The grain factory used to get consumer references.</param>
        /// <param name="streamId">The stream ID to use for the grain ID construction.</param>
        /// <param name="streamSubscriber">The GrainBindings for the grain to create</param>
        /// <returns></returns>
        private IStreamConsumerExtension MakeConsumerReference(
            IInternalGrainFactory grainFactory,
            InternalStreamId streamId,
            StreamSubscriber streamSubscriber)
        {
            var grainId = streamSubscriber.GetGrainId(streamId);

            return(grainFactory.GetGrain <IStreamConsumerExtension>(grainId));
        }
        /// <summary>
        /// Create a reference to a grain that we expect to support the stream consumer extension.
        /// </summary>
        /// <param name="grainFactory">The grain factory used to get consumer references.</param>
        /// <param name="streamId">The stream ID to use for the grain ID construction.</param>
        /// <param name="subscriber">The subscriber prototype.</param>
        /// <returns></returns>
        private IStreamConsumerExtension MakeConsumerReference(
            IInternalGrainFactory grainFactory,
            StreamId streamId,
            StreamSubscriber subscriber)
        {
            var keyExtension = subscriber.IncludeNamespaceInGrainId ? streamId.Namespace : null;
            var grainId      = GrainId.Create(subscriber.GrainType, GrainIdKeyExtensions.CreateGuidKey(streamId.Guid, keyExtension));

            return(grainFactory.GetGrain <IStreamConsumerExtension>(grainId));
        }
Esempio n. 4
0
        /// <summary>
        /// Create a reference to a grain that we expect to support the stream consumer extension.
        /// </summary>
        /// <param name="grainFactory">The grain factory used to get consumer references.</param>
        /// <param name="streamId">The stream ID to use for the grain ID construction.</param>
        /// <param name="subscriber">The subscriber prototype.</param>
        /// <returns></returns>
        private IStreamConsumerExtension MakeConsumerReference(
            IInternalGrainFactory grainFactory,
            InternalStreamId streamId,
            StreamSubscriber subscriber)
        {
            var keyExtension = subscriber.IncludeNamespaceInGrainId ? streamId.GetNamespace() : null;
            // TODO BPETIT: CHANGE THIS TO STRING
            var grainId = GrainId.Create(subscriber.GrainType, GrainIdKeyExtensions.CreateGuidKey(Guid.Parse(streamId.GetKeyAsString()), keyExtension));

            return(grainFactory.GetGrain <IStreamConsumerExtension>(grainId));
        }
        public void DoubleStopShouldNotCauseErrorTest()
        {
            var model    = new Mock <IModel>();
            var consumer = new Mock <IConsumer>();
            var actor    = new Mock <IActorRef>();

            var test = new StreamSubscriber(model.Object, consumer.Object, actor.Object);

            model.Setup(x => x.BasicConsume("test", true, consumer.Object.Id, false, false, null, test)).Callback(() => { });
            test.StartConsuming("test");

            test.StopConsuming();
            test.StopConsuming();
        }
Esempio n. 6
0
        private Cache BuildCache(MajorMinorVersion version, ImmutableDictionary <GrainType, GrainBindings> bindings)
        {
            var newPredicates = new List <StreamSubscriberPredicate>();

            foreach (var binding in bindings.Values)
            {
                foreach (var grainBinding in binding.Bindings)
                {
                    if (!grainBinding.TryGetValue(WellKnownGrainTypeProperties.BindingTypeKey, out var type) ||
                        !string.Equals(type, WellKnownGrainTypeProperties.StreamBindingTypeValue, StringComparison.Ordinal))
                    {
                        continue;
                    }

                    if (!grainBinding.TryGetValue(WellKnownGrainTypeProperties.StreamBindingPatternKey, out var pattern))
                    {
                        throw new KeyNotFoundException(
                                  $"Stream binding for grain type {binding.GrainType} is missing a \"{WellKnownGrainTypeProperties.StreamBindingPatternKey}\" value");
                    }

                    IStreamNamespacePredicate predicate = null;
                    foreach (var provider in _providers)
                    {
                        if (provider.TryGetPredicate(pattern, out predicate))
                        {
                            break;
                        }
                    }

                    if (predicate is null)
                    {
                        throw new KeyNotFoundException(
                                  $"Could not find an {nameof(IStreamNamespacePredicate)} for the pattern \"{pattern}\"."
                                  + $" Ensure that a corresponding {nameof(IStreamNamespacePredicateProvider)} is registered");
                    }

                    if (!grainBinding.TryGetValue(WellKnownGrainTypeProperties.StreamIdMapperKey, out var mapperName))
                    {
                        throw new KeyNotFoundException(
                                  $"Stream binding for grain type {binding.GrainType} is missing a \"{WellKnownGrainTypeProperties.StreamIdMapperKey}\" value");
                    }

                    var streamIdMapper = _serviceProvider.GetServiceByName <IStreamIdMapper>(string.IsNullOrWhiteSpace(mapperName) ? DefaultStreamIdMapper.Name : mapperName);
                    var subscriber     = new StreamSubscriber(binding, streamIdMapper);
                    newPredicates.Add(new StreamSubscriberPredicate(subscriber, predicate));
                }
            }

            return(new Cache(version, newPredicates));
        }
        public void StopShouldsENDdIApatchesRemoveMessageTest()
        {
            var model    = new Mock <IModel>();
            var consumer = new Mock <IConsumer>();
            var actor    = new Mock <IActorRef>();
            var test     = new StreamSubscriber(model.Object, consumer.Object, actor.Object);

            model.Setup(x => x.BasicConsume("test", true, consumer.Object.Id, false, false, null, test)).Callback(() => { });
            //test.IsDisposed.Should().Be(false);

            test.StartConsuming("test");
            test.StopConsuming();

            //TODO check RemoveSubscriberMessage
        }
        public void StartConsumingTest()
        {
            var model    = new Mock <IModel>();
            var consumer = new Mock <IConsumer>();
            var actor    = new Mock <IActorRef>();

            var test = new StreamSubscriber(model.Object, consumer.Object, actor.Object);


            int startConsume = 0;

            model.Setup(x => x.BasicConsume("test", true, consumer.Object.Id, false, false, null, test)).Callback(() => startConsume++);
            test.StartConsuming("test");

            startConsume.Should().Be(1);
        }
Esempio n. 9
0
        private bool ProcessNewConsumer(IConsumer consumer)
        {
            Logger.LogDebug($"Method=ProcessNewConsumer triggered fixtureId={consumer?.Id} currentState={State.ToString()} connectionStatus={ConnectionStatus}");
            var queue = consumer.GetQueueDetails();

            Logger.LogDebug($"Method=ProcessNewConsumer queueName={queue?.Name}");

            if (string.IsNullOrEmpty(queue?.Name))
            {
                Logger.LogWarning($"Method=ProcessNewConsumer Invalid queue details, fixtureId={consumer?.Id}");
                return(false);
            }

            if (!IsModelValid)
            {
                Logger.LogWarning($"Method=ProcessNewConsumer AMQP model not initialized, fixtureId={consumer?.Id}");
                Self.Tell(new CreateModelMessage());
                return(false);
            }

            StreamSubscriber subscriber = null;

            try
            {
                subscriber = new StreamSubscriber(Model, consumer, Dispatcher, LoggerFactory.CreateLogger <StreamSubscriber>());
                subscriber.StartConsuming(queue.Name);
            }
            catch (Exception e)
            {
                ProcessNewConsumerErrorCounter++;
                Logger.LogWarning(
                    $"Method=ProcessNewConsumer StartConsuming errored errorsCout={ProcessNewConsumerErrorCounter} for fixtureId={consumer.Id} {e}");
                if (ProcessNewConsumerErrorCounter > NewConsumerErrorLimit)
                {
                    ProcessNewConsumerErrorHandler(e);
                }
                return(false);
            }

            Logger.LogDebug($"Method=ProcessNewConsumer successfully executed fixtureId={consumer.Id}");

            ProcessNewConsumerErrorCounter = 0;
            return(true);
        }
Esempio n. 10
0
        public void StopConsumingTest()
        {
            Mock <IConsumer> consumer = new Mock <IConsumer>();

            consumer.Setup(x => x.Id).Returns(Id1);
            consumer.Setup(x => x.GetQueueDetails()).Returns(_queueDetails);

            var model = new Mock <IModel>();

            var echoControllerActor   = ActorOfAsTestActorRef(() => new EchoControllerActor(), EchoControllerActor.ActorName);
            var updateDispatcherActor = ActorOfAsTestActorRef(() => new UpdateDispatcherActor(), UpdateDispatcherActor.ActorName);
            var streamControllerActor = ActorOfAsTestActorRef(() => new StreamControllerActor(updateDispatcherActor), StreamControllerActor.ActorName);

            var subscriber = new StreamSubscriber(model.Object, consumer.Object, updateDispatcherActor);

            subscriber.HandleBasicConsumeOk(Id1);

            AwaitAssert(() =>
            {
                echoControllerActor.UnderlyingActor.ConsumerCount.Should().Be(1);
                updateDispatcherActor.UnderlyingActor.SubscribersCount.Should().Be(1);
            },
                        TimeSpan.FromMilliseconds(ASSERT_WAIT_TIMEOUT),
                        TimeSpan.FromMilliseconds(ASSERT_EXEC_INTERVAL));

            streamControllerActor.Tell(new RemoveConsumerMessage {
                Consumer = consumer.Object
            });

            AwaitAssert(() =>
            {
                echoControllerActor.UnderlyingActor.ConsumerCount.Should().Be(0);
                updateDispatcherActor.UnderlyingActor.SubscribersCount.Should().Be(0);
            },
                        TimeSpan.FromMilliseconds(ASSERT_WAIT_TIMEOUT),
                        TimeSpan.FromMilliseconds(ASSERT_EXEC_INTERVAL));
        }
Esempio n. 11
0
        protected virtual void AddConsumerToQueue(IConsumer consumer)
        {
            if (consumer == null)
            {
                Logger.LogWarning("Method=AddConsumerToQueue Consumer is null");
                return;
            }
            Logger.LogDebug($"Method=AddConsumerToQueue triggered fixtureId={consumer.Id}");

            var queue = consumer.GetQueueDetails();

            if (string.IsNullOrEmpty(queue?.Name))
            {
                Logger.LogWarning("Method=AddConsumerToQueue Invalid queue details");
                return;
            }

            if (StreamConnection == null)
            {
                Logger.LogWarning($"Method=AddConsumerToQueue StreamConnection is null currentState={State.ToString()}");
                Self.Tell(new DisconnectedMessage {
                    IDConnection = StreamConnection?.GetHashCode()
                });
                Stash.Stash();
                return;
            }

            if (!StreamConnection.IsOpen)
            {
                Logger.LogWarning($"Method=AddConsumerToQueue StreamConnection is closed currentState={State.ToString()}");
                Self.Tell(new DisconnectedMessage {
                    IDConnection = StreamConnection?.GetHashCode()
                });
                Stash.Stash();
                return;
            }

            if (!IsModelValid)
            {
                Logger.LogWarning($"Method=ProcessNewConsumer AMQP model not initialized, fixtureId={consumer?.Id}");
                Self.Tell(new CreateModelMessage());
                return;
            }

            StreamSubscriber subscriber = null;

            try
            {
                subscriber = new StreamSubscriber(Model, consumer, Dispatcher, LoggerFactory.CreateLogger <StreamSubscriber>());
                subscriber.StartConsuming(queue.Name);
                Logger.LogDebug($"Consumer with id={consumer.Id} added to queueName={queue.Name}");
            }
            catch (Exception e)
            {
                ProcessNewConsumerErrorCounter++;
                Logger.LogWarning($"Method=AddConsumerToQueue StartConsuming errored for fixtureId={consumer.Id} {e}");
                if (ProcessNewConsumerErrorCounter > NewConsumerErrorLimit)
                {
                    throw;
                }
            }
            Logger.LogDebug($"Method=AddConsumerToQueue successfully executed fixtureId={consumer.Id}");
        }
Esempio n. 12
0
 public StreamSubscriberPredicate(StreamSubscriber subscriber, IStreamNamespacePredicate predicate)
 {
     this.Subscriber = subscriber;
     this.Predicate  = predicate;
 }