Exemplo n.º 1
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void shouldSlowlyReduceTheNumberOfResourcesInThePoolWhenResourcesAreReleased() throws Exception
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void ShouldSlowlyReduceTheNumberOfResourcesInThePoolWhenResourcesAreReleased()
        {
            // given
            const int poolMinSize = 50;
            const int poolMaxSize = 200;

            StatefulMonitor stateMonitor = new StatefulMonitor(this);
            FakeClock       clock        = FakeClocks;
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final SomethingResourcePool pool = getResourcePool(stateMonitor, clock, poolMinSize);
            SomethingResourcePool pool = GetResourcePool(stateMonitor, clock, poolMinSize);

            AcquireResourcesAndExceedTimeout(pool, clock, poolMaxSize);

            // when
            // After the peak, stay below MIN_SIZE concurrent usage, using up all already present resources.
            ExceedTimeout(clock);
            for (int i = 0; i < poolMaxSize; i++)
            {
                AcquireFromPool(pool, 1)[0].release();
            }

            // then
            // currentPeakSize must have reset from the latest check to minimum size.
            assertEquals(1, stateMonitor.CurrentPeakSize.get());                 // because of timeout
            // targetSize must be set to MIN_SIZE since currentPeakSize was that 2 checks ago and didn't increase
            assertEquals(poolMinSize, stateMonitor.TargetSize.get());
            // Only pooled resources must be used, disposing what is in excess
            // +1 that was used to trigger exceed timeout check
            assertEquals(poolMinSize, pool.UnusedSize());
            assertEquals(poolMaxSize - poolMinSize + 1, stateMonitor.DisposedConflict.get());
        }
Exemplo n.º 2
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void shouldReclaimAndRecreateWhenUsageGoesDownBetweenSpikes() throws Exception
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void ShouldReclaimAndRecreateWhenUsageGoesDownBetweenSpikes()
        {
            // given
            const int poolMinSize = 50;
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final int bellowPoolMinSize = poolMinSize / 5;
            int       bellowPoolMinSize = poolMinSize / 5;
            const int poolMaxSize       = 200;

            StatefulMonitor stateMonitor = new StatefulMonitor(this);
            FakeClock       clock        = FakeClocks;
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final SomethingResourcePool pool = getResourcePool(stateMonitor, clock, poolMinSize);
            SomethingResourcePool pool = GetResourcePool(stateMonitor, clock, poolMinSize);

            AcquireResourcesAndExceedTimeout(pool, clock, poolMaxSize);

            // when
            // After the peak, stay well below concurrent usage, using up all already present resources in the process
            ExceedTimeout(clock);
            // Requires some rounds to happen, since there is constant racing between releasing and acquiring which does
            // not always result in reaping of resources, as there is reuse
            for (int i = 0; i < 30; i++)
            {
                // The latch is necessary to reduce races between batches
                System.Threading.CountdownEvent release = new System.Threading.CountdownEvent(bellowPoolMinSize);
                foreach (ResourceHolder holder in AcquireFromPool(pool, bellowPoolMinSize))
                {
                    holder.Release(release);
                }
                release.await();
                ExceedTimeout(clock);
            }

            // then
            // currentPeakSize should not be higher than bellowPoolMinSize
            assertTrue(stateMonitor.CurrentPeakSize.get().ToString(), stateMonitor.CurrentPeakSize.get() <= bellowPoolMinSize);
            // target size should remain at pool min size
            assertEquals(poolMinSize, stateMonitor.TargetSize.get());
            assertEquals(poolMinSize, pool.UnusedSize());
            // only the excess from the pool max size down to min size must have been disposed
            // +1 that was used to trigger initial exceed timeout check
            assertEquals(poolMaxSize - poolMinSize + 1, stateMonitor.DisposedConflict.get());

            stateMonitor.CreatedConflict.set(0);
            stateMonitor.DisposedConflict.set(0);

            // when
            // After the lull, recreate a peak
            AcquireResourcesAndExceedTimeout(pool, clock, poolMaxSize);

            // then
            assertEquals(poolMaxSize - poolMinSize + 1, stateMonitor.CreatedConflict.get());
            assertEquals(0, stateMonitor.DisposedConflict.get());
        }
Exemplo n.º 3
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void shouldMaintainPoolHigherThenMinSizeWhenPeekUsagePasses() throws Exception
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void ShouldMaintainPoolHigherThenMinSizeWhenPeekUsagePasses()
        {
            // given
            const int poolMinSize       = 50;
            const int poolMaxSize       = 200;
            const int afterPeekPoolSize = 90;

            StatefulMonitor stateMonitor = new StatefulMonitor(this);
            FakeClock       clock        = FakeClocks;
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final SomethingResourcePool pool = getResourcePool(stateMonitor, clock, poolMinSize);
            SomethingResourcePool pool = GetResourcePool(stateMonitor, clock, poolMinSize);

            AcquireResourcesAndExceedTimeout(pool, clock, poolMaxSize);

            // when
            // After the peak, stay at afterPeekPoolSize concurrent usage, using up all already present resources in the process
            // but also keeping the high watermark above the minimum size
            ExceedTimeout(clock);
            // Requires some rounds to happen, since there is constant racing between releasing and acquiring which does
            // not always result in reaping of resources, as there is reuse
            for (int i = 0; i < 10; i++)
            {
                // The latch is necessary to reduce races between batches
                System.Threading.CountdownEvent release = new System.Threading.CountdownEvent(afterPeekPoolSize);
                foreach (ResourceHolder holder in AcquireFromPool(pool, afterPeekPoolSize))
                {
                    holder.Release(release);
                }
                release.await();
                ExceedTimeout(clock);
            }

            // then
            // currentPeakSize should be at afterPeekPoolSize
            assertEquals(afterPeekPoolSize, stateMonitor.CurrentPeakSize.get());
            // target size too
            assertEquals(afterPeekPoolSize, stateMonitor.TargetSize.get());
            // only the excess from the maximum size down to after peek usage size must have been disposed
            // +1 that was used to trigger exceed timeout check
            assertEquals(afterPeekPoolSize, pool.UnusedSize());
            assertThat(stateMonitor.DisposedConflict.get(), greaterThanOrEqualTo(poolMaxSize - afterPeekPoolSize + 1));
        }