コード例 #1
0
        public void GetRecentMessagesReturnsTheLastNMessages()
        {
            var cache = new RecentMessageCache(10);

            // Prime the cache
            cache.Add("room", new List<MessageViewModel>());

            for (int i = 0; i < 100; i++)
            {
                cache.Add(MakeMessage("dfowler", "room", "Hello_" + i));
            }

            var messages = cache.GetRecentMessages("room").ToList();

            Assert.Equal(10, messages.Count);

            // The impl of the message store allocates bucket sizes optimized for the GC so it's not exact
            for (int i = 90, j = 0; i <= 99; i++, j++)
            {
                Assert.Equal("Hello_" + i, messages[j].Content);
            }
        }
コード例 #2
0
        public void ThreadSafety()
        {
            var cache = new RecentMessageCache(10);
            var initial = new List<MessageViewModel>();
            var wh = new ManualResetEventSlim();

            for (int i = 0; i < 5; i++)
            {
                initial.Add(new MessageViewModel(MakeMessage("dfowler", "room", "Hello_" + i)));
            }

            Task.Run(() =>
            {
                Thread.Sleep(100);

                for (int i = 0; i < 5; i++)
                {
                    cache.Add(MakeMessage("john", "room", "john_" + i));
                }

                wh.Set();
            });

            cache.Add("room", initial);

            wh.Wait();

            var messages = cache.GetRecentMessages("room").ToList();

            Assert.Equal(10, messages.Count);

            Assert.Equal("john_0", messages[5].Content);
            Assert.Equal("john_1", messages[6].Content);
            Assert.Equal("john_2", messages[7].Content);
            Assert.Equal("john_3", messages[8].Content);
            Assert.Equal("john_4", messages[9].Content);
        }