Esempio n. 1
0
        public void ResourceHandle_should_return_instance_to_pool_upon_disposal_even_if_not_fully_obtained()
        {
            using (var pool = new ResourcePool <IDisposable>(new InstanceHolder().CreateInstance, 1))
            {
                using (var handle = pool.CreateHandle())
                { var _ = handle.ObtainAsync(); }

                using (var handle2 = pool.CreateHandle())
                    Assert.DoesNotThrowAsync(() => handle2.ObtainAsync(new CancellationTokenSource(1000).Token));
            }
        }
Esempio n. 2
0
        public async Task ResourceHandle_should_cancel_obtain_upon_disposal()
        {
            using (var pool = new ResourcePool <IDisposable>(new InstanceHolder().CreateInstance, 1))
            {
                using (var handle = pool.CreateHandle())
                {
                    await handle.ObtainAsync();

                    Task task;
                    using (var handle2 = pool.CreateHandle())
                        task = handle2.ObtainAsync();
                    Assert.ThrowsAsync <OperationCanceledException>(() => task);
                }
            }
        }
Esempio n. 3
0
        public async Task ResourceHandle_should_always_return_the_same_resource_even_if_task_did_not_finish()
        {
            using (var pool = new ResourcePool <IDisposable>(new InstanceHolder().CreateInstance, 1))
            {
                using (var handle = pool.CreateHandle())
                {
                    await handle.ObtainAsync();

                    using (var handle2 = pool.CreateHandle())
                    {
                        var t1 = handle2.ObtainAsync();
                        var t2 = handle2.ObtainAsync();
                        handle.Dispose();
                        Assert.That(await t1, Is.SameAs(await t2));
                    }
                }
            }
        }
Esempio n. 4
0
        public async Task ResourceHandle_should_cancel_obtain_upon_disposal_allowing_other_handles_to_retrieve_resource()
        {
            using (var pool = new ResourcePool <IDisposable>(new InstanceHolder().CreateInstance, 1))
            {
                using (var handle = pool.CreateHandle())
                {
                    await handle.ObtainAsync();

                    using (var handle2 = pool.CreateHandle())
                    { var _ = handle2.ObtainAsync(); }

                    handle.Dispose();

                    using (var handle3 = pool.CreateHandle())
                        Assert.DoesNotThrowAsync(() => handle3.ObtainAsync(new CancellationTokenSource(1000).Token));
                }
            }
        }
Esempio n. 5
0
        public void ResourceHandle_should_cancel_instance_creation_upon_pool_disposal()
        {
            async Task <IDisposable> FactoryMethod(CancellationToken token)
            {
                await Task.Delay(2000, token);

                Assert.Fail("Factory method not get cancelled before reaching here");
                return(null);
            }

            using (var pool = new ResourcePool <IDisposable>(FactoryMethod, 1))
            {
                var handle = pool.CreateHandle();
                var task   = handle.ObtainAsync();
                pool.Dispose();
                Assert.ThrowsAsync <TaskCanceledException>(async() => await task);
            }
        }