Ejemplo n.º 1
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, canncellationToken) =>
                    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.Snapshot();
            }
        }
Ejemplo n.º 2
0
 public TestObserver(TestListener listener)
 {
     _listener = listener ??
                 throw new ArgumentNullException(nameof(listener));
 }