예제 #1
0
        public void Supports_different_operation_types_in_same_pipeline()
        {
            var incrementResults = new List <long>();
            var collectionCounts = new List <long>();
            var containsItem     = false;

            var typedList      = typedClient.Lists[ListKey];
            var typedSet       = typedClient.Sets[SetKey];
            var typedSortedSet = typedClient.SortedSets[SortedSetKey];

            Assert.That(typedClient.GetValue(Key), Is.Null);
            using (var pipeline = typedClient.CreatePipeline())
            {
                pipeline.QueueCommand(r => r.IncrementValue(Key), intResult => incrementResults.Add(intResult));
                pipeline.QueueCommand(r => r.AddItemToList(typedList, modelFactory.CreateInstance(1)));
                pipeline.QueueCommand(r => r.AddItemToList(typedList, modelFactory.CreateInstance(2)));
                pipeline.QueueCommand(r => r.AddItemToSet(typedSet, modelFactory.CreateInstance(3)));
                pipeline.QueueCommand(r => r.SetContainsItem(typedSet, modelFactory.CreateInstance(3)), b => containsItem = b);
                pipeline.QueueCommand(r => r.AddItemToSortedSet(typedSortedSet, modelFactory.CreateInstance(4)));
                pipeline.QueueCommand(r => r.AddItemToSortedSet(typedSortedSet, modelFactory.CreateInstance(5)));
                pipeline.QueueCommand(r => r.AddItemToSortedSet(typedSortedSet, modelFactory.CreateInstance(6)));
                pipeline.QueueCommand(r => r.GetListCount(typedList), intResult => collectionCounts.Add(intResult));
                pipeline.QueueCommand(r => r.GetSetCount(typedSet), intResult => collectionCounts.Add(intResult));
                pipeline.QueueCommand(r => r.GetSortedSetCount(typedSortedSet), intResult => collectionCounts.Add(intResult));
                pipeline.QueueCommand(r => r.IncrementValue(Key), intResult => incrementResults.Add(intResult));

                pipeline.Flush();
            }

            Assert.That(containsItem, Is.True);
            Assert.That(Redis.GetValue(Key), Is.EqualTo("2"));
            Assert.That(incrementResults, Is.EquivalentTo(new List <int> {
                1, 2
            }));
            Assert.That(collectionCounts, Is.EquivalentTo(new List <int> {
                2, 1, 3
            }));

            modelFactory.AssertListsAreEqual(typedList.GetAll(), new List <Shipper>
            {
                modelFactory.CreateInstance(1), modelFactory.CreateInstance(2)
            });

            Assert.That(typedSet.GetAll(), Is.EquivalentTo(new List <Shipper>
            {
                modelFactory.CreateInstance(3)
            }));

            modelFactory.AssertListsAreEqual(typedSortedSet.GetAll(), new List <Shipper>
            {
                modelFactory.CreateInstance(4), modelFactory.CreateInstance(5), modelFactory.CreateInstance(6)
            });
        }
예제 #2
0
        public async Task Supports_different_operation_types_in_same_transaction()
        {
            var incrementResults = new List <long>();
            var collectionCounts = new List <long>();
            var containsItem     = false;

            var typedList      = typedClient.Lists[ListKey];
            var typedSet       = typedClient.Sets[SetKey];
            var typedSortedSet = typedClient.SortedSets[SortedSetKey];

            Assert.That(await typedClient.GetValueAsync(Key), Is.Null);
            await using (var trans = await typedClient.CreateTransactionAsync())
            {
                trans.QueueCommand(r => r.IncrementValueAsync(Key), intResult => incrementResults.Add(intResult));
                trans.QueueCommand(r => r.AddItemToListAsync(typedList, modelFactory.CreateInstance(1)));
                trans.QueueCommand(r => r.AddItemToListAsync(typedList, modelFactory.CreateInstance(2)));
                trans.QueueCommand(r => r.AddItemToSetAsync(typedSet, modelFactory.CreateInstance(3)));
                trans.QueueCommand(r => r.SetContainsItemAsync(typedSet, modelFactory.CreateInstance(3)), b => containsItem = b);
                trans.QueueCommand(r => r.AddItemToSortedSetAsync(typedSortedSet, modelFactory.CreateInstance(4)));
                trans.QueueCommand(r => r.AddItemToSortedSetAsync(typedSortedSet, modelFactory.CreateInstance(5)));
                trans.QueueCommand(r => r.AddItemToSortedSetAsync(typedSortedSet, modelFactory.CreateInstance(6)));
                trans.QueueCommand(r => r.GetListCountAsync(typedList), intResult => collectionCounts.Add(intResult));
                trans.QueueCommand(r => r.GetSetCountAsync(typedSet), intResult => collectionCounts.Add(intResult));
                trans.QueueCommand(r => r.GetSortedSetCountAsync(typedSortedSet), intResult => collectionCounts.Add(intResult));
                trans.QueueCommand(r => r.IncrementValueAsync(Key), intResult => incrementResults.Add(intResult));

                await trans.CommitAsync();
            }

            Assert.That(containsItem, Is.True);
            Assert.That(await RedisAsync.GetValueAsync(Key), Is.EqualTo("2"));
            Assert.That(incrementResults, Is.EquivalentTo(new List <int> {
                1, 2
            }));
            Assert.That(collectionCounts, Is.EquivalentTo(new List <int> {
                2, 1, 3
            }));

            modelFactory.AssertListsAreEqual(await typedList.GetAllAsync(), new List <Shipper>
            {
                modelFactory.CreateInstance(1), modelFactory.CreateInstance(2)
            });

            Assert.That(await typedSet.GetAllAsync(), Is.EquivalentTo(new List <Shipper>
            {
                modelFactory.CreateInstance(3)
            }));

            modelFactory.AssertListsAreEqual(await typedSortedSet.GetAllAsync(), new List <Shipper>
            {
                modelFactory.CreateInstance(4), modelFactory.CreateInstance(5), modelFactory.CreateInstance(6)
            });
        }