Ejemplo n.º 1
0
        public void TestImmediateAckingTracker()
        {
            var builder = new ConsumerConfigBuilder <byte[]>();

            builder.AcknowledgmentGroupTime(0);
            builder.Topic($"TestAckTracker-{Guid.NewGuid()}");
            builder.SubscriptionName($"TestAckTracker-sub-{Guid.NewGuid()}");
            var conf     = builder.ConsumerConfigurationData;
            var consumer = _client.NewConsumer(builder);
            var unack    = _system.ActorOf(UnAckedChunckedMessageIdSequenceMap.Prop());
            var tracker  = _client.ActorSystem.ActorOf(PersistentAcknowledgmentsGroupingTracker <byte[]> .Prop(unack, consumer.ConsumerActor, consumer.ConsumerActor /*dummy*/, 1, consumer.ConsumerActor, conf));

            var msg1 = new MessageId(5, 1, 0);
            var msg2 = new MessageId(5, 2, 0);

            var isDuplicate = tracker.Ask <bool>(new IsDuplicate(msg1)).GetAwaiter().GetResult();

            Assert.False(isDuplicate);


            tracker.Tell(new AddAcknowledgment(msg1, CommandAck.AckType.Individual, new Dictionary <string, long>()));
            isDuplicate = tracker.Ask <bool>(new IsDuplicate(msg1)).GetAwaiter().GetResult();
            Assert.False(isDuplicate);

            tracker.Tell(FlushPending.Instance);
            isDuplicate = tracker.Ask <bool>(new IsDuplicate(msg1)).GetAwaiter().GetResult();
            Assert.False(isDuplicate);

            tracker.Tell(new AddAcknowledgment(msg2, CommandAck.AckType.Individual, new Dictionary <string, long>()));
            // Since we were connected, the ack went out immediately
            isDuplicate = tracker.Ask <bool>(new IsDuplicate(msg2)).GetAwaiter().GetResult();
            Assert.False(isDuplicate);
            tracker.GracefulStop(TimeSpan.FromSeconds(1));
        }
Ejemplo n.º 2
0
        public void TestUnAckMessageRedeliveryWithReceive()
        {
            var topic   = $"persistent://public/default/async-unack-redelivery-{Guid.NewGuid()}";
            var builder = new ConsumerConfigBuilder <byte[]>();

            builder.Topic(topic);
            builder.SubscriptionName("sub-TestUnAckMessageRedeliveryWithReceive");
            builder.AckTimeout(TimeSpan.FromMilliseconds(8000));
            builder.ForceTopicCreation(true);
            builder.AcknowledgmentGroupTime(0);
            builder.SubscriptionType(Protocol.Proto.CommandSubscribe.SubType.Shared);
            var consumer = _client.NewConsumer(builder);

            var pBuilder = new ProducerConfigBuilder <byte[]>();

            pBuilder.Topic(topic);
            var producer = _client.NewProducer(pBuilder);

            const int messageCount = 10;

            for (var i = 0; i < messageCount; i++)
            {
                var receipt = producer.Send(Encoding.UTF8.GetBytes("my-message-" + i));
                _output.WriteLine(JsonSerializer.Serialize(receipt, new JsonSerializerOptions {
                    WriteIndented = true
                }));
            }

            var messageReceived = 0;

            Thread.Sleep(TimeSpan.FromSeconds(5));
            for (var i = 0; i < messageCount; ++i)
            {
                var m = consumer.Receive();
                var receivedMessage = Encoding.UTF8.GetString(m.Data);
                _output.WriteLine($"Received message: [{receivedMessage}]");
                Assert.NotNull(receivedMessage);
                messageReceived++;
            }

            Assert.Equal(10, messageReceived);
            Thread.Sleep(TimeSpan.FromSeconds(10));
            for (var i = 0; i < messageCount; i++)
            {
                var m = consumer.Receive();
                var receivedMessage = Encoding.UTF8.GetString(m.Data);
                _output.WriteLine($"Received message: [{receivedMessage}]");
                Assert.NotNull(receivedMessage);
                messageReceived++;
            }
            Assert.Equal(20, messageReceived);
            producer.Close();
            consumer.Close();
        }
Ejemplo n.º 3
0
        public virtual void ConsumerInstantiation()
        {
            var consumer = new ConsumerConfigBuilder <string>();

            consumer.Topic("ConsumerInstantiation");
            consumer.SubscriptionName($"test-sub-{Guid.NewGuid()}");
            var stringConsumerBuilder = _client.NewConsumer(new StringSchema(), consumer);

            Assert.NotNull(stringConsumerBuilder);
            stringConsumerBuilder.Close();
        }
Ejemplo n.º 4
0
        private Consumer <byte[]> CreateConsumer(string topic, string consumerSub, KeySharedPolicy keySharedPolicy = null)
        {
            var builder = new ConsumerConfigBuilder <byte[]>();

            builder.Topic(topic);
            builder.SubscriptionName(consumerSub);
            builder.AckTimeout(TimeSpan.FromSeconds(10));
            builder.ForceTopicCreation(true);
            if (keySharedPolicy != null)
            {
                builder.KeySharedPolicy(keySharedPolicy);
            }
            builder.SubscriptionType(CommandSubscribe.SubType.KeyShared);
            return(_client.NewConsumer(builder));
        }
Ejemplo n.º 5
0
        public void TestAddAndRemove()
        {
            var builder = new ConsumerConfigBuilder <byte[]>();

            builder.Topic("TestAckTracker");
            builder.SubscriptionName("TestAckTracker-sub");
            var consumer = _client.NewConsumer(builder);
            var unack    = _system.ActorOf(UnAckedChunckedMessageIdSequenceMap.Prop());
            var tracker  = _client.ActorSystem.ActorOf(UnAckedMessageTracker.Prop(1000000, 100000, consumer.ConsumerActor, unack));

            var empty = tracker.Ask <bool>(Empty.Instance).GetAwaiter().GetResult();

            Assert.True(empty);

            var size = tracker.Ask <long>(Size.Instance).GetAwaiter().GetResult();

            Assert.Equal(0, size);

            var mid   = new MessageId(1L, 1L, -1);
            var added = tracker.Ask <bool>(new Add(mid)).GetAwaiter().GetResult();

            Assert.True(added);
            added = tracker.Ask <bool>(new Add(mid)).GetAwaiter().GetResult();
            Assert.False(added);
            size = tracker.Ask <long>(Size.Instance).GetAwaiter().GetResult();
            Assert.Equal(1, size);

            tracker.Tell(Clear.Instance);

            added = tracker.Ask <bool>(new Add(mid)).GetAwaiter().GetResult();
            Assert.True(added);

            size = tracker.Ask <long>(Size.Instance).GetAwaiter().GetResult();
            Assert.Equal(1, size);

            var removed = tracker.Ask <bool>(new Remove(mid)).GetAwaiter().GetResult();

            Assert.True(removed);

            empty = tracker.Ask <bool>(Empty.Instance).GetAwaiter().GetResult();
            Assert.True(empty);

            size = tracker.Ask <long>(Size.Instance).GetAwaiter().GetResult();
            Assert.Equal(0, size);
        }
Ejemplo n.º 6
0
        public void TestAckTracker()
        {
            var builder = new ConsumerConfigBuilder <byte[]>();

            builder.AcknowledgmentGroupTime(10000);
            builder.Topic($"TestAckTracker-{Guid.NewGuid()}");
            builder.SubscriptionName($"TestAckTracker-sub-{Guid.NewGuid()}");
            var conf     = builder.ConsumerConfigurationData;
            var consumer = _client.NewConsumer(builder);
            var unack    = _system.ActorOf(UnAckedChunckedMessageIdSequenceMap.Prop());
            var tracker  = _client.ActorSystem.ActorOf(PersistentAcknowledgmentsGroupingTracker <byte[]> .Prop(unack, consumer.ConsumerActor, consumer.ConsumerActor /*dummy*/, 1, consumer.ConsumerActor, conf));

            var msg1        = new MessageId(5, 1, 0);
            var msg2        = new MessageId(5, 2, 0);
            var msg3        = new MessageId(5, 3, 0);
            var msg4        = new MessageId(5, 4, 0);
            var msg5        = new MessageId(5, 5, 0);
            var msg6        = new MessageId(5, 6, 0);
            var isDuplicate = tracker.Ask <bool>(new IsDuplicate(msg1)).GetAwaiter().GetResult();

            Assert.False(isDuplicate);
            tracker.Tell(new AddAcknowledgment(msg1, CommandAck.AckType.Individual, new Dictionary <string, long>()));
            isDuplicate = tracker.Ask <bool>(new IsDuplicate(msg1)).GetAwaiter().GetResult();
            Assert.True(isDuplicate);

            isDuplicate = tracker.Ask <bool>(new IsDuplicate(msg2)).GetAwaiter().GetResult();
            Assert.False(isDuplicate);

            tracker.Tell(new AddAcknowledgment(msg5, CommandAck.AckType.Cumulative, new Dictionary <string, long>()));
            isDuplicate = tracker.Ask <bool>(new IsDuplicate(msg1)).GetAwaiter().GetResult();
            Assert.True(isDuplicate);
            isDuplicate = tracker.Ask <bool>(new IsDuplicate(msg2)).GetAwaiter().GetResult();
            Assert.True(isDuplicate);
            isDuplicate = tracker.Ask <bool>(new IsDuplicate(msg3)).GetAwaiter().GetResult();
            Assert.True(isDuplicate);

            isDuplicate = tracker.Ask <bool>(new IsDuplicate(msg4)).GetAwaiter().GetResult();
            Assert.True(isDuplicate);
            isDuplicate = tracker.Ask <bool>(new IsDuplicate(msg5)).GetAwaiter().GetResult();
            Assert.True(isDuplicate);
            isDuplicate = tracker.Ask <bool>(new IsDuplicate(msg6)).GetAwaiter().GetResult();
            Assert.False(isDuplicate);

            // Flush while disconnected. the internal tracking will not change
            tracker.Tell(FlushPending.Instance);

            isDuplicate = tracker.Ask <bool>(new IsDuplicate(msg1)).GetAwaiter().GetResult();
            Assert.True(isDuplicate);
            isDuplicate = tracker.Ask <bool>(new IsDuplicate(msg2)).GetAwaiter().GetResult();
            Assert.True(isDuplicate);
            isDuplicate = tracker.Ask <bool>(new IsDuplicate(msg3)).GetAwaiter().GetResult();
            Assert.True(isDuplicate);

            isDuplicate = tracker.Ask <bool>(new IsDuplicate(msg4)).GetAwaiter().GetResult();
            Assert.True(isDuplicate);
            isDuplicate = tracker.Ask <bool>(new IsDuplicate(msg5)).GetAwaiter().GetResult();
            Assert.True(isDuplicate);
            isDuplicate = tracker.Ask <bool>(new IsDuplicate(msg6)).GetAwaiter().GetResult();
            Assert.False(isDuplicate);

            tracker.Tell(new AddAcknowledgment(msg6, CommandAck.AckType.Individual, new Dictionary <string, long>()));
            isDuplicate = tracker.Ask <bool>(new IsDuplicate(msg6)).GetAwaiter().GetResult();
            Assert.True(isDuplicate);

            tracker.Tell(FlushPending.Instance);

            isDuplicate = tracker.Ask <bool>(new IsDuplicate(msg1)).GetAwaiter().GetResult();
            Assert.True(isDuplicate);
            isDuplicate = tracker.Ask <bool>(new IsDuplicate(msg2)).GetAwaiter().GetResult();
            Assert.True(isDuplicate);
            isDuplicate = tracker.Ask <bool>(new IsDuplicate(msg3)).GetAwaiter().GetResult();
            Assert.True(isDuplicate);

            isDuplicate = tracker.Ask <bool>(new IsDuplicate(msg4)).GetAwaiter().GetResult();
            Assert.True(isDuplicate);
            isDuplicate = tracker.Ask <bool>(new IsDuplicate(msg5)).GetAwaiter().GetResult();
            Assert.True(isDuplicate);
            isDuplicate = tracker.Ask <bool>(new IsDuplicate(msg6)).GetAwaiter().GetResult();
            Assert.False(isDuplicate);

            tracker.GracefulStop(TimeSpan.FromSeconds(1));
        }