Exemplo n.º 1
0
        static void WaitForDispose(DisposeTrackingHandler handler, TimeSpan?timeout = null)
        {
            timeout = timeout ?? TimeSpan.FromSeconds(20);

            var sw = Stopwatch.StartNew();

            do
            {
                GC.Collect(2, GCCollectionMode.Forced, true, true);
                GC.WaitForPendingFinalizers();
                GC.Collect(2, GCCollectionMode.Forced, true, true);
            } while (!handler.IsDisposed && sw.Elapsed < timeout);
        }
Exemplo n.º 2
0
        public async Task disposes_handlers_once_out_of_scope()
        {
            DisposeTrackingHandler handler = null;

            HttpMessageHandler createHandler()
            {
                return(handler =
                           new DisposeTrackingHandler(
                               new DelegateHandler((message, token) => Task.FromResult(new HttpResponseMessage()))));
            }

            var factory = new RoundRobinHttpClientFactory(1, createHandler, TimeSpan.FromMilliseconds(1));

            await CreateAndDisposeClientWithGarbageCollection(factory);

            WaitForDispose(handler, TimeSpan.FromSeconds(15));

            handler.IsDisposed.ShouldBeTrue();
        }