예제 #1
0
        public void TestPoolContention()
        {
            var barrier            = new Barrier(N);
            var tasks              = new Task[N];
            var totalTimesAcquired = 0;

            Assert.AreEqual(pool.maxThingId, 0);

            for (int i = 0; i < N; i++)
            {
                var t = Task.Run(() => {
                    barrier.SignalAndWait(); // Takes time for all threads to spin up
                    for (int j = 0; j < 128; j++)
                    {
                        using (var pooled = pool.GetAsync().Result) // blocks
                        {
                            Interlocked.Increment(ref totalTimesAcquired);
                        }
                    }
                });

                tasks[i] = t; // These N tasks do not yield their thread
            }

            Task.WaitAll(tasks);

            Assert.AreEqual(PoolSize, pool.maxThingId);
            Assert.AreEqual(PoolSize, pool.CurrentFreeCount);
            Assert.AreEqual(PoolSize, pool.CurrentPoolSize);
            Assert.AreEqual(N * 128, totalTimesAcquired);
        }
예제 #2
0
        public async void TestGetTimeout()
        {
            var p1 = await pool.GetAsync();

            var p2 = await pool.GetAsync();

            Assert.AreEqual(1, p1.Resource.Id);
            Assert.AreEqual(2, p2.Resource.Id);
            Assert.AreEqual(2, pool.maxThingId);

            await pool.GetAsync(); // Timesout
        }
        public async Task TestGetTimeout()
        {
            var p1 = await pool.GetAsync();

            var p2 = await pool.GetAsync();

            Assert.AreEqual(1, p1.Resource.Id);
            Assert.AreEqual(2, p2.Resource.Id);
            Assert.AreEqual(2, pool.maxThingId);

            Assert.ThrowsAsync <TimeoutException>(async() => await pool.GetAsync()); // Timesout
        }