Example #1
0
        public async Task StressTest()
        {
            // this test adds the integers min..max to the dictionary
            // then, in multiple threads (ratio * max tasks),
            // creates transactions that (in the given ratio)
            // either updates the value to its negative or removes it

            const int min   = 1;
            const int max   = 10000;
            const int ratio = 10;

            var rng = new ThreadLocal <Random>(() => new Random());

            using (var tx = new TestTransaction())
            {
                for (var i = min; i <= max; i++)
                {
                    await _dictionary.AddAsync(tx, i, i);
                }

                await tx.CommitAsync();
            }

            var tasks = Enumerable.Range(1, max * ratio).OrderBy(x => rng.Value.Next()).Select(async x =>
            {
                using (var tx = new TestTransaction())
                {
                    var key = x / ratio;
                    if (rng.Value.Next(ratio) == 0)
                    {
                        await _dictionary.TryRemoveAsync(tx, key);
                    }
                    else
                    {
                        await _dictionary.TryUpdateAsync(tx, key, -key, key);
                    }

                    await tx.CommitAsync();
                }
            }).ToArray();

            await Task.WhenAll(tasks);

            using (var tx = new TestTransaction())
            {
                var enumerable = await _dictionary.CreateEnumerableAsync(tx);

                using (var enumerator = enumerable.GetAsyncEnumerator())
                {
                    while (await enumerator.MoveNextAsync(CancellationToken.None))
                    {
                        var x = enumerator.Current.Value;
                        Assert.IsTrue(x >= -max);
                        Assert.IsTrue(x <= -min);
                    }
                }
            }
        }
Example #2
0
        public async Task StressTest()
        {
            // this test adds the integers min..max to the queue
            // then, in multiple threads (ratio * max tasks),
            // creates transactions that (in the given ratio)
            // either enqueues or dequeues

            const int min   = 1;
            const int max   = 1000;
            const int ratio = 10;

            var rng = new ThreadLocal <Random>(() => new Random());

            using (var tx = new TestTransaction())
            {
                for (var i = min; i <= max; i++)
                {
                    await _queue.EnqueueAsync(tx, i);
                }

                await tx.CommitAsync();
            }

            var added = 0;

            var tasks = Enumerable.Range(1, max * ratio).Select(async x =>
            {
                using (var tx = new TestTransaction())
                {
                    var key = x / ratio;
                    if (rng.Value.Next(ratio) == 0)
                    {
                        await _queue.TryDequeueAsync(tx);
                        Interlocked.Decrement(ref added);
                    }
                    else
                    {
                        await _queue.EnqueueAsync(tx, key);
                        Interlocked.Increment(ref added);
                    }

                    await tx.CommitAsync();
                }
            }).ToArray();

            await Task.WhenAll(tasks);

            using (var tx = new TestTransaction())
            {
                var count = await _queue.GetCountAsync(tx);

                Assert.AreEqual(max + added, count);
            }
        }
Example #3
0
        public async Task SingleValueTransactionComitted()
        {
            using (var tx = new TestTransaction())
            {
                await _queue.EnqueueAsync(tx, 1);

                await tx.CommitAsync();
            }

            using (var tx = new TestTransaction())
            {
                var value = await _queue.TryDequeueAsync(tx);

                Assert.AreEqual(1, value.Value);
            }
        }
Example #4
0
        public async Task SingleValueTransactionComitted()
        {
            using (var tx = new TestTransaction())
            {
                await _dictionary.AddAsync(tx, 1, 1);

                await tx.CommitAsync();
            }

            using (var tx = new TestTransaction())
            {
                var value = await _dictionary.TryGetValueAsync(tx, 1);

                Assert.AreEqual(1, value.Value);
            }
        }
Example #5
0
        public async Task TwoConcurrentTransactions()
        {
            using (var tx1 = new TestTransaction())
                using (var tx2 = new TestTransaction())
                {
                    await _queue.EnqueueAsync(tx1, 1);

                    var task = _queue.EnqueueAsync(tx2, 2);
                    await tx1.CommitAsync();

                    await task;
                    await tx2.CommitAsync();
                }

            using (var tx = new TestTransaction())
            {
                var item = await _queue.TryDequeueAsync(tx);

                Assert.AreEqual(1, item.Value);
                item = await _queue.TryDequeueAsync(tx);

                Assert.AreEqual(2, item.Value);
            }
        }