public void TestGetSessionFromDriverWithSessionReturnedToPool()
        {
            var sendCommandResponseWithStartSession = new SendCommandResponse
            {
                StartSession = new StartSessionResult
                {
                    SessionToken = "testToken"
                },
                ResponseMetadata = new ResponseMetadata
                {
                    RequestId = "testId"
                }
            };
            var sendCommandResponseEmpty = new SendCommandResponse();

            mockClient.SetupSequence(x => x.SendCommandAsync(It.IsAny <SendCommandRequest>(), It.IsAny <CancellationToken>()))
            .Returns(Task.FromResult(sendCommandResponseWithStartSession))
            .Returns(Task.FromResult(sendCommandResponseEmpty));
            builder = PooledQldbDriver.Builder().WithLedger("testLedger");

            var driver  = new PooledQldbDriver("ledgerName", mockClient.Object, 4, 4, 4, NullLogger.Instance);
            var session = driver.GetSession();

            session.Dispose();
            session = driver.GetSession();
            Assert.IsNotNull(session);
            // Once for start session, once for abort. Second will throw exception if start session due to empty response.
            mockClient.Verify(x => x.SendCommandAsync(It.IsAny <SendCommandRequest>(), It.IsAny <CancellationToken>()),
                              Times.Exactly(2));
        }
        public void TestDisposeSessionReleasesSemaphore()
        {
            var driver  = new PooledQldbDriver("ledgerName", mockClient.Object, 4, 1, 10, NullLogger.Instance);
            var session = driver.GetSession();

            session.Dispose();
            // Pool limit is 1 so will throw TimeoutException if semaphore was not released upon dispose.
            session = driver.GetSession();
            Assert.IsNotNull(session);
        }
        public void TestGetSessionReturnsValidSession()
        {
            var driver  = new PooledQldbDriver("ledgerName", mockClient.Object, 4, 4, 4, NullLogger.Instance);
            var session = driver.GetSession();

            Assert.IsNotNull(session);
            // Start session.
            mockClient.Verify(x => x.SendCommandAsync(It.IsAny <SendCommandRequest>(), It.IsAny <CancellationToken>()),
                              Times.Once);
        }
        public void TestGetSessionWithNoneInPoolAndTimeoutReached()
        {
            var driver  = new PooledQldbDriver("ledgerName", mockClient.Object, 4, 1, 10, NullLogger.Instance);
            var session = driver.GetSession();

            Assert.IsNotNull(session);
            Assert.ThrowsException <TimeoutException>(driver.GetSession);
            // Start session.
            mockClient.Verify(x => x.SendCommandAsync(It.IsAny <SendCommandRequest>(), It.IsAny <CancellationToken>()),
                              Times.Once);
        }
        public void TestDisposeEndsPooledSessions()
        {
            var driver  = new PooledQldbDriver("ledgerName", mockClient.Object, 4, 4, 4, NullLogger.Instance);
            var session = driver.GetSession();

            session.Dispose();
            driver.Dispose();
            driver.Dispose();
            // Once to start session, and once to end session in pool upon disposal of driver.
            mockClient.Verify(x => x.SendCommandAsync(It.IsAny <SendCommandRequest>(), It.IsAny <CancellationToken>()),
                              Times.Exactly(2));
        }