Esempio n. 1
0
        public void Size(int cacheSize, int expectedCacheSize)
        {
            // arrange
            var cache = new TaskCache(cacheSize);

            // act
            var result = cache.Size;

            // assert
            Assert.Equal(expectedCacheSize, result);
        }
Esempio n. 2
0
        public void ConstructorBNoException()
        {
            // arrange
            var cache = new TaskCache <string>(10, TimeSpan.Zero);

            // act
            Action verify = () => new CacheConstructor(cache);

            // assert
            Assert.Null(Record.Exception(verify));
        }
Esempio n. 3
0
        public void ConstructorBCacheNull()
        {
            // arrange
            TaskCache <string> cache = null;

            // act
            Action verify = () => new CacheConstructor(cache);

            // assert
            Assert.Throws <ArgumentNullException>("cache", verify);
        }
Esempio n. 4
0
        public void ClearEmptyCache()
        {
            // arrange
            var cacheSize = 10;
            var cache     = new TaskCache(cacheSize);

            // act
            cache.Clear();

            // assert
            Assert.Equal(0, cache.Usage);
        }
Esempio n. 5
0
        public void ClearNoException()
        {
            // arrange
            var cacheSize = 10;
            var cache     = new TaskCache(cacheSize);

            // act
            Action verify = () => cache.Clear();

            // assert
            Assert.Null(Record.Exception(verify));
        }
Esempio n. 6
0
        public void Size(int cacheSize, int expectedCacheSize)
        {
            // arrange
            TimeSpan slidingExpiration = TimeSpan.Zero;
            var      cache             = new TaskCache <string, string>(cacheSize,
                                                                        slidingExpiration);

            // act
            var result = cache.Size;

            // assert
            Assert.Equal(expectedCacheSize, result);
        }
        public void TryAddValueNull()
        {
            // arrange
            var cacheSize = 10;
            var cache     = new TaskCache(cacheSize);
            var key       = new TaskCacheKey("a", "Foo");

            // act
            void Verify() => cache.TryAdd(key, default(Task <string>) !);

            // assert
            Assert.Throws <ArgumentNullException>("value", Verify);
        }
        public void GetOrAddTaskWhenNothingIsCached_IntegerKey()
        {
            // arrange
            var cacheSize = 10;
            var cache     = new TaskCache(cacheSize);
            var key       = new TaskCacheKey("a", 1);

            // act
            Task <string> resolved = cache.GetOrAddTask(key, () => Task.FromResult("Quox"));

            // assert
            Assert.Equal("Quox", resolved.Result);
        }
Esempio n. 9
0
        public void RemoveNoException()
        {
            // arrange
            var cacheSize = 10;
            var cache     = new TaskCache(cacheSize);
            var key       = "Foo";

            // act
            Action verify = () => cache.Remove(key);

            // assert
            Assert.Null(Record.Exception(verify));
        }
Esempio n. 10
0
        public void RemoveNoException()
        {
            // arrange
            var cacheSize = 10;
            var cache     = new TaskCache(cacheSize);
            var key       = "Foo";

            // act
            bool Verify() => cache.TryRemove(new("a", key));

            // assert
            Assert.False(Verify());
        }
Esempio n. 11
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);
        }
Esempio n. 12
0
        public void ClearNoException()
        {
            // arrange
            var      cacheSize         = 10;
            TimeSpan slidingExpiration = TimeSpan.Zero;
            var      cache             = new TaskCache <string, string>(cacheSize,
                                                                        slidingExpiration);

            // act
            Action verify = () => cache.Clear();

            // assert
            Assert.Null(Record.Exception(verify));
        }
Esempio n. 13
0
        public void TryAddValueNull()
        {
            // arrange
            var    cacheSize = 10;
            var    cache     = new TaskCache(cacheSize);
            var    key       = "Foo";
            string value     = null;

            // act
            Action verify = () => cache.TryAdd(key, value);

            // assert
            Assert.Throws <ArgumentNullException>("value", verify);
        }
Esempio n. 14
0
        public void TryAddNoException()
        {
            // arrange
            var cacheSize = 10;
            var cache     = new TaskCache(cacheSize);
            var key       = "Foo";
            var value     = "Bar";

            // act
            Action verify = () => cache.TryAdd(key, value);

            // assert
            Assert.Null(Record.Exception(verify));
        }
Esempio n. 15
0
        public void ClearEmptyCache()
        {
            // arrange
            var      cacheSize         = 10;
            TimeSpan slidingExpiration = TimeSpan.Zero;
            var      cache             = new TaskCache <string, string>(cacheSize,
                                                                        slidingExpiration);

            // act
            cache.Clear();

            // assert
            Assert.Equal(0, cache.Usage);
        }
Esempio n. 16
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);
        }
Esempio n. 17
0
        public void RemoveKeyNull()
        {
            // arrange
            var      cacheSize         = 10;
            TimeSpan slidingExpiration = TimeSpan.Zero;
            var      cache             = new TaskCache <string, string>(cacheSize,
                                                                        slidingExpiration);
            string key = null;

            // act
            Action verify = () => cache.Remove(key);

            // assert
            Assert.Throws <ArgumentNullException>("key", verify);
        }
Esempio n. 18
0
        public void ClearAllEntries()
        {
            // arrange
            var cacheSize = 10;
            var cache     = new TaskCache(cacheSize);

            cache.TryAdd("Foo", Task.FromResult("Bar"));
            cache.TryAdd("Bar", Task.FromResult("Baz"));

            // act
            cache.Clear();

            // assert
            Assert.Equal(0, cache.Usage);
        }
Esempio n. 19
0
        public void RemoveNoException()
        {
            // arrange
            var      cacheSize         = 10;
            TimeSpan slidingExpiration = TimeSpan.Zero;
            var      cache             = new TaskCache <string>(cacheSize,
                                                                slidingExpiration);
            var key = "Foo";

            // act
            Action verify = () => cache.Remove(key);

            // assert
            Assert.Null(Record.Exception(verify));
        }
Esempio n. 20
0
        public void TryAddKeyNull()
        {
            // arrange
            var      cacheSize         = 10;
            TimeSpan slidingExpiration = TimeSpan.Zero;
            var      cache             = new TaskCache <string, string>(cacheSize,
                                                                        slidingExpiration);
            string key   = null;
            var    value = Task.FromResult("Foo");

            // act
            Action verify = () => cache.TryAdd(key, value);

            // assert
            Assert.Throws <ArgumentNullException>("key", verify);
        }
Esempio n. 21
0
        public void TryAddValueNull()
        {
            // arrange
            var      cacheSize         = 10;
            TimeSpan slidingExpiration = TimeSpan.Zero;
            var      cache             = new TaskCache <string, string>(cacheSize,
                                                                        slidingExpiration);
            var           key   = "Foo";
            Task <string> value = null;

            // act
            Action verify = () => cache.TryAdd(key, value);

            // assert
            Assert.Throws <ArgumentNullException>("value", verify);
        }
Esempio n. 22
0
        public void TryAddNoException()
        {
            // arrange
            var      cacheSize         = 10;
            TimeSpan slidingExpiration = TimeSpan.Zero;
            var      cache             = new TaskCache <string, string>(cacheSize,
                                                                        slidingExpiration);
            var key   = "Foo";
            var value = Task.FromResult("Bar");

            // act
            Action verify = () => cache.TryAdd(key, value);

            // assert
            Assert.Null(Record.Exception(verify));
        }
Esempio n. 23
0
        public void ClearAllEntries()
        {
            // arrange
            var      cacheSize         = 10;
            TimeSpan slidingExpiration = TimeSpan.Zero;
            var      cache             = new TaskCache <string, string>(cacheSize,
                                                                        slidingExpiration);

            cache.TryAdd("Foo", Task.FromResult("Bar"));
            cache.TryAdd("Bar", Task.FromResult("Baz"));

            // act
            cache.Clear();

            // assert
            Assert.Equal(0, cache.Usage);
        }
Esempio n. 24
0
        public void Usage(string[] values, int expectedUsage)
        {
            // arrange
            var cacheSize = 10;
            var cache     = new TaskCache(cacheSize);

            foreach (var value in values)
            {
                cache.TryAdd($"Key:{value}", value);
            }

            // act
            var result = cache.Usage;

            // assert
            Assert.Equal(expectedUsage, result);
        }
Esempio n. 25
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);
        }
Esempio n. 26
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);
        }
Esempio n. 27
0
        public void TryAddNewCacheEntryWithFactory()
        {
            // arrange
            var cacheSize = 10;
            var cache     = new TaskCache(cacheSize);
            var key       = new TaskCacheKey("a", "Foo");
            var expected  = Task.FromResult("Bar");

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

            // assert
            Task <string> resolved = cache.GetOrAddTask(key, () => Task.FromResult("Baz"));

            Assert.True(added);
            Assert.Same(expected, resolved);
        }
Esempio n. 28
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);
        }
Esempio n. 29
0
        public void RemoveEntry()
        {
            // arrange
            var cacheSize = 10;
            var cache     = new TaskCache(cacheSize);
            var key       = new TaskCacheKey("a", "Foo");
            var value     = Task.FromResult("Bar");

            cache.TryAdd(key, value);

            // act
            cache.TryRemove(key);

            // assert
            Task <string> retrieved = cache.GetOrAddTask(key, () => Task.FromResult("Baz"));

            Assert.NotSame(value, retrieved);
        }
Esempio n. 30
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);
        }