示例#1
0
        public void TryGetValueNullResult()
        {
            // arrange
            var cacheSize = 10;
            var cache     = new TaskCache(cacheSize);
            var key       = "Foo";

            // act
            var result = cache.TryGetValue(key, out object value);

            // assert
            Assert.False(result);
        }
示例#2
0
        public void TryGetValueNullResult()
        {
            // arrange
            var      cacheSize         = 10;
            TimeSpan slidingExpiration = TimeSpan.Zero;
            var      cache             = new TaskCache <string, string>(cacheSize,
                                                                        slidingExpiration);
            var key = "Foo";

            // act
            var result = cache.TryGetValue(key, out Task <string> value);

            // assert
            Assert.False(result);
        }
示例#3
0
        public void TryGetValueKeyNull()
        {
            // arrange
            var      cacheSize         = 10;
            TimeSpan slidingExpiration = TimeSpan.Zero;
            var      cache             = new TaskCache <string, string>(cacheSize,
                                                                        slidingExpiration);
            string key = null;

            // act
            Action verify = () => cache.TryGetValue(key,
                                                    out Task <string> value);

            // assert
            Assert.Throws <ArgumentNullException>("key", verify);
        }
示例#4
0
        public void TryGetValueResultByInteger()
        {
            // arrange
            var cacheSize = 10;
            var cache     = new TaskCache(cacheSize);
            var key       = 1;
            var expected  = "Bar";

            cache.TryAdd(key, expected);

            // act
            var result = cache.TryGetValue(key, out object actual);

            // assert
            Assert.True(result);
            Assert.Equal(expected, (string)actual);
        }
示例#5
0
        public void VerifyExpirationTrue()
        {
            // arrange
            var cacheSize         = 10;
            var slidingExpiration = TimeSpan.FromMilliseconds(500);
            var cache             = new TaskCache <string, string>(cacheSize,
                                                                   slidingExpiration);
            var key = "Foo";

            cache.TryAdd(key, Task.FromResult("Bar"));

            // act
            var exists = cache.TryGetValue(key, out Task <string> actual);

            // assert
            Assert.True(exists);
        }
示例#6
0
        public void TryAddNewCacheEntry()
        {
            // arrange
            var cacheSize = 10;
            var cache     = new TaskCache(cacheSize);
            var key       = "Foo";
            var expected  = "Bar";

            // act
            var added = cache.TryAdd(key, expected);

            // assert
            var exists = cache.TryGetValue(key, out object actual);

            Assert.True(added);
            Assert.True(exists);
            Assert.Equal(expected, (string)actual);
        }
示例#7
0
        public void RemoveEntry()
        {
            // arrange
            var cacheSize = 10;
            var cache     = new TaskCache(cacheSize);
            var key       = "Foo";

            cache.TryAdd(key, "Bar");

            // act
            cache.Remove(key);

            // assert
            var exists = cache.TryGetValue(key, out object actual);

            Assert.False(exists);
            Assert.Null(actual);
        }
示例#8
0
        public async Task VerifyExpirationFalse()
        {
            // arrange
            var cacheSize         = 10;
            var slidingExpiration = TimeSpan.FromMilliseconds(100);
            var cache             = new TaskCache <string, string>(cacheSize,
                                                                   slidingExpiration);
            var key = "Foo";

            cache.TryAdd(key, Task.FromResult("Bar"));
            await Task.Delay(300).ConfigureAwait(false);

            // act
            var exists = cache.TryGetValue(key, out Task <string> actual);

            // assert
            Assert.False(exists);
        }
示例#9
0
        public async Task TryGetValueResult()
        {
            // arrange
            var      cacheSize         = 10;
            TimeSpan slidingExpiration = TimeSpan.Zero;
            var      cache             = new TaskCache <string, string>(cacheSize,
                                                                        slidingExpiration);
            var key      = "Foo";
            var expected = Task.FromResult("Bar");

            cache.TryAdd(key, expected);

            // act
            var result = cache.TryGetValue(key, out Task <string> actual);

            // assert
            Assert.True(result);
            Assert.Equal(await expected.ConfigureAwait(false),
                         await actual.ConfigureAwait(false));
        }
示例#10
0
        public void RemoveEntry()
        {
            // arrange
            var      cacheSize         = 10;
            TimeSpan slidingExpiration = TimeSpan.Zero;
            var      cache             = new TaskCache <string, string>(cacheSize,
                                                                        slidingExpiration);
            var key = "Foo";

            cache.TryAdd(key, Task.FromResult("Bar"));

            // act
            cache.Remove(key);

            // assert
            var exists = cache.TryGetValue(key, out Task <string> actual);

            Assert.False(exists);
            Assert.Null(actual);
        }
示例#11
0
        public void TryAddTwice()
        {
            // arrange
            var cacheSize = 10;
            var cache     = new TaskCache(cacheSize);
            var key       = "Foo";
            var expected  = "Bar";
            var another   = "Baz";

            // act
            var addedFirst  = cache.TryAdd(key, expected);
            var addedSecond = cache.TryAdd(key, another);

            // assert
            var exists = cache.TryGetValue(key, out object actual);

            Assert.True(addedFirst);
            Assert.False(addedSecond);
            Assert.True(exists);
            Assert.Equal(expected, (string)actual);
        }
示例#12
0
        public async Task TryAddTwice()
        {
            // arrange
            var      cacheSize         = 10;
            TimeSpan slidingExpiration = TimeSpan.Zero;
            var      cache             = new TaskCache <string, string>(cacheSize,
                                                                        slidingExpiration);
            var key      = "Foo";
            var expected = Task.FromResult("Bar");
            var another  = Task.FromResult("Baz");

            // act
            var addedFirst  = cache.TryAdd(key, expected);
            var addedSecond = cache.TryAdd(key, another);

            // assert
            var exists = cache.TryGetValue(key, out Task <string> actual);

            Assert.True(addedFirst);
            Assert.False(addedSecond);
            Assert.True(exists);
            Assert.Equal(await expected.ConfigureAwait(false),
                         await actual.ConfigureAwait(false));
        }