예제 #1
0
        public async Task Ensure_Executor_Is_Correctly_Swapped_When_Evicted()
        {
            // arrange
            IRequestExecutorResolver resolver =
                new ServiceCollection()
                .AddGraphQL()
                .AddStarWarsRepositories()
                .AddStarWarsTypes()
                .Services
                .BuildServiceProvider()
                .GetRequiredService <IRequestExecutorResolver>();
            var evicted = false;
            var updated = false;

            var proxy = new RequestExecutorProxy(resolver, Schema.DefaultName);

            proxy.ExecutorEvicted += (sender, args) =>
            {
                evicted = true;
                updated = false;
            };
            proxy.ExecutorUpdated += (sender, args) => updated = true;

            // act
            IRequestExecutor a = await proxy.GetRequestExecutorAsync(CancellationToken.None);

            resolver.EvictRequestExecutor();
            IRequestExecutor b = await proxy.GetRequestExecutorAsync(CancellationToken.None);

            // assert
            Assert.NotSame(a, b);
            Assert.True(evicted);
            Assert.True(updated);
        }
        /// <summary>
        /// Creates a new auto-update proxy for <see cref="IRequestExecutor"/>.
        /// </summary>
        /// <param name="requestExecutorProxy">
        /// The underlying manual proxy.
        /// </param>
        /// <param name="initialExecutor">
        /// The initial executor instance.
        /// </param>
        /// <returns>
        /// Returns a new auto-update proxy for <see cref="IRequestExecutor"/>.
        /// </returns>
        public static AutoUpdateRequestExecutorProxy Create(
            RequestExecutorProxy requestExecutorProxy,
            IRequestExecutor initialExecutor)
        {
            if (requestExecutorProxy == null)
            {
                throw new ArgumentNullException(nameof(requestExecutorProxy));
            }

            return(new AutoUpdateRequestExecutorProxy(requestExecutorProxy, initialExecutor));
        }
        private AutoUpdateRequestExecutorProxy(
            RequestExecutorProxy requestExecutorProxy,
            IRequestExecutor initialExecutor)
        {
            _executorProxy = requestExecutorProxy;
            _executor      = initialExecutor;

            _executorProxy.ExecutorEvicted += (sender, args) => BeginUpdateExecutor();

            BeginUpdateExecutor();
        }
예제 #4
0
        public async Task Ensure_Executor_Is_Correctly_Swapped_When_Evicted()
        {
            // arrange
            IRequestExecutorResolver resolver =
                new ServiceCollection()
                .AddGraphQL()
                .AddStarWarsRepositories()
                .AddStarWarsTypes()
                .Services
                .BuildServiceProvider()
                .GetRequiredService <IRequestExecutorResolver>();
            var evicted = false;
            var updated = false;

            var innerProxy = new RequestExecutorProxy(resolver, Schema.DefaultName);

            innerProxy.ExecutorEvicted += (sender, args) =>
            {
                evicted = true;
                updated = false;
            };
            innerProxy.ExecutorUpdated += (sender, args) => updated = true;

            var proxy = await AutoUpdateRequestExecutorProxy.CreateAsync(innerProxy);

            // act
            IRequestExecutor a = proxy.InnerExecutor;

            resolver.EvictRequestExecutor();

            var i = 0;
            IRequestExecutor b = proxy.InnerExecutor;

            while (ReferenceEquals(a, b))
            {
                await Task.Delay(100);

                b = proxy.InnerExecutor;
                if (i++ > 10)
                {
                    break;
                }
            }

            // assert
            Assert.NotSame(a, b);
            Assert.True(evicted);
            Assert.True(updated);
        }
        /// <summary>
        /// Creates a new auto-update proxy for <see cref="IRequestExecutor"/>.
        /// </summary>
        /// <param name="requestExecutorProxy">
        /// The underlying manual proxy.
        /// </param>
        /// <param name="cancellationToken">
        /// The cancellation token.
        /// </param>
        /// <returns>
        /// Returns a new auto-update proxy for <see cref="IRequestExecutor"/>.
        /// </returns>
        public static async ValueTask <AutoUpdateRequestExecutorProxy> CreateAsync(
            RequestExecutorProxy requestExecutorProxy,
            CancellationToken cancellationToken = default)
        {
            if (requestExecutorProxy == null)
            {
                throw new ArgumentNullException(nameof(requestExecutorProxy));
            }

            IRequestExecutor executor = await requestExecutorProxy
                                        .GetRequestExecutorAsync(cancellationToken)
                                        .ConfigureAwait(false);

            return(new AutoUpdateRequestExecutorProxy(requestExecutorProxy, executor));
        }
예제 #6
0
        public async Task Ensure_Executor_Is_Cached()
        {
            // arrange
            IRequestExecutorResolver resolver =
                new ServiceCollection()
                .AddGraphQL()
                .AddStarWarsRepositories()
                .AddStarWarsTypes()
                .Services
                .BuildServiceProvider()
                .GetRequiredService <IRequestExecutorResolver>();

            // act
            var proxy          = new RequestExecutorProxy(resolver, Schema.DefaultName);
            IRequestExecutor a = await proxy.GetRequestExecutorAsync(CancellationToken.None);

            IRequestExecutor b = await proxy.GetRequestExecutorAsync(CancellationToken.None);

            // assert
            Assert.Same(a, b);
        }
예제 #7
0
        public async Task Ensure_Executor_Is_Cached()
        {
            // arrange
            IRequestExecutorResolver resolver =
                new ServiceCollection()
                .AddGraphQL()
                .AddStarWarsRepositories()
                .AddStarWarsTypes()
                .Services
                .BuildServiceProvider()
                .GetRequiredService <IRequestExecutorResolver>();

            var innerProxy = new RequestExecutorProxy(resolver, Schema.DefaultName);

            // act
            var proxy = await AutoUpdateRequestExecutorProxy.CreateAsync(innerProxy);

            IRequestExecutor a = proxy.InnerExecutor;
            IRequestExecutor b = proxy.InnerExecutor;

            // assert
            Assert.Same(a, b);
        }