Esempio n. 1
0
        private static async ValueTask <RedisResult> JoinedParticipantsRepository_RemoveParticipantSafe(
            IKeyValueDatabaseActions actions, string participantId, string participantKey, string conferenceKeyTemplate,
            string connectionId)
        {
            var conferenceId = await actions.GetAsync(participantKey);

            if (conferenceId == null)
            {
                return(RedisResult.Create(RedisValue.Null));
            }

            var conferenceKey      = conferenceKeyTemplate.Replace("*", conferenceId);
            var actualConnectionId = await actions.HashGetAsync(conferenceKey, participantId);

            if (actualConnectionId == connectionId)
            {
                await actions.KeyDeleteAsync(participantKey);

                await actions.HashDeleteAsync(conferenceKey, participantId);

                return(RedisResult.Create(true));
            }

            return(RedisResult.Create(false));
        }
Esempio n. 2
0
        public void ToDictionaryFailsOnMishapenResults()
        {
            var redisArrayResult = RedisResult.Create(
                new RedisValue[] { "one", 1, "two", 2, "three", 3, "four" /* missing 4 */ });

            Assert.Throws <IndexOutOfRangeException>(() => redisArrayResult.ToDictionary(StringComparer.Ordinal));
        }
Esempio n. 3
0
        public void ToDictionaryFailsWithDuplicateKeys()
        {
            var redisArrayResult = RedisResult.Create(
                new RedisValue[] { "banana", 1, "BANANA", 2, "orange", 3, "apple", 4 });

            Assert.Throws <ArgumentException>(() => redisArrayResult.ToDictionary(/* Use default comparer, causes collision of banana */));
        }
Esempio n. 4
0
        public void ToDictionaryWorksWithCustomComparator()
        {
            var redisArrayResult = RedisResult.Create(
                new RedisValue[] { "banana", 1, "BANANA", 2, "orange", 3, "apple", 4 });

            var dict = redisArrayResult.ToDictionary(StringComparer.Ordinal);

            Assert.Equal(4, dict.Count);
            Assert.Equal(1, (RedisValue)dict["banana"]);
            Assert.Equal(2, (RedisValue)dict["BANANA"]);
        }
Esempio n. 5
0
        public void ToDictionaryWorks()
        {
            var redisArrayResult = RedisResult.Create(
                new RedisValue[] { "one", 1, "two", 2, "three", 3, "four", 4 });

            var dict = redisArrayResult.ToDictionary();

            Assert.Equal(4, dict.Count);
            Assert.Equal(1, (RedisValue)dict["one"]);
            Assert.Equal(2, (RedisValue)dict["two"]);
            Assert.Equal(3, (RedisValue)dict["three"]);
            Assert.Equal(4, (RedisValue)dict["four"]);
        }
Esempio n. 6
0
        private static async ValueTask <RedisResult> RoomRepository_SetParticipantRoom(IKeyValueDatabaseActions actions,
                                                                                       string roomMappingKey, string roomListKey, string participantId, string newRoomId)
        {
            var roomExists = await actions.HashExistsAsync(roomListKey, newRoomId);

            if (!roomExists)
            {
                return(RedisResult.Create(false));
            }

            await actions.HashSetAsync(roomMappingKey, participantId, newRoomId);

            return(RedisResult.Create(true));
        }
Esempio n. 7
0
 private static void MockDatabase(Mock <IDatabase> mockDatabase, Func <bool> returns)
 {
     mockDatabase.Setup(d => d.StringSet(It.IsAny <RedisKey>(), It.IsAny <RedisValue>(), It.IsAny <TimeSpan?>(), It.IsAny <When>(), It.IsAny <CommandFlags>()))
     .Returns(returns);
     mockDatabase.Setup(d => d.StringSetAsync(It.IsAny <RedisKey>(), It.IsAny <RedisValue>(), It.IsAny <TimeSpan?>(), It.IsAny <When>(), It.IsAny <CommandFlags>()))
     .Returns(() => Task.Run(returns));
     mockDatabase.Setup(d => d.ScriptEvaluate(It.IsAny <string>(), It.IsAny <RedisKey[]>(), It.IsAny <RedisValue[]>(), It.IsAny <CommandFlags>()))
     .Returns(() => RedisResult.Create(returns()));
     mockDatabase.Setup(d => d.ScriptEvaluateAsync(It.IsAny <string>(), It.IsAny <RedisKey[]>(), It.IsAny <RedisValue[]>(), It.IsAny <CommandFlags>()))
     .Returns(() => Task.Run(() => RedisResult.Create(returns())));
     mockDatabase.Setup(d => d.SortedSetRemove(It.IsAny <RedisKey>(), It.IsAny <RedisValue>(), It.IsAny <CommandFlags>()))
     .Returns(() => (bool)RedisResult.Create(returns()));
     mockDatabase.Setup(d => d.SortedSetRemoveAsync(It.IsAny <RedisKey>(), It.IsAny <RedisValue>(), It.IsAny <CommandFlags>()))
     .Returns(() => Task.Run(() => (bool)RedisResult.Create(returns())));
 }
Esempio n. 8
0
        public void SingleResultConvertibleViaTo()
        {
            var value = RedisResult.Create(123);

            Assert.StrictEqual((int)123, Convert.ToInt32(value));
            Assert.StrictEqual((uint)123U, Convert.ToUInt32(value));
            Assert.StrictEqual((long)123, Convert.ToInt64(value));
            Assert.StrictEqual((ulong)123U, Convert.ToUInt64(value));
            Assert.StrictEqual((byte)123, Convert.ToByte(value));
            Assert.StrictEqual((sbyte)123, Convert.ToSByte(value));
            Assert.StrictEqual((short)123, Convert.ToInt16(value));
            Assert.StrictEqual((ushort)123, Convert.ToUInt16(value));
            Assert.Equal("123", Convert.ToString(value));
            Assert.StrictEqual(123M, Convert.ToDecimal(value));
            Assert.StrictEqual((char)123, Convert.ToChar(value));
            Assert.StrictEqual(123f, Convert.ToSingle(value));
            Assert.StrictEqual(123d, Convert.ToDouble(value));
        }
Esempio n. 9
0
        public void SingleResultConvertibleDirectViaChangeType_TypeCode()
        {
            var value = RedisResult.Create(123);

            Assert.StrictEqual((int)123, Convert.ChangeType(value, TypeCode.Int32));
            Assert.StrictEqual((uint)123U, Convert.ChangeType(value, TypeCode.UInt32));
            Assert.StrictEqual((long)123, Convert.ChangeType(value, TypeCode.Int64));
            Assert.StrictEqual((ulong)123U, Convert.ChangeType(value, TypeCode.UInt64));
            Assert.StrictEqual((byte)123, Convert.ChangeType(value, TypeCode.Byte));
            Assert.StrictEqual((sbyte)123, Convert.ChangeType(value, TypeCode.SByte));
            Assert.StrictEqual((short)123, Convert.ChangeType(value, TypeCode.Int16));
            Assert.StrictEqual((ushort)123, Convert.ChangeType(value, TypeCode.UInt16));
            Assert.Equal("123", Convert.ChangeType(value, TypeCode.String));
            Assert.StrictEqual(123M, Convert.ChangeType(value, TypeCode.Decimal));
            Assert.StrictEqual((char)123, Convert.ChangeType(value, TypeCode.Char));
            Assert.StrictEqual(123f, Convert.ChangeType(value, TypeCode.Single));
            Assert.StrictEqual(123d, Convert.ChangeType(value, TypeCode.Double));
        }
Esempio n. 10
0
        public void SingleResultConvertibleDirectViaChangeType_Type()
        {
            var value = RedisResult.Create(123);

            Assert.StrictEqual((int)123, Convert.ChangeType(value, typeof(int)));
            Assert.StrictEqual((uint)123U, Convert.ChangeType(value, typeof(uint)));
            Assert.StrictEqual((long)123, Convert.ChangeType(value, typeof(long)));
            Assert.StrictEqual((ulong)123U, Convert.ChangeType(value, typeof(ulong)));
            Assert.StrictEqual((byte)123, Convert.ChangeType(value, typeof(byte)));
            Assert.StrictEqual((sbyte)123, Convert.ChangeType(value, typeof(sbyte)));
            Assert.StrictEqual((short)123, Convert.ChangeType(value, typeof(short)));
            Assert.StrictEqual((ushort)123, Convert.ChangeType(value, typeof(ushort)));
            Assert.Equal("123", Convert.ChangeType(value, typeof(string)));
            Assert.StrictEqual(123M, Convert.ChangeType(value, typeof(decimal)));
            Assert.StrictEqual((char)123, Convert.ChangeType(value, typeof(char)));
            Assert.StrictEqual(123f, Convert.ChangeType(value, typeof(float)));
            Assert.StrictEqual(123d, Convert.ChangeType(value, typeof(double)));
        }
Esempio n. 11
0
        private static async ValueTask <RedisResult> JoinedParticipantsRepository_RemoveParticipant(
            IKeyValueDatabaseActions actions, string participantId, string participantKey, string conferenceKeyTemplate)
        {
            var conferenceId = await actions.GetAsync(participantKey);

            if (conferenceId == null)
            {
                return(RedisResult.Create(RedisValue.Null));
            }

            await actions.KeyDeleteAsync(participantKey);

            var conferenceKey        = conferenceKeyTemplate.Replace("*", conferenceId);
            var previousConnectionId = await actions.HashGetAsync(conferenceKey, participantId);

            await actions.HashDeleteAsync(conferenceKey, participantId);

            return(RedisResult.Create(new RedisValue[] { conferenceId, previousConnectionId }));
        }
Esempio n. 12
0
 /// <summary>
 /// 命令
 /// </summary>
 /// <param name="command"></param>
 /// <param name="args"></param>
 public static ValueTask <RedisResult> ExecuteAsy(string command, int db = 0, params object[] args)
 {
     try
     {
         db = db == 0 ? _db : db;
         if (!string.IsNullOrEmpty(command))
         {
             return(new ValueTask <RedisResult>(Context.GetDatabase(db).ExecuteAsync(command, args)));
         }
         else
         {
             return(new ValueTask <RedisResult>(RedisResult.Create(RedisValue.Null)));
         }
     }
     catch (RedisException ex)
     {
         SaveLog(ex, "Execute", command);
         return(new ValueTask <RedisResult>(RedisResult.Create(RedisValue.Null)));
     }
 }
Esempio n. 13
0
        public void ToDictionaryWorksWhenNested()
        {
            var redisArrayResult = RedisResult.Create(
                new []
            {
                RedisResult.Create((RedisValue)"one"),
                RedisResult.Create(new RedisValue[] { "two", 2, "three", 3 }),

                RedisResult.Create((RedisValue)"four"),
                RedisResult.Create(new RedisValue[] { "five", 5, "six", 6 }),
            });

            var dict       = redisArrayResult.ToDictionary();
            var nestedDict = dict["one"].ToDictionary();

            Assert.Equal(2, dict.Count);
            Assert.Equal(2, nestedDict.Count);
            Assert.Equal(2, (RedisValue)nestedDict["two"]);
            Assert.Equal(3, (RedisValue)nestedDict["three"]);
        }