Beispiel #1
0
        public void Test_MessageKeyPartitionSelection_Fallbacks_To_RoundRobin_If_MessageKey_Null()
        {
            var nodeMock   = new NodeMock();
            var partitions = new[]
            {
                new Partition {
                    Id = 0, Leader = nodeMock
                },
                new Partition {
                    Id = 1, Leader = nodeMock
                },
                new Partition {
                    Id = 2, Leader = nodeMock
                },
            };
            var partitionStrategy = new MessageKeyPartitionSelection(Serializer, RoundRobinPartitionSelection, Mock.Of <ILogger>());
            var partitioner       = new PartitionSelector(partitionStrategy);
            var message           = ProduceMessage.New(string.Empty, Partitions.Any, new Message {
                Key = null
            }, new DateTime());

            var partition = partitioner.GetPartition(message, partitions);

            Assert.IsTrue(partition.Id != Partition.None.Id);
        }
Beispiel #2
0
        public void Test_MessageKeyPartitionSelection_Fallbacks_To_RoundRobin_If_Partition_Blacklisted(int partitionIdBlacklisted)
        {
            var nodeMock   = new NodeMock();
            var partitions = new[]
            {
                new Partition {
                    Id = 0, Leader = nodeMock
                },
                new Partition {
                    Id = 1, Leader = nodeMock
                },
                new Partition {
                    Id = 2, Leader = nodeMock
                },
            };
            var blacklistedPartitions = new Dictionary <int, DateTime> {
                { partitionIdBlacklisted, DateTime.MaxValue }
            };
            var partitionStrategy = new MessageKeyPartitionSelection(Serializer, RoundRobinPartitionSelection, Mock.Of <ILogger>());
            var partitioner       = new PartitionSelector(partitionStrategy);
            var message           = ProduceMessage.New(string.Empty, Partitions.Any, new Message {
                Key = "ThisIsMyKey"
            }, new DateTime());

            for (var i = 0; i < 300; i++)
            {
                var partition = partitioner.GetPartition(message, partitions, blacklistedPartitions);
                Assert.IsTrue(partition.Id != Partition.None.Id);
                Assert.IsTrue(partition.Id != partitionIdBlacklisted);
            }
        }
Beispiel #3
0
        public void Test_MessageKeyPartitionSelection_Is_Consistent()
        {
            var nodeMock   = new NodeMock();
            var partitions = new[]
            {
                new Partition {
                    Id = 0, Leader = nodeMock
                },
                new Partition {
                    Id = 1, Leader = nodeMock
                },
                new Partition {
                    Id = 2, Leader = nodeMock
                },
            };
            var partitionStrategy = new MessageKeyPartitionSelection(Serializer, RoundRobinPartitionSelection, Mock.Of <ILogger>());
            var partitioner       = new PartitionSelector(partitionStrategy);
            var message1          = ProduceMessage.New(string.Empty, Partitions.Any, new Message {
                Key = "ThisIsMyKey"
            }, new DateTime());
            var message2 = ProduceMessage.New(string.Empty, Partitions.Any, new Message {
                Key = "ThisIsMyOtherKey"
            }, new DateTime());

            var expectedPartition1 = partitioner.GetPartition(message1, partitions);
            var expectedPartition2 = partitioner.GetPartition(message2, partitions);

            for (var i = 0; i < 300; i++)
            {
                var currentPartition1 = partitioner.GetPartition(message1, partitions);
                var currentPartition2 = partitioner.GetPartition(message2, partitions);
                Assert.AreEqual(expectedPartition1.Id, currentPartition1.Id);
                Assert.AreEqual(expectedPartition2.Id, currentPartition2.Id);
            }
        }