public void AcquireRelease()
        {
            var lockName = "AcquireRelease" + DateTime.Now.Ticks;

            var collection = GetCollection();

            collection.Should().NotBeNull();

            var locker = new ThrottleLock(collection, TimeSpan.FromMinutes(5));

            locker.Should().NotBeNull();

            var result = locker.Acquire(lockName);

            result.Should().BeTrue();

            var status = locker.Status(lockName);

            status.Should().NotBeNull();
            status.IsLocked.Should().BeTrue();

            locker.Release(lockName);

            status = locker.Status(lockName);
            status.Should().NotBeNull();
            status.IsLocked.Should().BeFalse();
        }
예제 #2
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Before public void setup() throws Exception
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void Setup()
        {
            @lock = NewThrottleLockMock();

            _config = mock(typeof(SocketChannelConfig));

            _lockAttribute = mock(typeof(Attribute));
            when(_lockAttribute.get()).thenReturn(@lock);

            Attribute durationExceedAttribute = mock(typeof(Attribute));

            when(durationExceedAttribute.get()).thenReturn(null);

            _channel = mock(typeof(SocketChannel), Answers.RETURNS_MOCKS);
            when(_channel.config()).thenReturn(_config);
            when(_channel.Open).thenReturn(true);
            when(_channel.remoteAddress()).thenReturn(InetSocketAddress.createUnresolved("localhost", 32000));
            when(_channel.attr(TransportWriteThrottle.LockKey)).thenReturn(_lockAttribute);
            when(_channel.attr(TransportWriteThrottle.MaxDurationExceededKey)).thenReturn(durationExceedAttribute);

            ChannelPipeline pipeline = _channel.pipeline();

            when(_channel.pipeline()).thenReturn(pipeline);

            _context = mock(typeof(ChannelHandlerContext), Answers.RETURNS_MOCKS);
            when(_context.channel()).thenReturn(_channel);
        }
        public void AcquireExpire()
        {
            var lockName = "AcquireExpire" + DateTime.Now.Ticks;

            var collection = GetCollection();

            collection.Should().NotBeNull();

            var locker = new ThrottleLock(collection);

            locker.Should().NotBeNull();

            var result = locker.Acquire(lockName, TimeSpan.FromMilliseconds(5));

            result.Should().BeTrue();

            var status = locker.Status(lockName);

            status.Should().NotBeNull();
            status.IsLocked.Should().BeTrue();

            // wait for expire
            Thread.Sleep(5);

            var blocked = locker.Acquire(lockName, TimeSpan.FromMilliseconds(5));

            blocked.Should().BeTrue();
        }
예제 #4
0
        private TransportThrottle NewThrottleAndInstall(Channel channel, ThrottleLock lockOverride, Clock clock, Duration maxLockDuration)
        {
            TransportThrottle throttle = NewThrottle(lockOverride, clock, maxLockDuration);

            throttle.Install(channel);

            return(throttle);
        }
예제 #5
0
        private TransportThrottle NewThrottle(ThrottleLock lockOverride, Clock clock, Duration maxLockDuration)
        {
            if (lockOverride != null)
            {
                @lock = lockOverride;

                when(_lockAttribute.get()).thenReturn(lockOverride);
            }

            return(new TransportWriteThrottle(64, 256, clock, maxLockDuration, () => @lock));
        }
예제 #6
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: private static ThrottleLock newThrottleLockMock() throws InterruptedException
        private static ThrottleLock NewThrottleLockMock()
        {
            ThrottleLock @lock = mock(typeof(ThrottleLock));

            doAnswer(invocation =>
            {
                // sleep a bit to prevent the caller thread spinning in a tight loop
                // every mock invocation is recorded and generates objects, like the stacktrace
                Thread.Sleep(500);
                return(null);
            }).when(@lock).@lock(any(), anyLong());
            return(@lock);
        }
예제 #7
0
 private TransportThrottle NewThrottleAndInstall(Channel channel, ThrottleLock lockOverride)
 {
     return(NewThrottleAndInstall(channel, lockOverride, Clocks.systemClock(), Duration.ZERO));
 }