public static async Task <AsyncRedisClient> CreateAndConnectAsync(ConnectionSettings settings)
        {
            var client = new AsyncRedisClient();
            await client.Connect(settings);

            return(client);
        }
Пример #2
0
        public async Task Test_MultipleSubscribe()
        {
            var dutPublisher  = new AsyncRedisClient();
            var dutSubscriber = new AsyncRedisSubscriptionClient();

            await dutPublisher.Connect(LocalHostDefaultPort.AsConnectionSettings());

            await dutSubscriber.Connect(LocalHostDefaultPort.AsConnectionSettings());

            var received = new List <Tuple <string, string> >();

            dutSubscriber.OnMessageReceived += (ch, msg) =>
            {
                received.Add(Tuple.Create(ch, msg));
            };

            await dutSubscriber.Subscribe(new[] { Channel, OtherChannel });

            await dutPublisher.Publish(Channel, Message);

            Thread.Sleep(100);

            await dutPublisher.Publish(OtherChannel, OtherMessage);

            Thread.Sleep(100);

            Assert.AreEqual(2, received.Count);
            Assert.AreEqual(Channel, received[0].Item1);
            Assert.AreEqual(Message, received[0].Item2);
            Assert.AreEqual(OtherChannel, received[1].Item1);
            Assert.AreEqual(OtherMessage, received[1].Item2);
        }
Пример #3
0
        public async Task Test_Unsubscribe()
        {
            var dutPublisher  = new AsyncRedisClient();
            var dutSubscriber = new AsyncRedisSubscriptionClient();

            await dutPublisher.Connect(LocalHostDefaultPort.AsConnectionSettings());

            await dutSubscriber.Connect(LocalHostDefaultPort.AsConnectionSettings());

            string receivedChannel = null;
            string receivedMessage = null;

            dutSubscriber.OnMessageReceived += (ch, msg) =>
            {
                receivedChannel = ch;
                receivedMessage = msg;
            };

            await dutSubscriber.Subscribe(Channel);

            await dutPublisher.Publish(Channel, Message);

            Thread.Sleep(100);

            await dutSubscriber.Unsubscribe(Channel);

            await dutPublisher.Publish(Channel, SecondMessage);

            Thread.Sleep(100);

            Assert.AreEqual(Channel, receivedChannel);
            Assert.AreEqual(Message, receivedMessage);
        }
        public async Task Cleanup()
        {
            try
            {
                var dut = new AsyncRedisClient();
                await dut.Connect(LocalHostDefaultPort.AsConnectionSettings());

                await dut.Select(0);

                await dut.Del(Key);

                await dut.Del(Key2);

                await dut.Select(7);

                await dut.Del(Key);

                await dut.Del(Key2);
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error during cleanup: {ex}");
                throw;
            }
        }
Пример #5
0
        public async Task TestScriptLoadAndRun_ListInList()
        {
            var dut = new AsyncRedisClient();

            await dut.Connect(LocalHostDefaultPort.AsConnectionSettings());

            var sha = await dut.LoadScript(ScriptWithListInListResult);

            Console.WriteLine(sha);

            var result = (await dut.EvalSha(sha, new string[0])).ToArray();

            Assert.AreEqual(2, result?.Length);

            var embeddedArray1 = result[0] as object[];
            var embeddedArray2 = result[1] as object[];

            Assert.AreEqual("10", embeddedArray1?[0]?.ToString());
            Assert.AreEqual("20", embeddedArray1?[1]?.ToString());
            Assert.AreEqual("30", embeddedArray1?[2]?.ToString());
            Assert.AreEqual("40", embeddedArray1?[3]?.ToString());

            Assert.AreEqual("10", embeddedArray2?[0]?.ToString());
            Assert.AreEqual("20", embeddedArray2?[1]?.ToString());
            Assert.AreEqual("30", embeddedArray2?[2]?.ToString());
            Assert.AreEqual("40", embeddedArray2?[3]?.ToString());
        }
        public static AsyncRedisClient CreateAndConnect(ConnectionSettings settings)
        {
            var client = new AsyncRedisClient();

            client.Connect(settings).GetAwaiter().GetResult();

            return(client);
        }
Пример #7
0
        public async Task Cleanup()
        {
            var dut = new AsyncRedisClient();

            await dut.Connect(LocalHostDefaultPort.AsConnectionSettings());

            await dut.Del(ListKey);
        }
Пример #8
0
        public async Task Test_GetAllHash_Null()
        {
            var dut = new AsyncRedisClient();
            await dut.Connect(LocalHostDefaultPort.AsConnectionSettings());

            var res = await dut.HGetAll(_keys[3]);

            Assert.AreEqual(0, res.Count);
        }
Пример #9
0
        public static void Main()
        {
            var client             = new AsyncRedisClient();
            var connectionSettings = new ConnectionSettings("127.0.0.1", 6379);

            FailIfException(async() =>
            {
                Console.WriteLine($"Trying to connect to {connectionSettings.Address}:{connectionSettings.Port}...");
                await client.Connect(connectionSettings);
                Pass();
            }).GetAwaiter().GetResult();

            FailIfException(async() =>
            {
                Console.WriteLine("Trying simple write...");
                await client.Set(Key, Value);
                Pass();
            }).GetAwaiter().GetResult();

            FailIfException(async() =>
            {
                Console.WriteLine("Trying simple read...");
                var result = await client.Get(Key);
                if (result == Value)
                {
                    Pass();
                }
                else
                {
                    Fail("Read incorrect value from the server");
                }
            }).GetAwaiter().GetResult();

            FailIfException(async() =>
            {
                Console.WriteLine("Trying DbSize...");
                var result = await client.DbSize();

                if (result == 1)
                {
                    Pass();
                }
                else
                {
                    Fail("Read incorrect db size from the server");
                }
            }).GetAwaiter().GetResult();

            FailIfException(async() =>
            {
                Console.WriteLine("Trying delete...");
                await client.Del(Key);
                Pass();
            }).GetAwaiter().GetResult();
        }
Пример #10
0
        public async Task Test_GeHash_Null()
        {
            var dut = new AsyncRedisClient();
            await dut.Connect(LocalHostDefaultPort.AsConnectionSettings());

            var res = (await dut.HMGet(_keys[3], new[] { Field1, Field2 })).ToList();

            Assert.AreEqual(2, res.Count);
            Assert.IsNull(res[0]);
            Assert.IsNull(res[1]);
        }
Пример #11
0
        public async Task Test_Set_Get()
        {
            var dut = new AsyncRedisClient();
            await dut.Connect(LocalHostDefaultPort.AsConnectionSettings());

            await dut.Set(Key, Value);

            var res = await dut.Get(Key);

            Assert.AreEqual(Value, res);
        }
Пример #12
0
        public async Task Test_RPush()
        {
            var dut = new AsyncRedisClient();
            await dut.Connect(LocalHostDefaultPort.AsConnectionSettings());

            await dut.RPush(ListKey, ListItems);

            var result = (await dut.LRange(ListKey, 0, 100)).ToList();

            Assert.IsTrue(ListItems.SequenceEqual(result));
        }
Пример #13
0
        public async Task Test_SetAndGetHash()
        {
            var dut = new AsyncRedisClient();
            await dut.Connect(LocalHostDefaultPort.AsConnectionSettings());

            await dut.HSet(_keys[0], Field1, Value1);

            var res = await dut.HGet(_keys[0], Field1);

            Assert.AreEqual(Value1, res);
        }
Пример #14
0
        public async Task Test_LRange()
        {
            var dut = new AsyncRedisClient();
            await dut.Connect(LocalHostDefaultPort.AsConnectionSettings());

            await dut.RPush(ListKey, ListItems);

            var result = (await dut.LRange(ListKey, 1, 2)).ToList();

            Assert.AreEqual(2, result.Count);
            Assert.AreEqual(ListItems[1], result[0]);
            Assert.AreEqual(ListItems[2], result[1]);
        }
Пример #15
0
        public async Task TestScriptLoadAndRun_SingleString()
        {
            var dut = new AsyncRedisClient();

            await dut.Connect(LocalHostDefaultPort.AsConnectionSettings());

            var sha = await dut.LoadScript(ScriptWithoutParameter);

            Assert.AreEqual(ShaLength, sha.Length);

            var res       = (await dut.EvalSha(sha, new string[0])).ToList();
            var resString = res[0];

            Assert.AreEqual("it works", resString);
        }
Пример #16
0
        public async Task ConnectToKnownHostWrongPort_ThrowsException()
        {
            Exception thrownException = null;
            var       underTest       = new AsyncRedisClient();

            try
            {
                await underTest.Connect(LocalHostPort7000.AsConnectionSettings());
            }
            catch (Exception ex)
            {
                thrownException = ex;
            }

            thrownException.Should().NotBeNull().And.BeAssignableTo <SocketException>();
        }
Пример #17
0
        public async Task Connect_ConnectsSuccessfully()
        {
            Exception thrownException = null;
            var       underTest       = new AsyncRedisClient();

            try
            {
                await underTest.Connect(LocalHostDefaultPort.AsConnectionSettings());
            }
            catch (Exception ex)
            {
                thrownException = ex;
            }

            thrownException.Should().BeNull();
        }
Пример #18
0
        public async Task Cleanup()
        {
            try
            {
                var dut = new AsyncRedisClient();
                await dut.Connect(LocalHostDefaultPort.AsConnectionSettings());

                await dut.Select(0);

                _keys.ForEach(k => dut.Del(k).GetAwaiter().GetResult());
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error during cleanup: {ex}");
                throw;
            }
        }
Пример #19
0
        public async Task Test_GetMultipleHash()
        {
            var dut = new AsyncRedisClient();
            await dut.Connect(LocalHostDefaultPort.AsConnectionSettings());

            await dut.HSet(_keys[2], Field1, Value1);

            await dut.HSet(_keys[2], Field2, Value2);

            await dut.HSet(_keys[2], Field3, Value3);

            var res = (await dut.HMGet(_keys[2], new[] { Field1, Field2, Field3 })).ToList();

            Assert.AreEqual(Value1, res[0]);
            Assert.AreEqual(Value2, res[1]);
            Assert.AreEqual(Value3, res[2]);
        }
Пример #20
0
        public async Task ConnectCalledTwice_ThrowsException()
        {
            Exception thrownException = null;
            var       underTest       = new AsyncRedisClient();

            try
            {
                await underTest.Connect(LocalHostDefaultPort.AsConnectionSettings());

                await underTest.Connect(LocalHostDefaultPort.AsConnectionSettings());
            }
            catch (Exception ex)
            {
                thrownException = ex;
            }

            thrownException.Should().NotBeNull().And.BeOfType <InvalidOperationException>();
        }
Пример #21
0
        public async Task TestUnconnectedClient_GetThrowsException()
        {
            Exception thrownException = null;

            var dut = new AsyncRedisClient();

            try
            {
                await dut.Get(Key);
            }
            catch (Exception ex)
            {
                thrownException = ex;
            }

            Assert.IsNotNull(thrownException);
            Assert.IsInstanceOfType(thrownException, typeof(InvalidOperationException));
        }
Пример #22
0
        public async Task Test_Ping()
        {
            var dut = new AsyncRedisClient();
            await dut.Connect(LocalHostDefaultPort.AsConnectionSettings());

            Exception thrownException = null;

            try
            {
                await dut.Ping();
            }
            catch (Exception ex)
            {
                thrownException = ex;
            }

            Assert.IsNull(thrownException);
        }
Пример #23
0
        public async Task TestScriptLoadAndRun_List()
        {
            var dut = new AsyncRedisClient();

            await dut.Connect(LocalHostDefaultPort.AsConnectionSettings());

            var sha = await dut.LoadScript(ScriptWithListResult);

            Console.WriteLine(sha);

            var res = await dut.EvalSha(sha, new string[0]);

            var resList = res as object[];

            Assert.AreEqual(2, resList?.Length);
            Assert.AreEqual("one", resList?[0] as string);
            Assert.AreEqual("two", resList?[1] as string);
        }
Пример #24
0
        public async Task Test_GetAllHash()
        {
            var dut = new AsyncRedisClient();
            await dut.Connect(LocalHostDefaultPort.AsConnectionSettings());

            await dut.HSet(_keys[3], Field1, Value1);

            await dut.HSet(_keys[3], Field2, Value2);

            await dut.HSet(_keys[3], Field3, Value3);

            var res = await dut.HGetAll(_keys[3]);

            Assert.AreEqual(3, res.Count);
            Assert.AreEqual(Value1, res[Field1]);
            Assert.AreEqual(Value2, res[Field2]);
            Assert.AreEqual(Value3, res[Field3]);
        }
Пример #25
0
        public async Task Test_ConnectedEvent()
        {
            var dut = new AsyncRedisClient();

            string result = null;

            dut.OnConnected += async c =>
            {
                await c.Set(Key, Value);

                result = await dut.Get(Key);
            };

            await dut.Connect(LocalHostDefaultPort.AsConnectionSettings());

            await Task.Delay(1250);

            Assert.AreEqual(Value, result);
        }
Пример #26
0
        public async Task TestWrongOperation_GetThrowsException()
        {
            Exception thrownException = null;

            var dut = new AsyncRedisClient();
            await dut.Connect(LocalHostDefaultPort.AsConnectionSettings());

            try
            {
                await dut.SAdd(Key, Value);

                await dut.Get(Key);
            }
            catch (Exception ex)
            {
                thrownException = ex;
            }

            Assert.IsNotNull(thrownException);
            Assert.IsInstanceOfType(thrownException, typeof(RedisException));
        }
Пример #27
0
        public async Task TestWrongOperation_LRangeThrowsException()
        {
            Exception thrownException = null;

            var dut = new AsyncRedisClient();
            await dut.Connect(LocalHostDefaultPort.AsConnectionSettings());

            try
            {
                await dut.Set(ListKey, ListItems[0]);

                await dut.LRange(ListKey, 0, 1);
            }
            catch (Exception ex)
            {
                thrownException = ex;
            }

            Assert.IsNotNull(thrownException);
            Assert.IsInstanceOfType(thrownException, typeof(RedisException));
        }
Пример #28
0
        public async Task Test_SetMultipleHash()
        {
            var dut = new AsyncRedisClient();
            await dut.Connect(LocalHostDefaultPort.AsConnectionSettings());

            var additionalFields = new Dictionary <string, string>
            {
                { Field1, Value1 },
                { Field2, Value2 },
                { Field3, Value3 }
            };

            await dut.HMSet(_keys[1], additionalFields);

            var res1 = await dut.HGet(_keys[1], Field1);

            var res2 = await dut.HGet(_keys[1], Field2);

            var res3 = await dut.HGet(_keys[1], Field3);

            Assert.AreEqual(Value1, res1);
            Assert.AreEqual(Value2, res2);
            Assert.AreEqual(Value3, res3);
        }