Exemple #1
0
        public static async Task TestConcurrent(IResourcePool <int, Nothing> pool, int clients)
        {
            async Task Call()
            {
                int resource = -1;

                try
                {
                    do
                    {
                        resource = pool.Acquire();
                    } while (resource < 0);

                    await Task.Delay(_random.Next(10, 100));
                }
                finally
                {
                    pool.Release(resource);
                    var stats = pool.Stats();
                    Assert.Equal(stats.Allocations, stats.Evictions + stats.Idle + stats.InUse);
                }
            }

            var tasks = new List <Task>();

            for (var i = 0; i < clients; i++)
            {
                var task = Task.Run(Call);
                tasks.Add(task);
            }

            await Task.WhenAll(tasks);

            Assert.Equal(pool.Stats().Idle, pool.Size);
        }