/// <summary>
 ///   Requests creation of an Azure storage account to use for a specific test run,
 ///   transforming the asynchronous request into a synchronous one that can be used with
 ///   lazy instantiation.
 /// </summary>
 ///
 /// <returns>The active Azure storage account for this test run./returns>
 ///
 private static StorageScope.StorageProperties CreateStorageAccount() =>
     Task
         .Run(async () => await StorageScope.CreateStorageAccountAsync().ConfigureAwait(false))
         .ConfigureAwait(false)
         .GetAwaiter()
         .GetResult();
        public async Task ClaimOwnershipAsyncReturnsOnlyTheSuccessfullyClaimedOwnership()
        {
            await using (StorageScope storageScope = await StorageScope.CreateAsync())
            {
                var storageConnectionString = StorageTestEnvironment.StorageConnectionString;
                var containerClient         = new BlobContainerClient(storageConnectionString, storageScope.ContainerName);

                var checkpointStore = new BlobsCheckpointStore(containerClient, DefaultRetryPolicy);
                var ownershipList   = new List <PartitionOwnership>();
                var ownershipCount  = 5;

                for (int i = 0; i < ownershipCount; i++)
                {
                    ownershipList.Add(
                        new PartitionOwnership
                        (
                            "namespace",
                            "eventHubName",
                            "consumerGroup",
                            "ownerIdentifier",
                            partitionId: $"{i}"
                        ));
                }

                await checkpointStore.ClaimOwnershipAsync(ownershipList, default);

                // The ETags must have been set by the checkpoint store.

                var eTags = ownershipList.Select(ownership => ownership.ETag).ToList();

                ownershipList.Clear();

                // Use a valid eTag when 'i' is odd.  This way, we can expect 'ownershipCount / 2' successful
                // claims (rounded down).

                var expectedClaimedCount = ownershipCount / 2;

                for (int i = 0; i < ownershipCount; i++)
                {
                    ownershipList.Add(
                        new PartitionOwnership
                        (
                            "namespace",
                            "eventHubName",
                            "consumerGroup",
                            "ownerIdentifier",
                            partitionId: $"{i}",
                            eTag: i % 2 == 1 ? eTags[i] : null
                        ));
                }

                IEnumerable <PartitionOwnership> claimedOwnershipList = await checkpointStore.ClaimOwnershipAsync(ownershipList, default);

                IEnumerable <PartitionOwnership> expectedOwnership = ownershipList.Where(ownership => int.Parse(ownership.PartitionId) % 2 == 1);

                Assert.That(claimedOwnershipList, Is.Not.Null);
                Assert.That(claimedOwnershipList.Count, Is.EqualTo(expectedClaimedCount));

                var index = 0;

                foreach (PartitionOwnership ownership in claimedOwnershipList.OrderBy(ownership => ownership.PartitionId))
                {
                    Assert.That(ownership.IsEquivalentTo(expectedOwnership.ElementAt(index)), Is.True, $"Ownership of partition '{ ownership.PartitionId }' should be equivalent.");
                    ++index;
                }
            }
        }