예제 #1
0
        public async Task VerifyEvents()
        {
            var listener = new TestListener();
            var observer = new TestObserver(listener);

            using (DiagnosticListener.AllListeners.Subscribe(observer))
            {
                // arrange
                var batchScheduler = new ManualBatchScheduler();
                var loader         = new DataLoader <string, string>(batchScheduler, FetchDataAsync);
                var errorLoader    = new DataLoader <string, string>(
                    batchScheduler,
                    (keys, cancellationToken) =>
                    throw new Exception("BatchError: Foo"));

                // act
                var tasks = new Task[]
                {
                    Catch(() => loader.LoadAsync("Foo")),
                    Catch(() => loader.LoadAsync("Foo", "Bar")),
                    Catch(() => loader.LoadAsync("Bar", "Baz")),
                    Catch(() => loader.LoadAsync("Qux")),
                    Catch(() => errorLoader.LoadAsync("Foo"))
                };
                batchScheduler.Dispatch();
                await Task.WhenAll(tasks).ConfigureAwait(false);

                // assert
                listener.MatchSnapshot();
            }
        }
예제 #2
0
        public async Task VerifyEvents()
        {
            var listener = new TestListener();
            var observer = new TestObserver(listener);

            using (DiagnosticListener.AllListeners.Subscribe(observer))
            {
                // arrange
                var batchOptions = new DataLoaderOptions <string>
                {
                    AutoDispatching   = true,
                    Batching          = true,
                    BatchRequestDelay = TimeSpan.FromMilliseconds(150),
                    Caching           = true
                };
                var batchLoader = new DataLoader <string, string>(
                    batchOptions,
                    FetchDataAsync);
                var batchErrorLoader = new DataLoader <string, string>(
                    batchOptions,
                    (keys, cancellationToken) =>
                    throw new Exception("BatchError: Foo"));
                var singleOptions = new DataLoaderOptions <string>
                {
                    AutoDispatching = true,
                    Batching        = false,
                    Caching         = true
                };
                var singleLoader = new DataLoader <string, string>(
                    singleOptions,
                    FetchDataAsync);

                // act
                await Catch(() => batchLoader.LoadAsync("Foo"))
                .ConfigureAwait(false);

                await Task.Delay(400).ConfigureAwait(false);
                await Catch(() => batchLoader.LoadAsync("Foo", "Bar"))
                .ConfigureAwait(false);

                await Task.Delay(400).ConfigureAwait(false);
                await Catch(() => batchLoader.LoadAsync("Bar", "Baz"))
                .ConfigureAwait(false);

                await Task.Delay(400).ConfigureAwait(false);
                await Catch(() => batchLoader.LoadAsync("Qux"))
                .ConfigureAwait(false);

                await Task.Delay(400).ConfigureAwait(false);
                await Catch(() => batchErrorLoader.LoadAsync("Foo"))
                .ConfigureAwait(false);

                await Task.Delay(400).ConfigureAwait(false);
                await Catch(() => singleLoader.LoadAsync("Foo"))
                .ConfigureAwait(false);

                await Task.Delay(400).ConfigureAwait(false);
                await Catch(() => singleLoader.LoadAsync("Foo", "Bar"))
                .ConfigureAwait(false);

                await Task.Delay(400).ConfigureAwait(false);
                await Catch(() => singleLoader.LoadAsync("Bar", "Baz"))
                .ConfigureAwait(false);

                await Task.Delay(400).ConfigureAwait(false);
                await Catch(() => singleLoader.LoadAsync("Qux"))
                .ConfigureAwait(false);

                // assert
                listener.MatchSnapshot();
            }
        }