示例#1
0
        public void PartitionerTest()
        {
            var partitioner = new Partitioner();

            Assert.That(partitioner.Count, Is.EqualTo(0));

            Assert.That(partitioner.GetPartitionOwner(1), Is.EqualTo(Guid.Empty));
            Assert.That(partitioner.GetPartitionOwner(-1), Is.EqualTo(Guid.Empty));
            Assert.That(partitioner.GetPartitionOwner(new PartitionHashClass(1).PartitionHash), Is.EqualTo(Guid.Empty));
            Assert.That(partitioner.GetPartitionId(new PartitionHashClass(1).PartitionHash), Is.EqualTo(0));
            Assert.That(partitioner.GetPartitionId(new PartitionHashClass(int.MinValue).PartitionHash), Is.EqualTo(0));
            Assert.That(partitioner.GetRandomPartitionId(), Is.EqualTo(0));

            partitioner.SetOrVerifyPartitionCount(4);

            Assert.That(partitioner.GetPartitionOwner(1), Is.EqualTo(Guid.Empty));
            Assert.That(partitioner.GetPartitionOwner(-1), Is.EqualTo(Guid.Empty));
            Assert.That(partitioner.GetPartitionOwner(new PartitionHashClass(1).PartitionHash), Is.EqualTo(Guid.Empty));
            Assert.That(partitioner.GetPartitionId(new PartitionHashClass(1).PartitionHash), Is.EqualTo(1));
            Assert.That(partitioner.GetPartitionId(new PartitionHashClass(5).PartitionHash), Is.EqualTo(1));
            Assert.That(partitioner.GetPartitionId(new PartitionHashClass(int.MinValue).PartitionHash), Is.EqualTo(0));
            var random = partitioner.GetRandomPartitionId();

            Assert.That(random, Is.GreaterThanOrEqualTo(0));
            Assert.That(random, Is.LessThan(4));

            var originClientId = Guid.NewGuid();
            var map            = new Dictionary <int, Guid>
            {
                { 1, Guid.NewGuid() },
                { 2, Guid.NewGuid() },
                { 3, Guid.NewGuid() },
            };

            Assert.That(partitioner.NotifyPartitionView(originClientId, 0, map));

            Assert.Throws <InvalidOperationException>(() => partitioner.SetOrVerifyPartitionCount(7));

            Assert.That(partitioner.GetPartitionOwner(1), Is.EqualTo(map[1]));
            Assert.That(partitioner.GetPartitionOwner(-1), Is.EqualTo(Guid.Empty));
            Assert.That(partitioner.GetPartitionOwner(new PartitionHashClass(1).PartitionHash), Is.EqualTo(map[1]));
            Assert.That(partitioner.GetPartitionId(new PartitionHashClass(1).PartitionHash), Is.EqualTo(1));
            Assert.That(partitioner.GetPartitionId(new PartitionHashClass(4).PartitionHash), Is.EqualTo(1));
            Assert.That(partitioner.GetPartitionId(new PartitionHashClass(int.MinValue).PartitionHash), Is.EqualTo(0));
            random = partitioner.GetRandomPartitionId();
            Assert.That(random, Is.GreaterThanOrEqualTo(0));
            Assert.That(random, Is.LessThan(3));

            Assert.That(partitioner.GetPartitionHashOwner(1), Is.EqualTo(map[1]));

            var newMap = new Dictionary <int, Guid>
            {
                { 1, Guid.NewGuid() },
                { 2, Guid.NewGuid() },
                { 3, Guid.NewGuid() },
            };

            Assert.That(partitioner.NotifyPartitionView(originClientId, 0, newMap), Is.False);
            Assert.That(partitioner.GetPartitionOwner(1), Is.EqualTo(map[1]));

            Assert.That(partitioner.NotifyPartitionView(originClientId, 1, newMap));
            Assert.That(partitioner.GetPartitionOwner(1), Is.EqualTo(newMap[1]));

            newMap = new Dictionary <int, Guid>
            {
                { 1, Guid.NewGuid() },
                { 2, Guid.NewGuid() },
                { 3, Guid.NewGuid() },
            };

            var newOwnerId = Guid.NewGuid();

            Assert.That(partitioner.NotifyPartitionView(newOwnerId, 1, newMap));
            Assert.That(partitioner.GetPartitionOwner(1), Is.EqualTo(newMap[1]));

            Assert.That(partitioner.NotifyPartitionView(newOwnerId, 1, new Dictionary <int, Guid>()), Is.False);
            Assert.That(partitioner.GetPartitionOwner(1), Is.EqualTo(newMap[1]));
        }