Ejemplo n.º 1
0
        public void Should_honour_cancellation_even_if_prior_execution_has_cached()
        {
            const string valueToReturn = "valueToReturn";
            const string executionKey  = "SomeExecutionKey";

            CachePolicy <string> cache = Policy.Cache <string>(new StubCacheProvider(), TimeSpan.MaxValue);

            CancellationTokenSource tokenSource = new CancellationTokenSource();

            int delegateInvocations = 0;
            Func <CancellationToken, string> func = ct =>
            {
                // delegate does not observe cancellation token; test is whether CacheEngine does.
                delegateInvocations++;
                return(valueToReturn);
            };

            cache.Execute(func, new Context(executionKey), tokenSource.Token).Should().Be(valueToReturn);
            delegateInvocations.Should().Be(1);

            tokenSource.Cancel();

            cache.Invoking(policy => policy.Execute(func, new Context(executionKey), tokenSource.Token))
            .ShouldThrow <OperationCanceledException>();
            delegateInvocations.Should().Be(1);
        }
Ejemplo n.º 2
0
        public void Should_honour_cancellation_during_delegate_execution_and_not_put_to_cache()
        {
            const string valueToReturn = "valueToReturn";
            const string executionKey  = "SomeExecutionKey";

            ISyncCacheProvider   stubCacheProvider = new StubCacheProvider();
            CachePolicy <string> cache             = Policy.Cache <string>(stubCacheProvider, TimeSpan.MaxValue);

            CancellationTokenSource tokenSource = new CancellationTokenSource();

            Func <CancellationToken, string> func = ct =>
            {
                tokenSource.Cancel(); // simulate cancellation raised during delegate execution
                ct.ThrowIfCancellationRequested();
                return(valueToReturn);
            };

            cache.Invoking(policy => policy.Execute(func, new Context(executionKey), tokenSource.Token))
            .ShouldThrow <OperationCanceledException>();

            stubCacheProvider.Get(executionKey).Should().BeNull();
        }
Ejemplo n.º 3
0
        public void Should_honour_cancellation_during_delegate_execution_and_not_put_to_cache()
        {
            const string valueToReturn = "valueToReturn";
            const string operationKey  = "SomeOperationKey";

            ISyncCacheProvider stubCacheProvider = new StubCacheProvider();
            CachePolicy        cache             = Policy.Cache(stubCacheProvider, TimeSpan.MaxValue);

            CancellationTokenSource tokenSource = new CancellationTokenSource();

            Func <Context, CancellationToken, string> func = (ctx, ct) =>
            {
                tokenSource.Cancel(); // simulate cancellation raised during delegate execution
                ct.ThrowIfCancellationRequested();
                return(valueToReturn);
            };

            cache.Invoking(policy => policy.Execute(func, new Context(operationKey), tokenSource.Token))
            .ShouldThrow <OperationCanceledException>();

            (bool cacheHit, object fromCache) = stubCacheProvider.TryGet(operationKey);
            cacheHit.Should().BeFalse();
            fromCache.Should().BeNull();
        }