Пример #1
0
        public async Task CancelScenario()
        {
            var connection = ConfigurationHelper.GetNewConnection();
            var library    = new ScriptLibrary(connection);
            var service    = new LockService(library, new Internals.Locks.IdentifierGenerator());

            var root = Guid.NewGuid().ToString().Substring(0, 8);
            var key  = new Key(root, "name", TimeSpan.FromSeconds(2));

            var db = connection.GetDatabase();

            await ResetKeys(db, key);

            try
            {
                using var cancel = new CancellationTokenSource();
                cancel.CancelAfter(500);
                try
                {
                    await service.LockAsync(key, c => Task.Delay(1000, c), cancel.Token);
                }
                catch (Exception e)
                {
                    Assert.IsType <TaskCanceledException>(e.InnerException?.InnerException ?? e.InnerException ?? e);
                }

                Assert.False(await db.KeyExistsAsync(key.GetLockKey()));
                Assert.False(await db.KeyExistsAsync(key.GetLockQueueKey()));
                Assert.False(await db.KeyExistsAsync(key.GetLockQueueSetKey()));
            }
            finally
            {
                await ResetKeys(db, key);
            }
        }
Пример #2
0
        public async Task SimpleScenario()
        {
            var connection = ConfigurationHelper.GetNewConnection();
            var library    = new ScriptLibrary(connection);
            var service    = new LockService(library, new Internals.Locks.IdentifierGenerator());

            var root = Guid.NewGuid().ToString().Substring(0, 8);
            var key  = new Key(root, "name", TimeSpan.FromSeconds(1));

            var result = false;
            var db     = connection.GetDatabase();

            await ResetKeys(db, key);

            try
            {
                await service.LockAsync(key, c =>
                {
                    result = true;
                    return(Task.CompletedTask);
                }, CancellationToken.None);

                Assert.True(result);
                Assert.False(await db.KeyExistsAsync(key.GetLockKey()));
                Assert.False(await db.KeyExistsAsync(key.GetLockQueueKey()));
                Assert.False(await db.KeyExistsAsync(key.GetLockQueueSetKey()));
            }
            finally
            {
                await ResetKeys(db, key);
            }
        }
Пример #3
0
        public async Task PublishTests(object value)
        {
            var connection = ConfigurationHelper.GetNewConnection();
            var library    = new MemoScriptLibrary(connection);
            var db         = connection.GetDatabase();

            var root       = Guid.NewGuid().ToString().Substring(0, 8);
            var key        = new MemoKey(root, "key", TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(1));
            var parameters = new MemoSetValueParameters(key, RedisValue.Unbox(value));

            try
            {
                await ResetKeys(db, parameters);

                var message = RedisValue.Null;
                await connection.GetSubscriber().SubscribeAsync(parameters.MemoChannelKey, (c, v) => message = v);

                await library.PublishAsync(parameters);

                await Task.Delay(500);

                var expectedValue = value == null ? RedisValue.EmptyString : RedisValue.Unbox(value);
                Assert.Equal(expectedValue, message);
                Assert.Equal(expectedValue, await db.StringGetAsync(parameters.MemoKey));
            }
            finally
            {
                await ResetKeys(db, parameters);
            }
        }
Пример #4
0
        public async Task ConcurrencyScenario()
        {
            var connection = ConfigurationHelper.GetNewConnection();
            var library    = new ScriptLibrary(connection);
            var service    = new LockService(library, new Internals.Locks.IdentifierGenerator());

            var root = Guid.NewGuid().ToString().Substring(0, 8);
            var key  = new Key(root, "name", TimeSpan.FromSeconds(5));

            var db = connection.GetDatabase();

            var events = new ConcurrentQueue <string>();
            var concurrencyScenarioCounter = 0;
            var errorCount = 0;
            var counter    = 0;

            await ResetKeys(db, key);

            try
            {
                var watch = new Stopwatch();
                var tasks = Enumerable.Range(0, 100).Select(i => service.LockAsync(key, async c =>
                {
                    Interlocked.Increment(ref concurrencyScenarioCounter);
                    events.Enqueue($"{i:d3} in  at {watch.ElapsedMilliseconds:d4}ms thread id {Thread.CurrentThread.ManagedThreadId}");
                    if (concurrencyScenarioCounter != 1)
                    {
                        watch.Stop();
                        Interlocked.Increment(ref errorCount);
                        throw new Exception($"Lock failed after {watch.ElapsedMilliseconds}" + Environment.NewLine + string.Join(Environment.NewLine, events));
                    }
                    Interlocked.Increment(ref counter);
                    await Task.Delay(10);
                    events.Enqueue($"{i:d3} out at {watch.ElapsedMilliseconds:d4}ms thread id {Thread.CurrentThread.ManagedThreadId}");
                    Interlocked.Decrement(ref concurrencyScenarioCounter);
                }, CancellationToken.None));

                watch.Start();
                await Task.WhenAll(tasks);

                Assert.Equal(100, counter);
                Assert.Equal(0, errorCount);

                Assert.False(await db.KeyExistsAsync(key.GetLockKey()));
                Assert.False(await db.KeyExistsAsync(key.GetLockQueueKey()));
                Assert.False(await db.KeyExistsAsync(key.GetLockQueueSetKey()));
            }
            finally
            {
                await ResetKeys(db, key);
            }
        }
Пример #5
0
        public async Task SubscribeTests()
        {
            var connection = ConfigurationHelper.GetNewConnection();
            var library    = new MemoScriptLibrary(connection);
            var db         = connection.GetDatabase();

            var timeout = 500;
            var root    = Guid.NewGuid().ToString().Substring(0, 8);
            var key     = new MemoKey(root, "key", TimeSpan.FromMilliseconds(timeout), TimeSpan.FromSeconds(1));
            var result  = false;
            await library.SubscribeAsync(key.GetSubscribtionChannel(), (c, v) => result = true);

            await db.PublishAsync(key.GetValueChannel(), "identifier");

            await Task.Delay(30);

            Assert.True(result);
        }
Пример #6
0
        public async Task SubscribeTests()
        {
            var connection = ConfigurationHelper.GetNewConnection();
            var library    = new ScriptLibrary(connection);
            var db         = connection.GetDatabase();

            var root = Guid.NewGuid().ToString().Substring(0, 8);
            var key  = new Key(root, "name", TimeSpan.FromSeconds(2));

            using var context = new LockState(key, "identifier", CancellationToken.None);
            await library.SubscribeAsync(context);

            await db.PublishAsync(key.GetLockChannelKey(), "identifier");

            await Task.Delay(500);

            Assert.Equal(State.WithKey, context.State);//proof that the message was received
            await library.UnSubscribeAsync(context);
        }
Пример #7
0
        public async Task GetKeySituationTests(string priorValue, string waiting, int expected)
        {
            var connection = ConfigurationHelper.GetNewConnection();
            var library    = new ScriptLibrary(connection);
            var db         = connection.GetDatabase();

            //SETUP
            var root       = Guid.NewGuid().ToString().Substring(0, 8);
            var parameters = new LockLuaParameters("id", new Key(root, "name", TimeSpan.FromMilliseconds(500)));

            try
            {
                await ResetKeys(db, parameters);

                if (!string.IsNullOrEmpty(priorValue))
                {
                    await db.StringSetAsync(parameters.LockKey, priorValue);
                }

                if (!string.IsNullOrEmpty(waiting))
                {
                    await db.HashSetAsync(parameters.LockWaitingSetKey, waiting, 0);
                }

                //ACT
                var result = await library.GetKeySituation(parameters);

                var exists = await db.HashExistsAsync(parameters.LockWaitingSetKey, parameters.Identifier);

                Assert.Equal(expected, result);
            }
            finally
            {
                await ResetKeys(db, parameters);
            }
        }
Пример #8
0
        public async Task FreeLockAndPopScriptTests(string priorValue, string waiting)
        {
            var connection = ConfigurationHelper.GetNewConnection();
            var library    = new ScriptLibrary(connection);
            var db         = connection.GetDatabase();

            //SETUP
            var root       = Guid.NewGuid().ToString().Substring(0, 8);
            var parameters = new LockLuaParameters("id", new Key(root, "name", TimeSpan.FromMilliseconds(500)));

            try
            {
                await ResetKeys(db, parameters);

                if (!string.IsNullOrEmpty(priorValue))
                {
                    await db.StringSetAsync(parameters.LockKey, priorValue, TimeSpan.FromMilliseconds(parameters.Timeout));
                }

                if (!string.IsNullOrEmpty(waiting))
                {
                    await db.HashSetAsync(parameters.LockWaitingSetKey, waiting, 0);

                    await db.ListRightPushAsync(parameters.LockWaitingListKey, waiting);
                }

                var message = "";
                await connection.GetSubscriber().SubscribeAsync(parameters.LockNotifKey, (a, b) => message = b);

                //ACT
                await library.FreeLockAndPop(parameters);

                var locked = await db.StringGetAsync(parameters.LockKey);

                Assert.NotEqual(parameters.Identifier, (string)locked);

                if (priorValue == parameters.Identifier)
                {
                    if (waiting != "")
                    {
                        Assert.Equal(waiting, locked);
                        Assert.NotNull(await db.KeyTimeToLiveAsync(parameters.LockKey));

                        //doesn't work somehow
                        //Assert.NotNull(await db.KeyTimeToLiveAsync(parameters.LockWaitingListKey));
                        //Assert.NotNull(await db.KeyTimeToLiveAsync(parameters.LockWaitingSetKey));

                        await Task.Delay(50);

                        Assert.Equal(message, waiting);

                        await Task.Delay(450);

                        Assert.False(await db.KeyExistsAsync(parameters.LockKey));
                        Assert.False(await db.KeyExistsAsync(parameters.LockWaitingListKey));
                        Assert.False(await db.KeyExistsAsync(parameters.LockWaitingSetKey));
                    }
                    else
                    {
                        Assert.Equal(RedisValue.Null, locked);
                        Assert.False(await db.KeyExistsAsync(parameters.LockKey));
                        Assert.False(await db.KeyExistsAsync(parameters.LockWaitingListKey));
                        Assert.False(await db.KeyExistsAsync(parameters.LockWaitingSetKey));
                    }
                }
            }
            finally
            {
                await ResetKeys(db, parameters);
            }
        }
Пример #9
0
        public async Task GetLockOrSubscribeTests(string priorValue, string waiting, bool expectedResult)
        {
            var connection = ConfigurationHelper.GetNewConnection();
            var library    = new ScriptLibrary(connection);
            var db         = connection.GetDatabase();

            //SETUP
            var root       = Guid.NewGuid().ToString().Substring(0, 8);
            var parameters = new LockLuaParameters("id", new Key(root, "name", TimeSpan.FromMilliseconds(500)));

            try
            {
                await ResetKeys(db, parameters);

                if (!string.IsNullOrEmpty(priorValue))
                {
                    await db.StringSetAsync(parameters.LockKey, priorValue, TimeSpan.FromMilliseconds(parameters.Timeout));
                }

                if (!string.IsNullOrEmpty(waiting))
                {
                    await db.HashSetAsync(parameters.LockWaitingSetKey, waiting, 0);

                    await db.KeyExpireAsync(parameters.LockWaitingSetKey, TimeSpan.FromMilliseconds(parameters.Timeout));

                    await db.ListRightPushAsync(parameters.LockWaitingListKey, waiting);

                    await db.KeyExpireAsync(parameters.LockWaitingListKey, TimeSpan.FromMilliseconds(parameters.Timeout));
                }

                //ACT
                var result = await library.GetLockOrAddToQueue(parameters);

                //ASSERT
                Assert.Equal(expectedResult, result);
                if (expectedResult)
                {
                    Assert.Equal(parameters.Identifier, await db.StringGetAsync(parameters.LockKey));
                    Assert.Equal(!string.IsNullOrEmpty(waiting), await db.KeyExistsAsync(parameters.LockWaitingListKey));
                    Assert.Equal(!string.IsNullOrEmpty(waiting), await db.KeyExistsAsync(parameters.LockWaitingSetKey));
                }
                else
                {
                    var lockKey = (string)await db.StringGetAsync(parameters.LockKey);

                    Assert.NotEmpty(lockKey);
                    Assert.NotEqual(parameters.Identifier, lockKey);
                    Assert.Contains(parameters.Identifier, await db.ListRangeAsync(parameters.LockWaitingListKey));
                    Assert.Contains(parameters.Identifier, (await db.HashGetAllAsync(parameters.LockWaitingSetKey)).Select(e => e.Name));
                }

                //expiration delay
                await Task.Delay((int)parameters.Timeout + 50);

                Assert.False(await db.KeyExistsAsync(parameters.LockKey));
                Assert.False(await db.KeyExistsAsync(parameters.LockWaitingListKey));
                Assert.False(await db.KeyExistsAsync(parameters.LockWaitingSetKey));
            }
            finally
            {
                await ResetKeys(db, parameters);
            }
        }