public void SimpleManagerTest()
        {
            var connectionString = Environment.GetEnvironmentVariable("ConnectionString");

            // Retrieve the partition information from the Event or IoT Hub
            var client = EventHubClient.CreateFromConnectionString(connectionString);

            client.RetryPolicy = new RetryExponential(TimeSpan.FromSeconds(2), TimeSpan.FromSeconds(10), 10);
            var runtimeInformation = client.GetRuntimeInformationAsync().GetAwaiter().GetResult();
            var partitions         = runtimeInformation.PartitionIds.OrderBy(item => item).ToArray();

            var logger  = new LoggerFactory().CreateLogger("test");
            var manager = new FixedPartitionManager(logger, connectionString, 0);

            manager.InitializeAsync(2).GetAwaiter().GetResult();

            var managerPartitions      = manager.GetPartitions();
            var managerOwnedPartitions = manager.GetPartitions();
            var isOwned = manager.IsOwner("0");

            Assert.IsTrue(managerPartitions.Length == partitions.Length, "Partition counts should match");
            Assert.IsTrue(managerPartitions.Intersect(partitions).ToArray().Length == partitions.Length, "Intersecting partition should include all partitions");

            var partitionHalf = (int)(partitions.Length / 2) - 1;

            Assert.IsTrue(Math.Abs(managerOwnedPartitions.Length - partitionHalf) <= 1, "Owned partitions should be approximately half in case of odd numbers");
            Assert.IsTrue(Math.Abs(managerPartitions.Intersect(managerOwnedPartitions).ToArray().Length - partitionHalf) <= 1, "Intersecting owned partition should include all partitions");
            Assert.IsTrue(isOwned, "Ownership of partition 0 is expected based on the known algorithm. If the algorithm of modulo fails this needs adjusted");
        }
        public void DetermineOwnership(int id, int quantity)
        {
            var logger  = GetLogger("testing");
            var manager = new FixedPartitionManager(logger, Environment.GetEnvironmentVariable("iotc_eventhub"), id);


            manager.InitializeAsync(quantity).GetAwaiter().GetResult();
            var partitions = manager.GetPartitions();
            var ownership  = new Dictionary <string, bool>();


            var ownedPartitions = manager.GetOwnedPartitions();

            for (var index = 0; index < partitions.Length; index++)
            {
                var partitionId = partitions[index];
                var isOwner     = manager.IsOwner(partitionId);

                ownership.Add(partitionId, isOwner);
            }

            for (var index = 0; index < partitions.Length; index++)
            {
                var partitionId = partitions[index];
                var shouldOwn   = (index % quantity == id);

                Assert.IsTrue(ownership[partitionId] == shouldOwn);
            }
        }
Example #3
0
        /// <summary>
        /// An shared method for populating the lease collection
        /// </summary>
        private void PopulateCollectionInternal()
        {
            var leases        = new Dictionary <string, EventProcessorPartitionOwnership>();
            var allPartitions = _partitionManager.GetPartitions();

            foreach (var partition in allPartitions)
            {
                var isOwned = _partitionManager.IsOwner(partition);
                var exists  = _leases.TryGetValue(partition, out var existing);

                var ownership = new EventProcessorPartitionOwnership
                {
                    ConsumerGroup           = _client.ConsumerGroup,
                    EventHubName            = _client.EventHubName,
                    FullyQualifiedNamespace = _client.FullyQualifiedNamespace,
                    PartitionId             = partition,
                    Version = LeaseVersion
                };

                if (isOwned)
                {
                    if (exists && string.Equals(existing.OwnerIdentifier, _client.Identifier, StringComparison.Ordinal))
                    {
                        ownership.OwnerIdentifier  = existing.OwnerIdentifier;
                        ownership.LastModifiedTime = existing.LastModifiedTime;
                    }
                    else
                    {
                        ownership.OwnerIdentifier  = null;
                        ownership.LastModifiedTime = DateTimeOffset.MinValue;
                    }
                }
                else
                {
                    ownership.OwnerIdentifier  = NotOwned;
                    ownership.LastModifiedTime = _maximumDateTime;
                }

                leases.Add(partition, ownership);
            }

            _leases = leases;
        }