public async Task UT_PubSub_SubscribeMultipleTypesAsync(RedisContext context)
        {
            var ch        = $"{TestContext.CurrentContext.Test.MethodName}-{context.GetSerializer().GetType().Name}";
            var users     = GetUsers();
            int objCount  = 0;
            int iDtoCount = 0;
            await context.PubSub.SubscribeAsync <object>(ch, (c, o) => objCount ++);

            await context.PubSub.SubscribeAsync <IDto>(ch, (c, o) => iDtoCount ++);

            foreach (var t in users)
            {
                await context.PubSub.PublishAsync(ch, t);
            }
            await context.PubSub.PublishAsync(ch, new Exception("a different object type"));

            await context.PubSub.PublishAsync(ch, users[0].Deparments[0]);

            await context.PubSub.PublishAsync(ch, "some string");

            await Task.Delay(500);

            Assert.AreEqual(users.Count + 3, objCount);
            Assert.AreEqual(users.Count + 1, iDtoCount);
            await context.PubSub.UnsubscribeAsync(ch);
        }
        public void UT_PubSub_SubscribeWilcards(RedisContext context)
        {
            var ch       = $"{TestContext.CurrentContext.Test.MethodName}-{context.GetSerializer().GetType().Name}";
            var users    = GetUsers();
            var channels = new List <string>();
            var objects  = new List <User>();

            context.PubSub.Subscribe <User>(ch + ".*", (c, o) =>
            {
                channels.Add(c);
                objects.Add(o);
            });
            int user0count = 0;

            context.PubSub.Subscribe <User>(ch + ".user0", (c, o) =>
            {
                user0count++;
            });
            context.PubSub.Publish(ch + ".user0", users[0]);
            Thread.Sleep(100);
            Thread.Sleep(100);
            context.PubSub.Publish(ch + ".user1", users[1]);
            Thread.Sleep(100);
            Thread.Sleep(100);
            Assert.AreEqual(2, channels.Count);
            Assert.AreEqual(users[0].Id, objects[0].Id);
            Assert.AreEqual(users[1].Id, objects[1].Id);
            Assert.AreEqual(1, user0count);

            context.PubSub.Unsubscribe(ch + ".*");
            Thread.Sleep(1500);
            context.PubSub.Publish(ch + ".user2", users[2]);
            Assert.AreEqual(2, channels.Count);
            Assert.AreEqual(1, user0count);
        }
        public void UT_PubSub_SingleUnsubscribe(RedisContext context)
        {
            var ch        = $"{TestContext.CurrentContext.Test.MethodName}-{context.GetSerializer().GetType().Name}";
            var users     = GetUsers();
            var usersList = new List <User>();

            context.PubSub.Subscribe <User>(ch, (c, o) => usersList.Add(o));
            foreach (var t in users)
            {
                context.PubSub.Publish(ch, t);
            }
            Thread.Sleep(500);
            Assert.AreEqual(users.Count, usersList.Count);
            context.PubSub.Unsubscribe(ch);
            context.PubSub.Publish(ch, users[0]);
            Assert.AreEqual(users.Count, usersList.Count);
        }
        public async Task UT_PubSub_SingleUnsubscribeAsync(RedisContext context)
        {
            var ch        = $"{TestContext.CurrentContext.Test.MethodName}-{context.GetSerializer().GetType().Name}";
            var users     = GetUsers();
            var usersList = new List <User>();
            await context.PubSub.SubscribeAsync <User>(ch, (c, o) => usersList.Add(o));

            foreach (var t in users)
            {
                await context.PubSub.PublishAsync(ch, t);
            }
            await Task.Delay(500);

            Assert.AreEqual(users.Count, usersList.Count);
            await context.PubSub.UnsubscribeAsync(ch);

            await context.PubSub.PublishAsync(ch, users[0]);

            Assert.AreEqual(users.Count, usersList.Count);
        }
        public void UT_PubSub_SubscribeMultipleTypes(RedisContext context)
        {
            var ch        = $"{TestContext.CurrentContext.Test.MethodName}-{context.GetSerializer().GetType().Name}";
            var users     = GetUsers();
            int objCount  = 0;
            int iDtoCount = 0;

            context.PubSub.Subscribe <object>(ch, (c, o) => objCount++);
            context.PubSub.Subscribe <IDto>(ch, (c, o) => iDtoCount++);
            foreach (var t in users)
            {
                context.PubSub.Publish(ch, t);
            }
            context.PubSub.Publish(ch, new Exception("a different object type"));
            context.PubSub.Publish(ch, users[0].Deparments[0]);
            context.PubSub.Publish(ch, "some string");
            Thread.Sleep(500);
            Assert.AreEqual(users.Count + 3, objCount);
            Assert.AreEqual(users.Count + 1, iDtoCount);
            context.PubSub.Unsubscribe(ch);
        }
        public async Task UT_PubSub_SubscribeWilcardsAsync(RedisContext context)
        {
            var ch       = $"{TestContext.CurrentContext.Test.MethodName}-{context.GetSerializer().GetType().Name}";
            var users    = GetUsers();
            var channels = new List <string>();
            var objects  = new List <User>();
            await context.PubSub.SubscribeAsync <User>(ch + ".*", (c, o) =>
            {
                channels.Add(c);
                objects.Add(o);
            });

            int user0count = 0;
            await context.PubSub.SubscribeAsync <User>(ch + ".user0", (c, o) =>
            {
                user0count++;
            });

            await context.PubSub.PublishAsync(ch + ".user0", users[0]);

            await Task.Delay(200);

            await context.PubSub.PublishAsync(ch + ".user1", users[1]);

            await Task.Delay(200);

            Assert.AreEqual(2, channels.Count);
            Assert.AreEqual(users[0].Id, objects[0].Id);
            Assert.AreEqual(users[1].Id, objects[1].Id);
            Assert.AreEqual(1, user0count);

            await context.PubSub.UnsubscribeAsync(ch + ".*");

            await Task.Delay(1500);

            await context.PubSub.PublishAsync(ch + ".user2", users[2]);

            Assert.AreEqual(2, channels.Count);
            Assert.AreEqual(1, user0count);
        }
        public void UT_PubSub_SingleSubscribe(RedisContext context)
        {
            var ch        = $"{TestContext.CurrentContext.Test.MethodName}-{context.GetSerializer().GetType().Name}";
            var users     = GetUsers();
            var usersList = new List <User>();
            var locker    = new object();

            context.PubSub.Subscribe <User>(ch, (c, o) =>
            {
                lock (locker)
                {
                    usersList.Add(o);
                }
            });
            foreach (var t in users)
            {
                context.PubSub.Publish(ch, t);
            }
            Thread.Sleep(500);
            Assert.AreEqual(users.Count, usersList.Count);
            Assert.IsTrue(users.All(u => usersList.Any(ul => ul.Id == u.Id)));
            context.PubSub.Unsubscribe(ch);
        }
Beispiel #8
0
        public async Task UT_CacheSerializer_Async(RedisContext context)
        {
            var kss      = "short:string";
            var kch      = "char";
            var kds      = "decimal";
            var kls      = "long:string";
            var kpBool   = "primitive:bool";
            var kpInt    = "primitive:int";
            var kpLong   = "primitive:long";
            var kpSingle = "primitive:single";
            var kpUInt16 = "primitive:uint16";
            var kpUInt32 = "primitive:uint32";
            var kpUInt64 = "primitive:uint64";
            var kby      = "primitive:byte";
            var ksby     = "primitive:sbyte";
            var ki16     = "primitive:int16";
            var ki32     = "primitive:int32";
            var kuip     = "primitive:uintptr";
            var kdbl     = "primitive:double";
            var kdt      = "datetime";

            context.Cache.Remove(new[] { kss, kls, kpBool, kpInt, kpLong, kpSingle, kpUInt16, kpUInt32, kpUInt64,
                                         kch, kds, kdt, kby, ksby, ki16, ki32, kuip, kdbl });
            var ss = "this is a short string";
            var ls = @"UTF-8 is a character encoding capable of encoding all possible characters, or code points, in Unicode.
                       The encoding is variable-length and uses 8-bit code units. It was designed for backward compatibility with ASCII, and to avoid the complications of endianness and byte order marks in the alternative UTF-16 and UTF-32 encodings. The name is derived from: Universal Coded Character Set + Transformation Format—8-bit.";
            await context.Cache.SetObjectAsync(kss, ss);

            await context.Cache.SetObjectAsync(kls, ls);

            await context.Cache.SetObjectAsync <char>(kch, 'c');

            await context.Cache.SetObjectAsync <Byte>(kby, Byte.MaxValue);

            await context.Cache.SetObjectAsync <SByte>(ksby, SByte.MaxValue);

            await context.Cache.SetObjectAsync <Int16>(ki16, Int16.MaxValue);

            await context.Cache.SetObjectAsync <Int32>(ki32, Int32.MaxValue);

            await context.Cache.SetObjectAsync <UIntPtr>(kuip, UIntPtr.Zero);

            await context.Cache.SetObjectAsync <Double>(kdbl, Double.NegativeInfinity);

            await context.Cache.SetObjectAsync <bool>(kpBool, true);

            await context.Cache.SetObjectAsync <int>(kpInt, int.MaxValue);

            await context.Cache.SetObjectAsync <Int64>(kpLong, Int64.MaxValue);

            var now = DateTime.Now;
            await context.Cache.SetObjectAsync <DateTime>(kdt, now);

            await context.Cache.SetObjectAsync <Single>(kpSingle, Single.MaxValue);

            await context.Cache.SetObjectAsync <UInt16>(kpUInt16, UInt16.MaxValue);

            await context.Cache.SetObjectAsync <UInt32>(kpUInt32, UInt32.MaxValue);

            await context.Cache.SetObjectAsync <UInt64>(kpUInt64, UInt64.MaxValue);

            await context.Cache.SetObjectAsync <decimal>(kds, decimal.MaxValue);

            var    ss_      = context.Cache.GetObject <string>(kss);
            var    ls_      = context.Cache.GetObject <string>(kls);
            var    pInt_    = context.Cache.GetObject <int>(kpInt);
            var    pLong_   = context.Cache.GetObject <Int64>(kpLong);
            var    pSingle_ = context.Cache.GetObject <Single>(kpSingle);
            var    pUint16_ = context.Cache.GetObject <UInt16>(kpUInt16);
            var    pUint32_ = context.Cache.GetObject <UInt32>(kpUInt32);
            UInt64 pUint64_ = 0;

            if (context.GetSerializer().GetType() != typeof(JsonSerializer))
            {
                pUint64_ = context.Cache.GetObject <UInt64>(kpUInt64);
            }
            var kch_    = context.Cache.GetObject <char>(kch);
            var kds_    = context.Cache.GetObject <decimal>(kds);
            var kdt_    = context.Cache.GetObject <DateTime>(kdt);
            var kby_    = context.Cache.GetObject <Byte>(kby);
            var ksby_   = context.Cache.GetObject <SByte>(ksby);
            var ki16_   = context.Cache.GetObject <Int16>(ki16);
            var ki32_   = context.Cache.GetObject <Int32>(ki32);
            var kuip_   = context.Cache.GetObject <UIntPtr>(kuip);
            var kdbl_   = context.Cache.GetObject <Double>(kdbl);
            var kpBool_ = context.Cache.GetObject <bool>(kpBool);

            Assert.AreEqual(ss, ss_);
            Assert.AreEqual(ls, ls_);
            Assert.AreEqual('c', kch_);
            Assert.IsTrue((now - kdt_).TotalMilliseconds < 0.001);
            Assert.AreEqual(decimal.MaxValue, kds_);
            Assert.AreEqual(Byte.MaxValue, kby_);
            Assert.AreEqual(SByte.MaxValue, ksby_);
            Assert.AreEqual(Int16.MaxValue, ki16_);
            Assert.AreEqual(Int32.MaxValue, ki32_);
            Assert.AreEqual(UIntPtr.Zero, kuip_);
            Assert.AreEqual(Double.NegativeInfinity, kdbl_);
            Assert.AreEqual(int.MaxValue, pInt_);
            Assert.AreEqual(long.MaxValue, pLong_);
            Assert.AreEqual(Single.Parse(Single.MaxValue.ToString("F")), Single.Parse(pSingle_.ToString("F")));
            Assert.AreEqual(UInt16.MaxValue, pUint16_);
            Assert.AreEqual(UInt32.MaxValue, pUint32_);
            if (context.GetSerializer().GetType() != typeof(JsonSerializer))
            {
                Assert.AreEqual(UInt64.MaxValue, pUint64_);
            }
            Assert.AreEqual(true, kpBool_);
            context.Cache.Remove(new[] { kss, kls, kpBool, kpInt, kpLong, kpSingle, kpUInt16, kpUInt32, kpUInt64,
                                         kch, kds, kdt, kby, ksby, ki16, ki32, kuip, kdbl });
        }
Beispiel #9
0
        public async Task UT_Context_Dispose_Async(RedisContext context)
        {
            var ctx = new RedisContext(context.GetConnectionMultiplexer().Configuration, context.GetSerializer());
            await ctx.Cache.SetObjectAsync("key", "value");

            ctx.Dispose();
            await context.Cache.RemoveAsync("key");

            Assert.ThrowsAsync <ObjectDisposedException>(async() => await ctx.Cache.SetObjectAsync("key", "value2"));
        }