Example #1
0
        public static async Task VerifyQueueAsync(
            TransactionalReplicator transactionalReplicator,
            Uri queueName,
            long startingNumber,
            int batchCount,
            int batchSize)
        {
            var queue = GetQueue <long>(transactionalReplicator, queueName);

            using (var tx = transactionalReplicator.CreateTransaction())
            {
                var enumerableQueue = await queue.CreateEnumerableAsync(tx).ConfigureAwait(false);

                var enumerator = enumerableQueue.GetAsyncEnumerator();
                var count      = 0;
                while (await enumerator.MoveNextAsync(CancellationToken.None).ConfigureAwait(false))
                {
                    count += 1;
                }

                Assert.AreEqual(batchCount * batchSize, count, "Queue does not have the expected size");
                Assert.AreEqual(
                    batchCount * batchSize,
                    await queue.GetCountAsync(tx).ConfigureAwait(false),
                    "Queue does not have the expected size");
            }

            for (var batchIndex = 0; batchIndex < batchCount; batchIndex++)
            {
                using (var txn = transactionalReplicator.CreateTransaction())
                {
                    for (var key = (batchIndex * batchSize) + startingNumber; key < (batchIndex + 1) * batchSize; key++)
                    {
                        var enumerable = await queue.CreateEnumerableAsync(txn);

                        var enumerator = enumerable.GetAsyncEnumerator();

                        var foundKey = false;
                        while (await enumerator.MoveNextAsync(CancellationToken.None).ConfigureAwait(false))
                        {
                            if (enumerator.Current == key)
                            {
                                foundKey = true;
                                break;
                            }
                        }

                        Assert.IsTrue(foundKey == true, "Queue does not contain {0}", key);
                    }

                    await txn.CommitAsync().ConfigureAwait(false);
                }
            }
        }
Example #2
0
        public static async Task VerifyDictionaryAsync(
            TransactionalReplicator transactionalReplicator,
            Uri dictionaryName,
            long startingNumber,
            int batchCount,
            int batchSize)
        {
            var dictionary = GetDictionary <long, long>(transactionalReplicator, dictionaryName);

            for (var batchIndex = 0; batchIndex < batchCount; batchIndex++)
            {
                using (var txn = transactionalReplicator.CreateTransaction())
                {
                    for (var key = (batchIndex * batchSize) + startingNumber; key < (batchIndex + 1) * batchSize; key++)
                    {
                        var value = await dictionary.TryGetValueAsync(txn, key).ConfigureAwait(false);

                        Assert.IsTrue(value.HasValue, "Dictionary must contain {0}", key);
                        Assert.AreEqual(key, value.Value, "Dictionary must contain {0}", key);
                    }

                    await txn.CommitAsync().ConfigureAwait(false);
                }
            }
        }
Example #3
0
        public static async Task PopulateDictionaryAsync(
            TransactionalReplicator transactionalReplicator,
            Uri dictionaryName,
            long startingNumber,
            int batchCount,
            int batchSize)
        {
            var dictionary = GetDictionary <long, long>(transactionalReplicator, dictionaryName);
            IDistributedDictionary <long, long> distributedDictionary = dictionary as IDistributedDictionary <long, long>;

            var startingCount = distributedDictionary.Count;

            for (var batchIndex = 0; batchIndex < batchCount; batchIndex++)
            {
                using (var txn = transactionalReplicator.CreateTransaction())
                {
                    for (var key = (batchIndex * batchSize) + startingNumber; key < ((batchIndex + 1) * batchSize) + startingNumber; key++)
                    {
                        await dictionary.AddAsync(txn, key, key).ConfigureAwait(false);
                    }

                    Assert.AreEqual(startingCount + (batchIndex * batchSize), distributedDictionary.Count);
                    await txn.CommitAsync().ConfigureAwait(false);
                }

                Assert.AreEqual(startingCount + ((batchIndex + 1) * batchSize), distributedDictionary.Count);
            }

            var finalCount = distributedDictionary.Count;

            Assert.AreEqual(startingCount + (batchCount * batchSize), finalCount);

            await VerifyDictionaryAsync(transactionalReplicator, dictionaryName, startingNumber, batchCount, batchSize).ConfigureAwait(false);
        }
Example #4
0
        public static async Task PopulateConcurrentQueueAsync(
            TransactionalReplicator transactionalReplicator,
            Uri queueName,
            long startingNumber,
            int batchCount,
            int batchSize)
        {
            var queue          = GetConcurrentQueue <long>(transactionalReplicator, queueName);
            var expectedValues = new List <long>(batchCount * batchSize);

            for (var batchIndex = 0; batchIndex < batchCount; batchIndex++)
            {
                using (var txn = transactionalReplicator.CreateTransaction())
                {
                    for (var key = (batchIndex * batchSize) + startingNumber; key < (batchIndex + 1) * batchSize; key++)
                    {
                        await queue.EnqueueAsync(txn, key);

                        expectedValues.Add(key);
                    }

                    await txn.CommitAsync();
                }
            }

            VerifyConcurrentQueueUnordered(queue, expectedValues);
        }
        internal static async Task PopulateDictionaryAsync <ValueType>(
            TransactionalReplicator transactionalReplicator,
            Uri dictionaryName,
            long startingNumber,
            int batchCount,
            int batchSize,
            ValueType value)
        {
            var dictionary            = transactionalReplicator.TryGetStateProvider(dictionaryName).Value as IReliableDictionary <long, ValueType>;
            var distributedDictionary = dictionary as IDistributedDictionary <long, ValueType>;

            var startingCount = distributedDictionary.Count;

            for (var batchIndex = 0; batchIndex < batchCount; batchIndex++)
            {
                using (var txn = transactionalReplicator.CreateTransaction())
                {
                    for (var key = (batchIndex * batchSize) + startingNumber; key < ((batchIndex + 1) * batchSize) + startingNumber; key++)
                    {
                        await dictionary.AddAsync(txn, key, value).ConfigureAwait(false);
                    }

                    // Data has not been added yet.
                    Assert.AreEqual(startingCount + (batchIndex * batchSize), distributedDictionary.Count);
                    await txn.CommitAsync().ConfigureAwait(false);
                }

                // Data is added and committed now.
                Assert.AreEqual(startingCount + ((batchIndex + 1) * batchSize), distributedDictionary.Count);
            }

            var finalCount = distributedDictionary.Count;

            Assert.AreEqual(startingCount + (batchCount * batchSize), finalCount);
        }
Example #6
0
        public static async Task PopulateQueueAsync(
            TransactionalReplicator transactionalReplicator,
            Uri queueName,
            long startingNumber,
            int batchCount,
            int batchSize)
        {
            var queue = GetQueue <long>(transactionalReplicator, queueName);

            for (var batchIndex = 0; batchIndex < batchCount; batchIndex++)
            {
                using (var txn = transactionalReplicator.CreateTransaction())
                {
                    for (var key = (batchIndex * batchSize) + startingNumber; key < (batchIndex + 1) * batchSize; key++)
                    {
                        await queue.EnqueueAsync(txn, key).ConfigureAwait(false);
                    }

                    await txn.CommitAsync().ConfigureAwait(false);
                }
            }

            await VerifyQueueAsync(transactionalReplicator, queueName, startingNumber, batchCount, batchSize).ConfigureAwait(false);
        }
        /// <summary>
        /// Creates a replicator transaction and retries transient errors.
        /// </summary>
        /// <returns></returns>
        public static async Task <Transaction> SafeCreateReplicatorTransactionAsync(TransactionalReplicator replicator)
        {
            Transaction replicatorTransaction = null;

            // Create replicator transaction.
            while (true)
            {
                bool doneCreateTx = false;
                try
                {
                    replicatorTransaction = replicator.CreateTransaction();
                    doneCreateTx          = true;
                }
                catch (FabricTransientException)
                {
                    // Retry.
                }
                catch (Exception e)
                {
                    // Be done.
                    throw e;
                }

                if (!doneCreateTx)
                {
                    // Sleep for a while.
                    await Task.Delay(1000);
                }
                else
                {
                    break;
                }
            }

            return(replicatorTransaction);
        }