Пример #1
0
        public void ResultDataIsNull_CachesIt()
        {
            // arrange
            var cache      = new CacheProviderStub();
            var cache_key  = Guid.NewGuid().ToString();
            var exec_count = 0;

            var create_result = new Func <CachableResult <string> >
                                    (() =>
            {
                exec_count++;
                return(new CachableResult <string> (null, CachingParameters.FromMinutes(1)));
            });


            // act
            var result  = cache.Get(cache_key, create_result);
            var result2 = cache.Get(cache_key, create_result);


            // assert
            cache.Values.Should().HaveCount(1);
            result.Should().BeNull();
            result2.Should().BeNull();
            exec_count.Should().Be(1);

            GetWithLockExtensions.Locks.Should().BeEmpty();
        }
Пример #2
0
        public void SingleThread_InvokesGetValueFunctionOnce()
        {
            // arrange
            var cache      = new CacheProviderStub();
            var cache_key  = Guid.NewGuid().ToString();
            var exec_count = 0;


            // act
            var actual = cache.Get
                             (cache_key,
                             () =>
            {
                exec_count++;
                return(new CachableResult <string> ("aaa", CachingParameters.FromMinutes(1)));
            });


            // assert
            actual.Should().Be("aaa");

            cache.Values
            .Should().HaveCount(1)
            .And.ContainKey(cache_key)
            .WhichValue.Value.Should().Be("aaa");

            exec_count.Should().Be(1);

            GetWithLockExtensions.Locks.Should().BeEmpty();
        }
Пример #3
0
        private static void MultiThreadWithConcurency_InvokesGetValueFunctionOnce_Test(int maxThreads, bool sta)
        {
            // arrange
            var cache     = new CacheProviderStub();
            var cache_key = Guid.NewGuid().ToString();

            var exec_count  = 0;
            var start_event = new ManualResetEvent(false);

            var results = new string[maxThreads];

            var threads = Enumerable.Range(0, maxThreads).Select
                              (x =>
            {
                var thread = new Thread
                                 (() =>
                {
                    start_event.WaitOne();
                    results[x] = cache.GetAsync
                                     (cache_key,
                                     () => Task.Run
                                         (async() =>
                    {
                        await Task.Delay(TimeSpan.FromMilliseconds(100)).ConfigureAwait(false);

                        Interlocked.Increment(ref exec_count);

                        return(new CachableResult <string>("aaa", CachingParameters.FromMinutes(1)));
                    })).Result;
                });

                thread.IsBackground = true;

                if (sta)
                {
                    thread.SetApartmentState(ApartmentState.STA);
                }

                return(thread);
            })
                          .ToList();


            // act
            threads.ForEach(t => t.Start());
            start_event.Set();
            threads.ForEach(t => t.Join());


            // assert
            cache.Values
            .Should().HaveCount(1)
            .And.ContainKey(cache_key)
            .WhichValue.Value.Should().Be("aaa");

            results.Should().OnlyContain(x => x == "aaa");
            exec_count.Should().Be(1);

            GetWithLockExtensions.Locks.Should().BeEmpty();
        }
Пример #4
0
        private static async Task MultiTaskWithConcurency_InvokesGetValueFunctionOnce_Test(int maxTasks)
        {
            // arrange
            var cache     = new CacheProviderStub();
            var cache_key = Guid.NewGuid().ToString();

            var exec_count  = 0;
            var start_event = new SemaphoreSlim(0, maxTasks);

            var results = new string[maxTasks];

            var task_factory = Task.Factory;

            var tasks = Enumerable.Range(0, maxTasks).Select
                            (x => task_factory.StartNew
                                (async() =>
            {
                await start_event.WaitAsync();

                await Task.Delay(TimeSpan.FromMilliseconds(100));

                Interlocked.Increment(ref exec_count);

                var result = cache.Get
                                 (cache_key,
                                 () =>
                {
                    Task.Delay(100).Wait();

                    return(new CachableResult <string> ("aaa", CachingParameters.FromMinutes(10)));
                });

                return(result);
            }
                                ).Unwrap()).ToList();


            // act
            start_event.Release(maxTasks);
            await Task.WhenAll(tasks);


            // assert
            cache.Values
            .Should().HaveCount(1)
            .And.ContainKey(cache_key)
            .WhichValue.Value.Should().Be("aaa");

            results.Should().OnlyContain(x => x == "aaa");
            exec_count.Should().Be(1);

            GetWithLockExtensions.Locks.Should().BeEmpty();
        }