From 877c98f7be878d41710b68c5a2de258ec0792c8d Mon Sep 17 00:00:00 2001 From: Timo <38291523+lovetodream@users.noreply.github.com> Date: Wed, 8 May 2024 14:39:43 +0200 Subject: [PATCH 1/8] test: add test case --- .../PoolStateMachineTests.swift | 377 ++++++++++++++++++ 1 file changed, 377 insertions(+) diff --git a/Tests/ConnectionPoolModuleTests/PoolStateMachineTests.swift b/Tests/ConnectionPoolModuleTests/PoolStateMachineTests.swift index f5ada14f..9fa0e498 100644 --- a/Tests/ConnectionPoolModuleTests/PoolStateMachineTests.swift +++ b/Tests/ConnectionPoolModuleTests/PoolStateMachineTests.swift @@ -377,4 +377,381 @@ final class PoolStateMachineTests: XCTestCase { connection.closeIfClosing() } + func testConnectionsRequestedInBurstsWork() { + var configuration = PoolConfiguration() + configuration.minimumConnectionCount = 1 + configuration.maximumConnectionSoftLimit = 20 + configuration.maximumConnectionHardLimit = 20 + configuration.keepAliveDuration = .seconds(30) + configuration.idleTimeoutDuration = .seconds(60) + + + var stateMachine = TestPoolStateMachine( + configuration: configuration, + generator: .init(), + timerCancellationTokenType: MockTimerCancellationToken.self + ) + + func makeKeepAliveTimer(timerID: Int, connectionID: Int) -> (timer: TestPoolStateMachine.Timer, cancellationToken: MockTimerCancellationToken) { + let timer = TestPoolStateMachine.Timer(.init(timerID: timerID, connectionID: connectionID, usecase: .keepAlive), duration: .seconds(30)) + return (timer, MockTimerCancellationToken(timer)) + } + func makeIdleTimer(timerID: Int, connectionID: Int) -> (timer: TestPoolStateMachine.Timer, cancellationToken: MockTimerCancellationToken) { + let timer = TestPoolStateMachine.Timer(.init(timerID: timerID, connectionID: connectionID, usecase: .idleTimeout), duration: .seconds(60)) + return (timer, MockTimerCancellationToken(timer)) + } + + // Connections + let conn8 = MockConnection(id: 8) + let conn14 = MockConnection(id: 14) + let conn11 = MockConnection(id: 11) + let conn3 = MockConnection(id: 3) + let conn16 = MockConnection(id: 16) + let conn13 = MockConnection(id: 13) + let conn10 = MockConnection(id: 10) + let conn7 = MockConnection(id: 7) + let conn5 = MockConnection(id: 5) + let conn18 = MockConnection(id: 18) + let conn9 = MockConnection(id: 9) + let conn0 = MockConnection(id: 0) + let conn4 = MockConnection(id: 4) + let conn19 = MockConnection(id: 19) + let conn6 = MockConnection(id: 6) + let conn12 = MockConnection(id: 12) + let conn17 = MockConnection(id: 17) + let conn15 = MockConnection(id: 15) + let conn2 = MockConnection(id: 2) + let conn1 = MockConnection(id: 1) + + stateMachine.refillConnections() + for _ in 0..<20 { // request 20 connections + stateMachine.leaseConnection(.init()) + } + + stateMachine.connectionEstablished(conn8, maxStreams: 1) + stateMachine.connectionEstablished(conn14, maxStreams: 1) + stateMachine.connectionEstablished(conn11, maxStreams: 1) + stateMachine.connectionEstablished(conn3, maxStreams: 1) + stateMachine.releaseConnection(conn8, streams: 1) + stateMachine.releaseConnection(conn14, streams: 1) + Optional("hello") + Optional("hello") + stateMachine.releaseConnection(conn8, streams: 1) + Optional("hello") + stateMachine.releaseConnection(conn14, streams: 1) + Optional("hello") + stateMachine.releaseConnection(conn11, streams: 1) + stateMachine.releaseConnection(conn8, streams: 1) + stateMachine.releaseConnection(conn14, streams: 1) + Optional("hello") + stateMachine.releaseConnection(conn3, streams: 1) + Optional("hello") + Optional("hello") + Optional("hello") + stateMachine.releaseConnection(conn11, streams: 1) + Optional("hello") + stateMachine.releaseConnection(conn14, streams: 1) + stateMachine.releaseConnection(conn8, streams: 1) + Optional("hello") + stateMachine.releaseConnection(conn3, streams: 1) + Optional("hello") + Optional("hello") + stateMachine.releaseConnection(conn11, streams: 1) + Optional("hello") + stateMachine.releaseConnection(conn8, streams: 1) + Optional("hello") + stateMachine.releaseConnection(conn11, streams: 1) + stateMachine.releaseConnection(conn14, streams: 1) + Optional("hello") + Optional("hello") + stateMachine.releaseConnection(conn8, streams: 1) + stateMachine.releaseConnection(conn3, streams: 1) + Optional("hello") + Optional("hello") + let conn8KeepAliveTimer = makeKeepAliveTimer(timerID: 0, connectionID: 8) + stateMachine.timerScheduled(conn8KeepAliveTimer.timer, cancelContinuation: conn8KeepAliveTimer.cancellationToken) + let conn8IdleTimer = makeIdleTimer(timerID: 1, connectionID: 8) + stateMachine.timerScheduled(conn8IdleTimer.timer, cancelContinuation: conn8IdleTimer.cancellationToken) + stateMachine.releaseConnection(conn14, streams: 1) + stateMachine.releaseConnection(conn11, streams: 1) + Optional("hello") + let conn3IdleTimer = makeIdleTimer(timerID: 1, connectionID: 3) + stateMachine.timerScheduled(conn3IdleTimer.timer, cancelContinuation: conn3IdleTimer.cancellationToken) + Optional("hello") + let conn3KeepAliveTimer = makeKeepAliveTimer(timerID: 0, connectionID: 3) + stateMachine.timerScheduled(conn3KeepAliveTimer.timer, cancelContinuation: conn3KeepAliveTimer.cancellationToken) + stateMachine.connectionEstablished(conn16, maxStreams: 1) + stateMachine.connectionEstablished(conn13, maxStreams: 1) + stateMachine.connectionEstablished(conn10, maxStreams: 1) + stateMachine.connectionEstablished(conn7, maxStreams: 1) + let conn14IdleTimer = makeIdleTimer(timerID: 1, connectionID: 14) + stateMachine.timerScheduled(conn14IdleTimer.timer, cancelContinuation: conn14IdleTimer.cancellationToken) + let conn14KeepAliveTimer = makeKeepAliveTimer(timerID: 0, connectionID: 14) + stateMachine.timerScheduled(conn14KeepAliveTimer.timer, cancelContinuation: conn14KeepAliveTimer.cancellationToken) + let conn11IdleTimer = makeIdleTimer(timerID: 1, connectionID: 11) + stateMachine.timerScheduled(conn11IdleTimer.timer, cancelContinuation: conn11IdleTimer.cancellationToken) + let conn11KeepAliveTimer = makeKeepAliveTimer(timerID: 0, connectionID: 11) + stateMachine.timerScheduled(conn11KeepAliveTimer.timer, cancelContinuation: conn11KeepAliveTimer.cancellationToken) + let conn16KeepAliveTimer = makeKeepAliveTimer(timerID: 0, connectionID: 16) + stateMachine.timerScheduled(conn16KeepAliveTimer.timer, cancelContinuation: conn16KeepAliveTimer.cancellationToken) + let conn16IdleTimer = makeIdleTimer(timerID: 1, connectionID: 16) + stateMachine.timerScheduled(conn16IdleTimer.timer, cancelContinuation: conn16IdleTimer.cancellationToken) + let conn13KeepAliveTimer = makeKeepAliveTimer(timerID: 0, connectionID: 13) + stateMachine.timerScheduled(conn13KeepAliveTimer.timer, cancelContinuation: conn13KeepAliveTimer.cancellationToken) + let conn13IdleTimer = makeIdleTimer(timerID: 1, connectionID: 13) + stateMachine.timerScheduled(conn13IdleTimer.timer, cancelContinuation: conn13IdleTimer.cancellationToken) + let conn10KeepAliveTimer = makeKeepAliveTimer(timerID: 0, connectionID: 10) + stateMachine.timerScheduled(conn10KeepAliveTimer.timer, cancelContinuation: conn10KeepAliveTimer.cancellationToken) + let conn10IdleTimer = makeIdleTimer(timerID: 1, connectionID: 10) + stateMachine.timerScheduled(conn10IdleTimer.timer, cancelContinuation: conn10IdleTimer.cancellationToken) + let conn7KeepAliveTimer = makeKeepAliveTimer(timerID: 0, connectionID: 7) + stateMachine.timerScheduled(conn7KeepAliveTimer.timer, cancelContinuation: conn7KeepAliveTimer.cancellationToken) + let conn7IdleTimer = makeIdleTimer(timerID: 1, connectionID: 7) + stateMachine.timerScheduled(conn7IdleTimer.timer, cancelContinuation: conn7IdleTimer.cancellationToken) + stateMachine.connectionEstablished(conn5, maxStreams: 1) + let conn5KeepAliveTimer = makeKeepAliveTimer(timerID: 0, connectionID: 5) + stateMachine.timerScheduled(conn5KeepAliveTimer.timer, cancelContinuation: conn5KeepAliveTimer.cancellationToken) + let conn5IdleTimer = makeIdleTimer(timerID: 1, connectionID: 5) + stateMachine.timerScheduled(conn5IdleTimer.timer, cancelContinuation: conn5IdleTimer.cancellationToken) + stateMachine.connectionEstablished(conn18, maxStreams: 1) + let conn18KeepAliveTimer = makeKeepAliveTimer(timerID: 0, connectionID: 18) + stateMachine.timerScheduled(conn18KeepAliveTimer.timer, cancelContinuation: conn18KeepAliveTimer.cancellationToken) + let conn18IdleTimer = makeIdleTimer(timerID: 1, connectionID: 18) + stateMachine.timerScheduled(conn18IdleTimer.timer, cancelContinuation: conn18IdleTimer.cancellationToken) + stateMachine.connectionEstablished(conn9, maxStreams: 1) + let conn9IdleTimer = makeIdleTimer(timerID: 1, connectionID: 9) + stateMachine.timerScheduled(conn9IdleTimer.timer, cancelContinuation: conn9IdleTimer.cancellationToken) + let conn9KeepAliveTimer = makeKeepAliveTimer(timerID: 0, connectionID: 9) + stateMachine.timerScheduled(conn9KeepAliveTimer.timer, cancelContinuation: conn9KeepAliveTimer.cancellationToken) + stateMachine.connectionEstablished(conn0, maxStreams: 1) + let conn0KeepAliveTimer = makeKeepAliveTimer(timerID: 0, connectionID: 0) + stateMachine.timerScheduled(conn0KeepAliveTimer.timer, cancelContinuation: conn0KeepAliveTimer.cancellationToken) + let conn0IdleTimer = makeIdleTimer(timerID: 1, connectionID: 0) + stateMachine.timerScheduled(conn0IdleTimer.timer, cancelContinuation: conn0IdleTimer.cancellationToken) + stateMachine.connectionEstablished(conn4, maxStreams: 1) + stateMachine.connectionEstablished(conn19, maxStreams: 1) + let conn4KeepAliveTimer = makeKeepAliveTimer(timerID: 0, connectionID: 4) + stateMachine.timerScheduled(conn4KeepAliveTimer.timer, cancelContinuation: conn4KeepAliveTimer.cancellationToken) + let conn19IdleTimer = makeIdleTimer(timerID: 1, connectionID: 19) + stateMachine.timerScheduled(conn19IdleTimer.timer, cancelContinuation: conn19IdleTimer.cancellationToken) + stateMachine.connectionEstablished(conn6, maxStreams: 1) + let conn4IdleTimer = makeIdleTimer(timerID: 1, connectionID: 4) + stateMachine.timerScheduled(conn4IdleTimer.timer, cancelContinuation: conn4IdleTimer.cancellationToken) + let conn19KeepAliveTimer = makeKeepAliveTimer(timerID: 0, connectionID: 19) + stateMachine.timerScheduled(conn19KeepAliveTimer.timer, cancelContinuation: conn19KeepAliveTimer.cancellationToken) + let conn6KeepAliveTimer = makeKeepAliveTimer(timerID: 0, connectionID: 6) + stateMachine.timerScheduled(conn6KeepAliveTimer.timer, cancelContinuation: conn6KeepAliveTimer.cancellationToken) + stateMachine.connectionEstablished(conn12, maxStreams: 1) + let conn6IdleTimer = makeIdleTimer(timerID: 1, connectionID: 6) + stateMachine.timerScheduled(conn6IdleTimer.timer, cancelContinuation: conn6IdleTimer.cancellationToken) + stateMachine.connectionEstablished(conn17, maxStreams: 1) + stateMachine.connectionEstablished(conn15, maxStreams: 1) + let conn12KeepAliveTimer = makeKeepAliveTimer(timerID: 0, connectionID: 12) + stateMachine.timerScheduled(conn12KeepAliveTimer.timer, cancelContinuation: conn12KeepAliveTimer.cancellationToken) + stateMachine.connectionEstablished(conn2, maxStreams: 1) + stateMachine.connectionEstablished(conn1, maxStreams: 1) + let conn12IdleTimer = makeIdleTimer(timerID: 1, connectionID: 12) + stateMachine.timerScheduled(conn12IdleTimer.timer, cancelContinuation: conn12IdleTimer.cancellationToken) + let conn17KeepAliveTimer = makeKeepAliveTimer(timerID: 0, connectionID: 17) + stateMachine.timerScheduled(conn17KeepAliveTimer.timer, cancelContinuation: conn17KeepAliveTimer.cancellationToken) + let conn17IdleTimer = makeIdleTimer(timerID: 1, connectionID: 17) + stateMachine.timerScheduled(conn17IdleTimer.timer, cancelContinuation: conn17IdleTimer.cancellationToken) + let conn15KeepAliveTimer = makeKeepAliveTimer(timerID: 0, connectionID: 15) + stateMachine.timerScheduled(conn15KeepAliveTimer.timer, cancelContinuation: conn15KeepAliveTimer.cancellationToken) + let conn15IdleTimer = makeIdleTimer(timerID: 1, connectionID: 15) + stateMachine.timerScheduled(conn15IdleTimer.timer, cancelContinuation: conn15IdleTimer.cancellationToken) + let conn2KeepAliveTimer = makeKeepAliveTimer(timerID: 0, connectionID: 2) + stateMachine.timerScheduled(conn2KeepAliveTimer.timer, cancelContinuation: conn2KeepAliveTimer.cancellationToken) + let conn2IdleTimer = makeIdleTimer(timerID: 1, connectionID: 2) + stateMachine.timerScheduled(conn2IdleTimer.timer, cancelContinuation: conn2IdleTimer.cancellationToken) + let conn1KeepAliveTimer = makeKeepAliveTimer(timerID: 0, connectionID: 1) + stateMachine.timerScheduled(conn1KeepAliveTimer.timer, cancelContinuation: conn1KeepAliveTimer.cancellationToken) + let conn1IdleTimer = makeIdleTimer(timerID: 0, connectionID: 1) + stateMachine.timerScheduled(conn1IdleTimer.timer, cancelContinuation: conn1IdleTimer.cancellationToken) + + stateMachine.timerTriggered(conn14KeepAliveTimer.timer) +// stateMachine.connectionKeepAliveTimerTriggered(14) + stateMachine.timerTriggered(conn8KeepAliveTimer.timer) +// stateMachine.connectionKeepAliveTimerTriggered(8) + stateMachine.timerTriggered(conn0KeepAliveTimer.timer) +// stateMachine.connectionKeepAliveTimerTriggered(0) + stateMachine.timerTriggered(conn12KeepAliveTimer.timer) +// stateMachine.connectionKeepAliveTimerTriggered(12) + stateMachine.timerTriggered(conn4KeepAliveTimer.timer) +// stateMachine.connectionKeepAliveTimerTriggered(4) + stateMachine.timerTriggered(conn6KeepAliveTimer.timer) +// stateMachine.connectionKeepAliveTimerTriggered(6) + stateMachine.timerTriggered(conn5KeepAliveTimer.timer) +// stateMachine.connectionKeepAliveTimerTriggered(5) + stateMachine.timerTriggered(conn10KeepAliveTimer.timer) +// stateMachine.connectionKeepAliveTimerTriggered(10) + stateMachine.timerTriggered(conn18KeepAliveTimer.timer) +// stateMachine.connectionKeepAliveTimerTriggered(18) + stateMachine.timerTriggered(conn15KeepAliveTimer.timer) +// stateMachine.connectionKeepAliveTimerTriggered(15) + stateMachine.timerTriggered(conn1KeepAliveTimer.timer) +// stateMachine.connectionKeepAliveTimerTriggered(1) + stateMachine.timerTriggered(conn3KeepAliveTimer.timer) +// stateMachine.connectionKeepAliveTimerTriggered(3) + stateMachine.timerTriggered(conn16KeepAliveTimer.timer) +// stateMachine.connectionKeepAliveTimerTriggered(16) + stateMachine.timerTriggered(conn7KeepAliveTimer.timer) +// stateMachine.connectionKeepAliveTimerTriggered(7) + stateMachine.timerTriggered(conn11KeepAliveTimer.timer) +// stateMachine.connectionKeepAliveTimerTriggered(11) + stateMachine.timerTriggered(conn19KeepAliveTimer.timer) +// stateMachine.connectionKeepAliveTimerTriggered(19) + stateMachine.timerTriggered(conn17KeepAliveTimer.timer) +// stateMachine.connectionKeepAliveTimerTriggered(17) + stateMachine.timerTriggered(conn2KeepAliveTimer.timer) +// stateMachine.connectionKeepAliveTimerTriggered(2) + stateMachine.timerTriggered(conn9KeepAliveTimer.timer) +// stateMachine.connectionKeepAliveTimerTriggered(9) + stateMachine.timerTriggered(conn13KeepAliveTimer.timer) +// stateMachine.connectionKeepAliveTimerTriggered(13) + + stateMachine.connectionKeepAliveDone(conn14) + stateMachine.connectionKeepAliveDone(conn8) + stateMachine.connectionKeepAliveDone(conn0) + stateMachine.connectionKeepAliveDone(conn10) + let conn14KeepAliveTimer2 = makeKeepAliveTimer(timerID: 2, connectionID: 14) + stateMachine.timerScheduled(conn14KeepAliveTimer2.timer, cancelContinuation: conn14KeepAliveTimer2.cancellationToken) + stateMachine.connectionKeepAliveDone(conn12) + let conn0KeepAliveTimer2 = makeKeepAliveTimer(timerID: 2, connectionID: 0) + stateMachine.timerScheduled(conn0KeepAliveTimer2.timer, cancelContinuation: conn0KeepAliveTimer2.cancellationToken) + let conn10KeepAliveTimer2 = makeKeepAliveTimer(timerID: 1, connectionID: 10) + stateMachine.timerScheduled(conn10KeepAliveTimer2.timer, cancelContinuation: conn10KeepAliveTimer2.cancellationToken) + stateMachine.connectionKeepAliveDone(conn3) + let conn8KeepAliveTimer2 = makeKeepAliveTimer(timerID: 2, connectionID: 8) + stateMachine.timerScheduled(conn8KeepAliveTimer2.timer, cancelContinuation: conn8KeepAliveTimer2.cancellationToken) + let conn12KeepAliveTimer2 = makeKeepAliveTimer(timerID: 2, connectionID: 12) + stateMachine.timerScheduled(conn12KeepAliveTimer2.timer, cancelContinuation: conn12KeepAliveTimer2.cancellationToken) + stateMachine.connectionKeepAliveDone(conn6) + let conn3KeepAliveTimer2 = makeKeepAliveTimer(timerID: 2, connectionID: 3) + stateMachine.timerScheduled(conn3KeepAliveTimer2.timer, cancelContinuation: conn3KeepAliveTimer2.cancellationToken) + stateMachine.connectionKeepAliveDone(conn15) + stateMachine.connectionKeepAliveDone(conn16) + stateMachine.connectionKeepAliveDone(conn9) + stateMachine.connectionKeepAliveDone(conn18) + let conn15KeepAliveTimer2 = makeKeepAliveTimer(timerID: 2, connectionID: 15) + stateMachine.timerScheduled(conn15KeepAliveTimer2.timer, cancelContinuation: conn15KeepAliveTimer2.cancellationToken) + let conn6KeepAliveTimer2 = makeKeepAliveTimer(timerID: 2, connectionID: 6) + stateMachine.timerScheduled(conn6KeepAliveTimer2.timer, cancelContinuation: conn6KeepAliveTimer2.cancellationToken) + let conn16KeepAliveTimer2 = makeKeepAliveTimer(timerID: 2, connectionID: 16) + stateMachine.timerScheduled(conn16KeepAliveTimer2.timer, cancelContinuation: conn16KeepAliveTimer2.cancellationToken) + let conn9KeepAliveTimer2 = makeKeepAliveTimer(timerID: 2, connectionID: 9) + stateMachine.timerScheduled(conn9KeepAliveTimer2.timer, cancelContinuation: conn9KeepAliveTimer2.cancellationToken) + stateMachine.connectionKeepAliveDone(conn1) + let conn18KeepAliveTimer2 = makeKeepAliveTimer(timerID: 2, connectionID: 18) + stateMachine.timerScheduled(conn18KeepAliveTimer2.timer, cancelContinuation: conn18KeepAliveTimer2.cancellationToken) + stateMachine.connectionKeepAliveDone(conn7) + stateMachine.connectionKeepAliveDone(conn13) + stateMachine.connectionKeepAliveDone(conn17) + stateMachine.connectionKeepAliveDone(conn19) + let conn1KeepAliveTimer2 = makeKeepAliveTimer(timerID: 2, connectionID: 1) + stateMachine.timerScheduled(conn1KeepAliveTimer2.timer, cancelContinuation: conn1KeepAliveTimer2.cancellationToken) + stateMachine.connectionKeepAliveDone(conn2) + let conn7KeepAliveTimer2 = makeKeepAliveTimer(timerID: 2, connectionID: 7) + stateMachine.timerScheduled(conn7KeepAliveTimer2.timer, cancelContinuation: conn7KeepAliveTimer2.cancellationToken) + stateMachine.connectionKeepAliveDone(conn5) + let conn13KeepAliveTimer2 = makeKeepAliveTimer(timerID: 2, connectionID: 13) + stateMachine.timerScheduled(conn13KeepAliveTimer2.timer, cancelContinuation: conn13KeepAliveTimer2.cancellationToken) + stateMachine.connectionKeepAliveDone(conn11) + let conn17KeepAliveTimer2 = makeKeepAliveTimer(timerID: 2, connectionID: 17) + stateMachine.timerScheduled(conn17KeepAliveTimer2.timer, cancelContinuation: conn17KeepAliveTimer2.cancellationToken) + stateMachine.connectionKeepAliveDone(conn4) + let conn19KeepAliveTimer2 = makeKeepAliveTimer(timerID: 2, connectionID: 19) + stateMachine.timerScheduled(conn19KeepAliveTimer2.timer, cancelContinuation: conn19KeepAliveTimer2.cancellationToken) + let conn2KeepAliveTimer2 = makeKeepAliveTimer(timerID: 2, connectionID: 2) + stateMachine.timerScheduled(conn2KeepAliveTimer2.timer, cancelContinuation: conn2KeepAliveTimer2.cancellationToken) + let conn5KeepAliveTimer2 = makeKeepAliveTimer(timerID: 2, connectionID: 5) + stateMachine.timerScheduled(conn5KeepAliveTimer2.timer, cancelContinuation: conn5KeepAliveTimer2.cancellationToken) + let conn11KeepAliveTimer2 = makeKeepAliveTimer(timerID: 2, connectionID: 11) + stateMachine.timerScheduled(conn11KeepAliveTimer2.timer, cancelContinuation: conn11KeepAliveTimer2.cancellationToken) + let conn4KeepAliveTimer2 = makeKeepAliveTimer(timerID: 2, connectionID: 4) + stateMachine.timerScheduled(conn4KeepAliveTimer2.timer, cancelContinuation: conn4KeepAliveTimer2.cancellationToken) + stateMachine.timerTriggered(conn3IdleTimer.timer) +// stateMachine.connectionIdleTimerTriggered(3) + stateMachine.timerTriggered(conn10IdleTimer.timer) +// stateMachine.connectionIdleTimerTriggered(10) + stateMachine.timerTriggered(conn7IdleTimer.timer) +// stateMachine.connectionIdleTimerTriggered(7) + stateMachine.timerTriggered(conn13IdleTimer.timer) +// stateMachine.connectionIdleTimerTriggered(13) + stateMachine.timerTriggered(conn18IdleTimer.timer) +// stateMachine.connectionIdleTimerTriggered(18) + stateMachine.timerTriggered(conn5IdleTimer.timer) +// stateMachine.connectionIdleTimerTriggered(5) + stateMachine.timerTriggered(conn9IdleTimer.timer) +// stateMachine.connectionIdleTimerTriggered(9) + stateMachine.timerTriggered(conn0IdleTimer.timer) +// stateMachine.connectionIdleTimerTriggered(0) + stateMachine.timerTriggered(conn4IdleTimer.timer) +// stateMachine.connectionIdleTimerTriggered(4) + stateMachine.timerTriggered(conn19IdleTimer.timer) +// stateMachine.connectionIdleTimerTriggered(19) + stateMachine.timerTriggered(conn6IdleTimer.timer) +// stateMachine.connectionIdleTimerTriggered(6) + stateMachine.timerTriggered(conn12IdleTimer.timer) +// stateMachine.connectionIdleTimerTriggered(12) + stateMachine.timerTriggered(conn17IdleTimer.timer) +// stateMachine.connectionIdleTimerTriggered(17) + stateMachine.timerTriggered(conn2IdleTimer.timer) +// stateMachine.connectionIdleTimerTriggered(2) + stateMachine.timerTriggered(conn15IdleTimer.timer) +// stateMachine.connectionIdleTimerTriggered(15) + stateMachine.timerTriggered(conn1IdleTimer.timer) +// stateMachine.connectionIdleTimerTriggered(1) + stateMachine.timerTriggered(conn14KeepAliveTimer2.timer) +// stateMachine.connectionKeepAliveTimerTriggered(14) + stateMachine.timerTriggered(conn8KeepAliveTimer2.timer) +// stateMachine.connectionKeepAliveTimerTriggered(8) + stateMachine.timerTriggered(conn14IdleTimer.timer) +// stateMachine.connectionIdleTimerTriggered(14) + stateMachine.timerTriggered(conn16KeepAliveTimer2.timer) +// stateMachine.connectionKeepAliveTimerTriggered(16) + stateMachine.timerTriggered(conn18KeepAliveTimer2.timer) +// stateMachine.connectionKeepAliveTimerTriggered(18) + stateMachine.timerTriggered(conn9KeepAliveTimer2.timer) +// stateMachine.connectionKeepAliveTimerTriggered(9) + stateMachine.timerTriggered(conn8IdleTimer.timer) +// stateMachine.connectionIdleTimerTriggered(8) + stateMachine.timerTriggered(conn10KeepAliveTimer2.timer) +// stateMachine.connectionKeepAliveTimerTriggered(10) + stateMachine.timerTriggered(conn17KeepAliveTimer2.timer) +// stateMachine.connectionKeepAliveTimerTriggered(17) + stateMachine.timerTriggered(conn19KeepAliveTimer2.timer) +// stateMachine.connectionKeepAliveTimerTriggered(19) + stateMachine.timerTriggered(conn6KeepAliveTimer2.timer) +// stateMachine.connectionKeepAliveTimerTriggered(6) + stateMachine.timerTriggered(conn2KeepAliveTimer2.timer) +// stateMachine.connectionKeepAliveTimerTriggered(2) + stateMachine.timerTriggered(conn5KeepAliveTimer2.timer) +// stateMachine.connectionKeepAliveTimerTriggered(5) + stateMachine.timerTriggered(conn11KeepAliveTimer2.timer) +// stateMachine.connectionKeepAliveTimerTriggered(11) + stateMachine.timerTriggered(conn1KeepAliveTimer2.timer) +// stateMachine.connectionKeepAliveTimerTriggered(1) + // Burst done: 1/50 + stateMachine.timerTriggered(conn0KeepAliveTimer2.timer) +// stateMachine.connectionKeepAliveTimerTriggered(0) + stateMachine.timerTriggered(conn7KeepAliveTimer2.timer) +// stateMachine.connectionKeepAliveTimerTriggered(7) + stateMachine.timerTriggered(conn3KeepAliveTimer2.timer) +// stateMachine.connectionKeepAliveTimerTriggered(3) + stateMachine.leaseConnection(MockRequest()) + stateMachine.connectionClosed(conn5) + stateMachine.connectionClosed(conn6) + stateMachine.connectionClosed(conn3) + stateMachine.connectionClosed(conn19) + stateMachine.connectionClosed(conn10) + stateMachine.connectionClosed(conn12) + stateMachine.connectionClosed(conn17) + stateMachine.connectionClosed(conn18) + stateMachine.connectionClosed(conn9) + stateMachine.connectionClosed(conn1) + stateMachine.timerTriggered(conn13KeepAliveTimer2.timer) +// stateMachine.connectionKeepAliveTimerTriggered(13) + } + } From 66b894e94e02426f840c13fa351da3cc4e4d3ddc Mon Sep 17 00:00:00 2001 From: Timo <38291523+lovetodream@users.noreply.github.com> Date: Wed, 8 May 2024 14:44:15 +0200 Subject: [PATCH 2/8] test: remove commented out stuff --- .../PoolStateMachineTests.swift | 75 ------------------- 1 file changed, 75 deletions(-) diff --git a/Tests/ConnectionPoolModuleTests/PoolStateMachineTests.swift b/Tests/ConnectionPoolModuleTests/PoolStateMachineTests.swift index 9fa0e498..67f13d27 100644 --- a/Tests/ConnectionPoolModuleTests/PoolStateMachineTests.swift +++ b/Tests/ConnectionPoolModuleTests/PoolStateMachineTests.swift @@ -434,50 +434,30 @@ final class PoolStateMachineTests: XCTestCase { stateMachine.connectionEstablished(conn3, maxStreams: 1) stateMachine.releaseConnection(conn8, streams: 1) stateMachine.releaseConnection(conn14, streams: 1) - Optional("hello") - Optional("hello") stateMachine.releaseConnection(conn8, streams: 1) - Optional("hello") stateMachine.releaseConnection(conn14, streams: 1) - Optional("hello") stateMachine.releaseConnection(conn11, streams: 1) stateMachine.releaseConnection(conn8, streams: 1) stateMachine.releaseConnection(conn14, streams: 1) - Optional("hello") stateMachine.releaseConnection(conn3, streams: 1) - Optional("hello") - Optional("hello") - Optional("hello") stateMachine.releaseConnection(conn11, streams: 1) - Optional("hello") stateMachine.releaseConnection(conn14, streams: 1) stateMachine.releaseConnection(conn8, streams: 1) - Optional("hello") stateMachine.releaseConnection(conn3, streams: 1) - Optional("hello") - Optional("hello") stateMachine.releaseConnection(conn11, streams: 1) - Optional("hello") stateMachine.releaseConnection(conn8, streams: 1) - Optional("hello") stateMachine.releaseConnection(conn11, streams: 1) stateMachine.releaseConnection(conn14, streams: 1) - Optional("hello") - Optional("hello") stateMachine.releaseConnection(conn8, streams: 1) stateMachine.releaseConnection(conn3, streams: 1) - Optional("hello") - Optional("hello") let conn8KeepAliveTimer = makeKeepAliveTimer(timerID: 0, connectionID: 8) stateMachine.timerScheduled(conn8KeepAliveTimer.timer, cancelContinuation: conn8KeepAliveTimer.cancellationToken) let conn8IdleTimer = makeIdleTimer(timerID: 1, connectionID: 8) stateMachine.timerScheduled(conn8IdleTimer.timer, cancelContinuation: conn8IdleTimer.cancellationToken) stateMachine.releaseConnection(conn14, streams: 1) stateMachine.releaseConnection(conn11, streams: 1) - Optional("hello") let conn3IdleTimer = makeIdleTimer(timerID: 1, connectionID: 3) stateMachine.timerScheduled(conn3IdleTimer.timer, cancelContinuation: conn3IdleTimer.cancellationToken) - Optional("hello") let conn3KeepAliveTimer = makeKeepAliveTimer(timerID: 0, connectionID: 3) stateMachine.timerScheduled(conn3KeepAliveTimer.timer, cancelContinuation: conn3KeepAliveTimer.cancellationToken) stateMachine.connectionEstablished(conn16, maxStreams: 1) @@ -570,45 +550,25 @@ final class PoolStateMachineTests: XCTestCase { stateMachine.timerScheduled(conn1IdleTimer.timer, cancelContinuation: conn1IdleTimer.cancellationToken) stateMachine.timerTriggered(conn14KeepAliveTimer.timer) -// stateMachine.connectionKeepAliveTimerTriggered(14) stateMachine.timerTriggered(conn8KeepAliveTimer.timer) -// stateMachine.connectionKeepAliveTimerTriggered(8) stateMachine.timerTriggered(conn0KeepAliveTimer.timer) -// stateMachine.connectionKeepAliveTimerTriggered(0) stateMachine.timerTriggered(conn12KeepAliveTimer.timer) -// stateMachine.connectionKeepAliveTimerTriggered(12) stateMachine.timerTriggered(conn4KeepAliveTimer.timer) -// stateMachine.connectionKeepAliveTimerTriggered(4) stateMachine.timerTriggered(conn6KeepAliveTimer.timer) -// stateMachine.connectionKeepAliveTimerTriggered(6) stateMachine.timerTriggered(conn5KeepAliveTimer.timer) -// stateMachine.connectionKeepAliveTimerTriggered(5) stateMachine.timerTriggered(conn10KeepAliveTimer.timer) -// stateMachine.connectionKeepAliveTimerTriggered(10) stateMachine.timerTriggered(conn18KeepAliveTimer.timer) -// stateMachine.connectionKeepAliveTimerTriggered(18) stateMachine.timerTriggered(conn15KeepAliveTimer.timer) -// stateMachine.connectionKeepAliveTimerTriggered(15) stateMachine.timerTriggered(conn1KeepAliveTimer.timer) -// stateMachine.connectionKeepAliveTimerTriggered(1) stateMachine.timerTriggered(conn3KeepAliveTimer.timer) -// stateMachine.connectionKeepAliveTimerTriggered(3) stateMachine.timerTriggered(conn16KeepAliveTimer.timer) -// stateMachine.connectionKeepAliveTimerTriggered(16) stateMachine.timerTriggered(conn7KeepAliveTimer.timer) -// stateMachine.connectionKeepAliveTimerTriggered(7) stateMachine.timerTriggered(conn11KeepAliveTimer.timer) -// stateMachine.connectionKeepAliveTimerTriggered(11) stateMachine.timerTriggered(conn19KeepAliveTimer.timer) -// stateMachine.connectionKeepAliveTimerTriggered(19) stateMachine.timerTriggered(conn17KeepAliveTimer.timer) -// stateMachine.connectionKeepAliveTimerTriggered(17) stateMachine.timerTriggered(conn2KeepAliveTimer.timer) -// stateMachine.connectionKeepAliveTimerTriggered(2) stateMachine.timerTriggered(conn9KeepAliveTimer.timer) -// stateMachine.connectionKeepAliveTimerTriggered(9) stateMachine.timerTriggered(conn13KeepAliveTimer.timer) -// stateMachine.connectionKeepAliveTimerTriggered(13) stateMachine.connectionKeepAliveDone(conn14) stateMachine.connectionKeepAliveDone(conn8) @@ -671,74 +631,40 @@ final class PoolStateMachineTests: XCTestCase { let conn4KeepAliveTimer2 = makeKeepAliveTimer(timerID: 2, connectionID: 4) stateMachine.timerScheduled(conn4KeepAliveTimer2.timer, cancelContinuation: conn4KeepAliveTimer2.cancellationToken) stateMachine.timerTriggered(conn3IdleTimer.timer) -// stateMachine.connectionIdleTimerTriggered(3) stateMachine.timerTriggered(conn10IdleTimer.timer) -// stateMachine.connectionIdleTimerTriggered(10) stateMachine.timerTriggered(conn7IdleTimer.timer) -// stateMachine.connectionIdleTimerTriggered(7) stateMachine.timerTriggered(conn13IdleTimer.timer) -// stateMachine.connectionIdleTimerTriggered(13) stateMachine.timerTriggered(conn18IdleTimer.timer) -// stateMachine.connectionIdleTimerTriggered(18) stateMachine.timerTriggered(conn5IdleTimer.timer) -// stateMachine.connectionIdleTimerTriggered(5) stateMachine.timerTriggered(conn9IdleTimer.timer) -// stateMachine.connectionIdleTimerTriggered(9) stateMachine.timerTriggered(conn0IdleTimer.timer) -// stateMachine.connectionIdleTimerTriggered(0) stateMachine.timerTriggered(conn4IdleTimer.timer) -// stateMachine.connectionIdleTimerTriggered(4) stateMachine.timerTriggered(conn19IdleTimer.timer) -// stateMachine.connectionIdleTimerTriggered(19) stateMachine.timerTriggered(conn6IdleTimer.timer) -// stateMachine.connectionIdleTimerTriggered(6) stateMachine.timerTriggered(conn12IdleTimer.timer) -// stateMachine.connectionIdleTimerTriggered(12) stateMachine.timerTriggered(conn17IdleTimer.timer) -// stateMachine.connectionIdleTimerTriggered(17) stateMachine.timerTriggered(conn2IdleTimer.timer) -// stateMachine.connectionIdleTimerTriggered(2) stateMachine.timerTriggered(conn15IdleTimer.timer) -// stateMachine.connectionIdleTimerTriggered(15) stateMachine.timerTriggered(conn1IdleTimer.timer) -// stateMachine.connectionIdleTimerTriggered(1) stateMachine.timerTriggered(conn14KeepAliveTimer2.timer) -// stateMachine.connectionKeepAliveTimerTriggered(14) stateMachine.timerTriggered(conn8KeepAliveTimer2.timer) -// stateMachine.connectionKeepAliveTimerTriggered(8) stateMachine.timerTriggered(conn14IdleTimer.timer) -// stateMachine.connectionIdleTimerTriggered(14) stateMachine.timerTriggered(conn16KeepAliveTimer2.timer) -// stateMachine.connectionKeepAliveTimerTriggered(16) stateMachine.timerTriggered(conn18KeepAliveTimer2.timer) -// stateMachine.connectionKeepAliveTimerTriggered(18) stateMachine.timerTriggered(conn9KeepAliveTimer2.timer) -// stateMachine.connectionKeepAliveTimerTriggered(9) stateMachine.timerTriggered(conn8IdleTimer.timer) -// stateMachine.connectionIdleTimerTriggered(8) stateMachine.timerTriggered(conn10KeepAliveTimer2.timer) -// stateMachine.connectionKeepAliveTimerTriggered(10) stateMachine.timerTriggered(conn17KeepAliveTimer2.timer) -// stateMachine.connectionKeepAliveTimerTriggered(17) stateMachine.timerTriggered(conn19KeepAliveTimer2.timer) -// stateMachine.connectionKeepAliveTimerTriggered(19) stateMachine.timerTriggered(conn6KeepAliveTimer2.timer) -// stateMachine.connectionKeepAliveTimerTriggered(6) stateMachine.timerTriggered(conn2KeepAliveTimer2.timer) -// stateMachine.connectionKeepAliveTimerTriggered(2) stateMachine.timerTriggered(conn5KeepAliveTimer2.timer) -// stateMachine.connectionKeepAliveTimerTriggered(5) stateMachine.timerTriggered(conn11KeepAliveTimer2.timer) -// stateMachine.connectionKeepAliveTimerTriggered(11) stateMachine.timerTriggered(conn1KeepAliveTimer2.timer) -// stateMachine.connectionKeepAliveTimerTriggered(1) // Burst done: 1/50 stateMachine.timerTriggered(conn0KeepAliveTimer2.timer) -// stateMachine.connectionKeepAliveTimerTriggered(0) stateMachine.timerTriggered(conn7KeepAliveTimer2.timer) -// stateMachine.connectionKeepAliveTimerTriggered(7) stateMachine.timerTriggered(conn3KeepAliveTimer2.timer) -// stateMachine.connectionKeepAliveTimerTriggered(3) stateMachine.leaseConnection(MockRequest()) stateMachine.connectionClosed(conn5) stateMachine.connectionClosed(conn6) @@ -751,7 +677,6 @@ final class PoolStateMachineTests: XCTestCase { stateMachine.connectionClosed(conn9) stateMachine.connectionClosed(conn1) stateMachine.timerTriggered(conn13KeepAliveTimer2.timer) -// stateMachine.connectionKeepAliveTimerTriggered(13) } } From 5b7f39dde1827d6ea4ee1a027bd4ca834a4f8a7b Mon Sep 17 00:00:00 2001 From: Timo <38291523+lovetodream@users.noreply.github.com> Date: Wed, 8 May 2024 14:58:52 +0200 Subject: [PATCH 3/8] test: remove irrelevant keepalives and releases --- .../PoolStateMachineTests.swift | 101 ------------------ 1 file changed, 101 deletions(-) diff --git a/Tests/ConnectionPoolModuleTests/PoolStateMachineTests.swift b/Tests/ConnectionPoolModuleTests/PoolStateMachineTests.swift index 67f13d27..65c044a2 100644 --- a/Tests/ConnectionPoolModuleTests/PoolStateMachineTests.swift +++ b/Tests/ConnectionPoolModuleTests/PoolStateMachineTests.swift @@ -432,167 +432,75 @@ final class PoolStateMachineTests: XCTestCase { stateMachine.connectionEstablished(conn14, maxStreams: 1) stateMachine.connectionEstablished(conn11, maxStreams: 1) stateMachine.connectionEstablished(conn3, maxStreams: 1) - stateMachine.releaseConnection(conn8, streams: 1) - stateMachine.releaseConnection(conn14, streams: 1) - stateMachine.releaseConnection(conn8, streams: 1) - stateMachine.releaseConnection(conn14, streams: 1) - stateMachine.releaseConnection(conn11, streams: 1) - stateMachine.releaseConnection(conn8, streams: 1) - stateMachine.releaseConnection(conn14, streams: 1) - stateMachine.releaseConnection(conn3, streams: 1) - stateMachine.releaseConnection(conn11, streams: 1) - stateMachine.releaseConnection(conn14, streams: 1) - stateMachine.releaseConnection(conn8, streams: 1) - stateMachine.releaseConnection(conn3, streams: 1) - stateMachine.releaseConnection(conn11, streams: 1) - stateMachine.releaseConnection(conn8, streams: 1) - stateMachine.releaseConnection(conn11, streams: 1) - stateMachine.releaseConnection(conn14, streams: 1) - stateMachine.releaseConnection(conn8, streams: 1) - stateMachine.releaseConnection(conn3, streams: 1) - let conn8KeepAliveTimer = makeKeepAliveTimer(timerID: 0, connectionID: 8) - stateMachine.timerScheduled(conn8KeepAliveTimer.timer, cancelContinuation: conn8KeepAliveTimer.cancellationToken) let conn8IdleTimer = makeIdleTimer(timerID: 1, connectionID: 8) stateMachine.timerScheduled(conn8IdleTimer.timer, cancelContinuation: conn8IdleTimer.cancellationToken) - stateMachine.releaseConnection(conn14, streams: 1) - stateMachine.releaseConnection(conn11, streams: 1) let conn3IdleTimer = makeIdleTimer(timerID: 1, connectionID: 3) stateMachine.timerScheduled(conn3IdleTimer.timer, cancelContinuation: conn3IdleTimer.cancellationToken) - let conn3KeepAliveTimer = makeKeepAliveTimer(timerID: 0, connectionID: 3) - stateMachine.timerScheduled(conn3KeepAliveTimer.timer, cancelContinuation: conn3KeepAliveTimer.cancellationToken) stateMachine.connectionEstablished(conn16, maxStreams: 1) stateMachine.connectionEstablished(conn13, maxStreams: 1) stateMachine.connectionEstablished(conn10, maxStreams: 1) stateMachine.connectionEstablished(conn7, maxStreams: 1) let conn14IdleTimer = makeIdleTimer(timerID: 1, connectionID: 14) stateMachine.timerScheduled(conn14IdleTimer.timer, cancelContinuation: conn14IdleTimer.cancellationToken) - let conn14KeepAliveTimer = makeKeepAliveTimer(timerID: 0, connectionID: 14) - stateMachine.timerScheduled(conn14KeepAliveTimer.timer, cancelContinuation: conn14KeepAliveTimer.cancellationToken) let conn11IdleTimer = makeIdleTimer(timerID: 1, connectionID: 11) stateMachine.timerScheduled(conn11IdleTimer.timer, cancelContinuation: conn11IdleTimer.cancellationToken) - let conn11KeepAliveTimer = makeKeepAliveTimer(timerID: 0, connectionID: 11) - stateMachine.timerScheduled(conn11KeepAliveTimer.timer, cancelContinuation: conn11KeepAliveTimer.cancellationToken) - let conn16KeepAliveTimer = makeKeepAliveTimer(timerID: 0, connectionID: 16) - stateMachine.timerScheduled(conn16KeepAliveTimer.timer, cancelContinuation: conn16KeepAliveTimer.cancellationToken) let conn16IdleTimer = makeIdleTimer(timerID: 1, connectionID: 16) stateMachine.timerScheduled(conn16IdleTimer.timer, cancelContinuation: conn16IdleTimer.cancellationToken) - let conn13KeepAliveTimer = makeKeepAliveTimer(timerID: 0, connectionID: 13) - stateMachine.timerScheduled(conn13KeepAliveTimer.timer, cancelContinuation: conn13KeepAliveTimer.cancellationToken) let conn13IdleTimer = makeIdleTimer(timerID: 1, connectionID: 13) stateMachine.timerScheduled(conn13IdleTimer.timer, cancelContinuation: conn13IdleTimer.cancellationToken) - let conn10KeepAliveTimer = makeKeepAliveTimer(timerID: 0, connectionID: 10) - stateMachine.timerScheduled(conn10KeepAliveTimer.timer, cancelContinuation: conn10KeepAliveTimer.cancellationToken) let conn10IdleTimer = makeIdleTimer(timerID: 1, connectionID: 10) stateMachine.timerScheduled(conn10IdleTimer.timer, cancelContinuation: conn10IdleTimer.cancellationToken) - let conn7KeepAliveTimer = makeKeepAliveTimer(timerID: 0, connectionID: 7) - stateMachine.timerScheduled(conn7KeepAliveTimer.timer, cancelContinuation: conn7KeepAliveTimer.cancellationToken) let conn7IdleTimer = makeIdleTimer(timerID: 1, connectionID: 7) stateMachine.timerScheduled(conn7IdleTimer.timer, cancelContinuation: conn7IdleTimer.cancellationToken) stateMachine.connectionEstablished(conn5, maxStreams: 1) - let conn5KeepAliveTimer = makeKeepAliveTimer(timerID: 0, connectionID: 5) - stateMachine.timerScheduled(conn5KeepAliveTimer.timer, cancelContinuation: conn5KeepAliveTimer.cancellationToken) let conn5IdleTimer = makeIdleTimer(timerID: 1, connectionID: 5) stateMachine.timerScheduled(conn5IdleTimer.timer, cancelContinuation: conn5IdleTimer.cancellationToken) stateMachine.connectionEstablished(conn18, maxStreams: 1) - let conn18KeepAliveTimer = makeKeepAliveTimer(timerID: 0, connectionID: 18) - stateMachine.timerScheduled(conn18KeepAliveTimer.timer, cancelContinuation: conn18KeepAliveTimer.cancellationToken) let conn18IdleTimer = makeIdleTimer(timerID: 1, connectionID: 18) stateMachine.timerScheduled(conn18IdleTimer.timer, cancelContinuation: conn18IdleTimer.cancellationToken) stateMachine.connectionEstablished(conn9, maxStreams: 1) let conn9IdleTimer = makeIdleTimer(timerID: 1, connectionID: 9) stateMachine.timerScheduled(conn9IdleTimer.timer, cancelContinuation: conn9IdleTimer.cancellationToken) - let conn9KeepAliveTimer = makeKeepAliveTimer(timerID: 0, connectionID: 9) - stateMachine.timerScheduled(conn9KeepAliveTimer.timer, cancelContinuation: conn9KeepAliveTimer.cancellationToken) stateMachine.connectionEstablished(conn0, maxStreams: 1) - let conn0KeepAliveTimer = makeKeepAliveTimer(timerID: 0, connectionID: 0) - stateMachine.timerScheduled(conn0KeepAliveTimer.timer, cancelContinuation: conn0KeepAliveTimer.cancellationToken) let conn0IdleTimer = makeIdleTimer(timerID: 1, connectionID: 0) stateMachine.timerScheduled(conn0IdleTimer.timer, cancelContinuation: conn0IdleTimer.cancellationToken) stateMachine.connectionEstablished(conn4, maxStreams: 1) stateMachine.connectionEstablished(conn19, maxStreams: 1) - let conn4KeepAliveTimer = makeKeepAliveTimer(timerID: 0, connectionID: 4) - stateMachine.timerScheduled(conn4KeepAliveTimer.timer, cancelContinuation: conn4KeepAliveTimer.cancellationToken) let conn19IdleTimer = makeIdleTimer(timerID: 1, connectionID: 19) stateMachine.timerScheduled(conn19IdleTimer.timer, cancelContinuation: conn19IdleTimer.cancellationToken) stateMachine.connectionEstablished(conn6, maxStreams: 1) let conn4IdleTimer = makeIdleTimer(timerID: 1, connectionID: 4) stateMachine.timerScheduled(conn4IdleTimer.timer, cancelContinuation: conn4IdleTimer.cancellationToken) - let conn19KeepAliveTimer = makeKeepAliveTimer(timerID: 0, connectionID: 19) - stateMachine.timerScheduled(conn19KeepAliveTimer.timer, cancelContinuation: conn19KeepAliveTimer.cancellationToken) - let conn6KeepAliveTimer = makeKeepAliveTimer(timerID: 0, connectionID: 6) - stateMachine.timerScheduled(conn6KeepAliveTimer.timer, cancelContinuation: conn6KeepAliveTimer.cancellationToken) stateMachine.connectionEstablished(conn12, maxStreams: 1) let conn6IdleTimer = makeIdleTimer(timerID: 1, connectionID: 6) stateMachine.timerScheduled(conn6IdleTimer.timer, cancelContinuation: conn6IdleTimer.cancellationToken) stateMachine.connectionEstablished(conn17, maxStreams: 1) stateMachine.connectionEstablished(conn15, maxStreams: 1) - let conn12KeepAliveTimer = makeKeepAliveTimer(timerID: 0, connectionID: 12) - stateMachine.timerScheduled(conn12KeepAliveTimer.timer, cancelContinuation: conn12KeepAliveTimer.cancellationToken) stateMachine.connectionEstablished(conn2, maxStreams: 1) stateMachine.connectionEstablished(conn1, maxStreams: 1) let conn12IdleTimer = makeIdleTimer(timerID: 1, connectionID: 12) stateMachine.timerScheduled(conn12IdleTimer.timer, cancelContinuation: conn12IdleTimer.cancellationToken) - let conn17KeepAliveTimer = makeKeepAliveTimer(timerID: 0, connectionID: 17) - stateMachine.timerScheduled(conn17KeepAliveTimer.timer, cancelContinuation: conn17KeepAliveTimer.cancellationToken) let conn17IdleTimer = makeIdleTimer(timerID: 1, connectionID: 17) stateMachine.timerScheduled(conn17IdleTimer.timer, cancelContinuation: conn17IdleTimer.cancellationToken) - let conn15KeepAliveTimer = makeKeepAliveTimer(timerID: 0, connectionID: 15) - stateMachine.timerScheduled(conn15KeepAliveTimer.timer, cancelContinuation: conn15KeepAliveTimer.cancellationToken) let conn15IdleTimer = makeIdleTimer(timerID: 1, connectionID: 15) stateMachine.timerScheduled(conn15IdleTimer.timer, cancelContinuation: conn15IdleTimer.cancellationToken) - let conn2KeepAliveTimer = makeKeepAliveTimer(timerID: 0, connectionID: 2) - stateMachine.timerScheduled(conn2KeepAliveTimer.timer, cancelContinuation: conn2KeepAliveTimer.cancellationToken) let conn2IdleTimer = makeIdleTimer(timerID: 1, connectionID: 2) stateMachine.timerScheduled(conn2IdleTimer.timer, cancelContinuation: conn2IdleTimer.cancellationToken) - let conn1KeepAliveTimer = makeKeepAliveTimer(timerID: 0, connectionID: 1) - stateMachine.timerScheduled(conn1KeepAliveTimer.timer, cancelContinuation: conn1KeepAliveTimer.cancellationToken) let conn1IdleTimer = makeIdleTimer(timerID: 0, connectionID: 1) stateMachine.timerScheduled(conn1IdleTimer.timer, cancelContinuation: conn1IdleTimer.cancellationToken) - stateMachine.timerTriggered(conn14KeepAliveTimer.timer) - stateMachine.timerTriggered(conn8KeepAliveTimer.timer) - stateMachine.timerTriggered(conn0KeepAliveTimer.timer) - stateMachine.timerTriggered(conn12KeepAliveTimer.timer) - stateMachine.timerTriggered(conn4KeepAliveTimer.timer) - stateMachine.timerTriggered(conn6KeepAliveTimer.timer) - stateMachine.timerTriggered(conn5KeepAliveTimer.timer) - stateMachine.timerTriggered(conn10KeepAliveTimer.timer) - stateMachine.timerTriggered(conn18KeepAliveTimer.timer) - stateMachine.timerTriggered(conn15KeepAliveTimer.timer) - stateMachine.timerTriggered(conn1KeepAliveTimer.timer) - stateMachine.timerTriggered(conn3KeepAliveTimer.timer) - stateMachine.timerTriggered(conn16KeepAliveTimer.timer) - stateMachine.timerTriggered(conn7KeepAliveTimer.timer) - stateMachine.timerTriggered(conn11KeepAliveTimer.timer) - stateMachine.timerTriggered(conn19KeepAliveTimer.timer) - stateMachine.timerTriggered(conn17KeepAliveTimer.timer) - stateMachine.timerTriggered(conn2KeepAliveTimer.timer) - stateMachine.timerTriggered(conn9KeepAliveTimer.timer) - stateMachine.timerTriggered(conn13KeepAliveTimer.timer) - - stateMachine.connectionKeepAliveDone(conn14) - stateMachine.connectionKeepAliveDone(conn8) - stateMachine.connectionKeepAliveDone(conn0) - stateMachine.connectionKeepAliveDone(conn10) let conn14KeepAliveTimer2 = makeKeepAliveTimer(timerID: 2, connectionID: 14) stateMachine.timerScheduled(conn14KeepAliveTimer2.timer, cancelContinuation: conn14KeepAliveTimer2.cancellationToken) - stateMachine.connectionKeepAliveDone(conn12) let conn0KeepAliveTimer2 = makeKeepAliveTimer(timerID: 2, connectionID: 0) stateMachine.timerScheduled(conn0KeepAliveTimer2.timer, cancelContinuation: conn0KeepAliveTimer2.cancellationToken) let conn10KeepAliveTimer2 = makeKeepAliveTimer(timerID: 1, connectionID: 10) stateMachine.timerScheduled(conn10KeepAliveTimer2.timer, cancelContinuation: conn10KeepAliveTimer2.cancellationToken) - stateMachine.connectionKeepAliveDone(conn3) let conn8KeepAliveTimer2 = makeKeepAliveTimer(timerID: 2, connectionID: 8) stateMachine.timerScheduled(conn8KeepAliveTimer2.timer, cancelContinuation: conn8KeepAliveTimer2.cancellationToken) let conn12KeepAliveTimer2 = makeKeepAliveTimer(timerID: 2, connectionID: 12) stateMachine.timerScheduled(conn12KeepAliveTimer2.timer, cancelContinuation: conn12KeepAliveTimer2.cancellationToken) - stateMachine.connectionKeepAliveDone(conn6) let conn3KeepAliveTimer2 = makeKeepAliveTimer(timerID: 2, connectionID: 3) stateMachine.timerScheduled(conn3KeepAliveTimer2.timer, cancelContinuation: conn3KeepAliveTimer2.cancellationToken) - stateMachine.connectionKeepAliveDone(conn15) - stateMachine.connectionKeepAliveDone(conn16) - stateMachine.connectionKeepAliveDone(conn9) - stateMachine.connectionKeepAliveDone(conn18) let conn15KeepAliveTimer2 = makeKeepAliveTimer(timerID: 2, connectionID: 15) stateMachine.timerScheduled(conn15KeepAliveTimer2.timer, cancelContinuation: conn15KeepAliveTimer2.cancellationToken) let conn6KeepAliveTimer2 = makeKeepAliveTimer(timerID: 2, connectionID: 6) @@ -601,25 +509,16 @@ final class PoolStateMachineTests: XCTestCase { stateMachine.timerScheduled(conn16KeepAliveTimer2.timer, cancelContinuation: conn16KeepAliveTimer2.cancellationToken) let conn9KeepAliveTimer2 = makeKeepAliveTimer(timerID: 2, connectionID: 9) stateMachine.timerScheduled(conn9KeepAliveTimer2.timer, cancelContinuation: conn9KeepAliveTimer2.cancellationToken) - stateMachine.connectionKeepAliveDone(conn1) let conn18KeepAliveTimer2 = makeKeepAliveTimer(timerID: 2, connectionID: 18) stateMachine.timerScheduled(conn18KeepAliveTimer2.timer, cancelContinuation: conn18KeepAliveTimer2.cancellationToken) - stateMachine.connectionKeepAliveDone(conn7) - stateMachine.connectionKeepAliveDone(conn13) - stateMachine.connectionKeepAliveDone(conn17) - stateMachine.connectionKeepAliveDone(conn19) let conn1KeepAliveTimer2 = makeKeepAliveTimer(timerID: 2, connectionID: 1) stateMachine.timerScheduled(conn1KeepAliveTimer2.timer, cancelContinuation: conn1KeepAliveTimer2.cancellationToken) - stateMachine.connectionKeepAliveDone(conn2) let conn7KeepAliveTimer2 = makeKeepAliveTimer(timerID: 2, connectionID: 7) stateMachine.timerScheduled(conn7KeepAliveTimer2.timer, cancelContinuation: conn7KeepAliveTimer2.cancellationToken) - stateMachine.connectionKeepAliveDone(conn5) let conn13KeepAliveTimer2 = makeKeepAliveTimer(timerID: 2, connectionID: 13) stateMachine.timerScheduled(conn13KeepAliveTimer2.timer, cancelContinuation: conn13KeepAliveTimer2.cancellationToken) - stateMachine.connectionKeepAliveDone(conn11) let conn17KeepAliveTimer2 = makeKeepAliveTimer(timerID: 2, connectionID: 17) stateMachine.timerScheduled(conn17KeepAliveTimer2.timer, cancelContinuation: conn17KeepAliveTimer2.cancellationToken) - stateMachine.connectionKeepAliveDone(conn4) let conn19KeepAliveTimer2 = makeKeepAliveTimer(timerID: 2, connectionID: 19) stateMachine.timerScheduled(conn19KeepAliveTimer2.timer, cancelContinuation: conn19KeepAliveTimer2.cancellationToken) let conn2KeepAliveTimer2 = makeKeepAliveTimer(timerID: 2, connectionID: 2) From 07a7a93c722a501bd54886c9eef0b63be6f36e59 Mon Sep 17 00:00:00 2001 From: Timo <38291523+lovetodream@users.noreply.github.com> Date: Wed, 8 May 2024 15:13:50 +0200 Subject: [PATCH 4/8] test: decrease connection count --- .../PoolStateMachineTests.swift | 163 +----------------- 1 file changed, 6 insertions(+), 157 deletions(-) diff --git a/Tests/ConnectionPoolModuleTests/PoolStateMachineTests.swift b/Tests/ConnectionPoolModuleTests/PoolStateMachineTests.swift index 65c044a2..9d5876ee 100644 --- a/Tests/ConnectionPoolModuleTests/PoolStateMachineTests.swift +++ b/Tests/ConnectionPoolModuleTests/PoolStateMachineTests.swift @@ -380,8 +380,8 @@ final class PoolStateMachineTests: XCTestCase { func testConnectionsRequestedInBurstsWork() { var configuration = PoolConfiguration() configuration.minimumConnectionCount = 1 - configuration.maximumConnectionSoftLimit = 20 - configuration.maximumConnectionHardLimit = 20 + configuration.maximumConnectionSoftLimit = 2 + configuration.maximumConnectionHardLimit = 2 configuration.keepAliveDuration = .seconds(30) configuration.idleTimeoutDuration = .seconds(60) @@ -402,180 +402,29 @@ final class PoolStateMachineTests: XCTestCase { } // Connections - let conn8 = MockConnection(id: 8) - let conn14 = MockConnection(id: 14) - let conn11 = MockConnection(id: 11) - let conn3 = MockConnection(id: 3) - let conn16 = MockConnection(id: 16) - let conn13 = MockConnection(id: 13) - let conn10 = MockConnection(id: 10) - let conn7 = MockConnection(id: 7) - let conn5 = MockConnection(id: 5) - let conn18 = MockConnection(id: 18) - let conn9 = MockConnection(id: 9) let conn0 = MockConnection(id: 0) - let conn4 = MockConnection(id: 4) - let conn19 = MockConnection(id: 19) - let conn6 = MockConnection(id: 6) - let conn12 = MockConnection(id: 12) - let conn17 = MockConnection(id: 17) - let conn15 = MockConnection(id: 15) - let conn2 = MockConnection(id: 2) let conn1 = MockConnection(id: 1) stateMachine.refillConnections() - for _ in 0..<20 { // request 20 connections + for _ in 0.. Date: Wed, 8 May 2024 15:21:01 +0200 Subject: [PATCH 5/8] test: remove unnecessary config --- Tests/ConnectionPoolModuleTests/PoolStateMachineTests.swift | 2 -- 1 file changed, 2 deletions(-) diff --git a/Tests/ConnectionPoolModuleTests/PoolStateMachineTests.swift b/Tests/ConnectionPoolModuleTests/PoolStateMachineTests.swift index 9d5876ee..27a48173 100644 --- a/Tests/ConnectionPoolModuleTests/PoolStateMachineTests.swift +++ b/Tests/ConnectionPoolModuleTests/PoolStateMachineTests.swift @@ -382,8 +382,6 @@ final class PoolStateMachineTests: XCTestCase { configuration.minimumConnectionCount = 1 configuration.maximumConnectionSoftLimit = 2 configuration.maximumConnectionHardLimit = 2 - configuration.keepAliveDuration = .seconds(30) - configuration.idleTimeoutDuration = .seconds(60) var stateMachine = TestPoolStateMachine( From 4c8360d0588dd134183f6824d40c6d7894a21ced Mon Sep 17 00:00:00 2001 From: Timo <38291523+lovetodream@users.noreply.github.com> Date: Wed, 8 May 2024 17:33:08 +0200 Subject: [PATCH 6/8] test: completely refactor test case --- .../PoolStateMachineTests.swift | 116 ++++++++++++++---- 1 file changed, 95 insertions(+), 21 deletions(-) diff --git a/Tests/ConnectionPoolModuleTests/PoolStateMachineTests.swift b/Tests/ConnectionPoolModuleTests/PoolStateMachineTests.swift index 27a48173..0dddebe6 100644 --- a/Tests/ConnectionPoolModuleTests/PoolStateMachineTests.swift +++ b/Tests/ConnectionPoolModuleTests/PoolStateMachineTests.swift @@ -380,8 +380,10 @@ final class PoolStateMachineTests: XCTestCase { func testConnectionsRequestedInBurstsWork() { var configuration = PoolConfiguration() configuration.minimumConnectionCount = 1 - configuration.maximumConnectionSoftLimit = 2 - configuration.maximumConnectionHardLimit = 2 + configuration.maximumConnectionSoftLimit = 3 + configuration.maximumConnectionHardLimit = 3 + configuration.keepAliveDuration = .seconds(30) + configuration.idleTimeoutDuration = .seconds(60) var stateMachine = TestPoolStateMachine( @@ -400,29 +402,101 @@ final class PoolStateMachineTests: XCTestCase { } // Connections - let conn0 = MockConnection(id: 0) - let conn1 = MockConnection(id: 1) + let connection0 = MockConnection(id: 0) + let connection2 = MockConnection(id: 2) + let connection1 = MockConnection(id: 1) + + let request0 = MockRequest() + let request1 = MockRequest() + let request2 = MockRequest() stateMachine.refillConnections() - for _ in 0.. Date: Wed, 8 May 2024 18:05:18 +0200 Subject: [PATCH 7/8] test: add assertions --- .../PoolStateMachineTests.swift | 23 +++++++++++++++---- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/Tests/ConnectionPoolModuleTests/PoolStateMachineTests.swift b/Tests/ConnectionPoolModuleTests/PoolStateMachineTests.swift index 0dddebe6..14284e54 100644 --- a/Tests/ConnectionPoolModuleTests/PoolStateMachineTests.swift +++ b/Tests/ConnectionPoolModuleTests/PoolStateMachineTests.swift @@ -481,17 +481,30 @@ final class PoolStateMachineTests: XCTestCase { XCTAssertEqual(leaseResult.request, .leaseConnection(.init(element: request3), connection0)) XCTAssertEqual(leaseResult.connection, .none) + // a few more timers are getting triggered - stateMachine.timerTriggered(connection0KeepAliveTimer2) - stateMachine.timerTriggered(connection2KeepAliveTimer2) - stateMachine.timerTriggered(connection2IdleTimer1) + let keepAliveTriggerOnBusyConnectionResult = stateMachine.timerTriggered(connection0KeepAliveTimer2) + XCTAssertEqual(keepAliveTriggerOnBusyConnectionResult.request, .none) + XCTAssertEqual(keepAliveTriggerOnBusyConnectionResult.connection, .none) + + let keepAliveTriggerResult = stateMachine.timerTriggered(connection2KeepAliveTimer2) + XCTAssertEqual(keepAliveTriggerResult.request, .none) + XCTAssertEqual(keepAliveTriggerResult.connection, .runKeepAlive(connection2, connection2KeepAliveTimer2CancellationToken)) + + let idleResult = stateMachine.timerTriggered(connection2IdleTimer1) + XCTAssertEqual(idleResult.request, .none) + XCTAssertEqual(idleResult.connection, .closeConnection(connection2, [connection2IdleTimer1CancellationToken])) + let request4 = MockRequest() // we need another connection, this will cause a crash - stateMachine.leaseConnection(request4) // it adds a request to the queue, as no connections are available + let leaseUnavailableResult = stateMachine.leaseConnection(request4) // it adds a request to the queue, as no connections are available + XCTAssertEqual(leaseUnavailableResult.request, .none) + XCTAssertEqual(leaseUnavailableResult.connection, .makeConnection(.init(connectionID: 3), [])) + stateMachine.timerTriggered(connection0IdleTimer1) // here the crash happens, either in idle or keep alive timer // The reason for the crash might be that all connections are currently unavailable: - // 0: keep alive in progress + // 0: currently leased // 1: marked as going away // 2: marked as going away From 08710026dcd7468d6c9f6f5fe3cb4754405bb5e8 Mon Sep 17 00:00:00 2001 From: Timo <38291523+lovetodream@users.noreply.github.com> Date: Thu, 9 May 2024 11:55:24 +0200 Subject: [PATCH 8/8] test: add more assertions and describe what is going on --- .../PoolStateMachineTests.swift | 133 ++++++++++++------ 1 file changed, 87 insertions(+), 46 deletions(-) diff --git a/Tests/ConnectionPoolModuleTests/PoolStateMachineTests.swift b/Tests/ConnectionPoolModuleTests/PoolStateMachineTests.swift index 14284e54..4dce6248 100644 --- a/Tests/ConnectionPoolModuleTests/PoolStateMachineTests.swift +++ b/Tests/ConnectionPoolModuleTests/PoolStateMachineTests.swift @@ -379,7 +379,7 @@ final class PoolStateMachineTests: XCTestCase { func testConnectionsRequestedInBurstsWork() { var configuration = PoolConfiguration() - configuration.minimumConnectionCount = 1 + configuration.minimumConnectionCount = 0 configuration.maximumConnectionSoftLimit = 3 configuration.maximumConnectionHardLimit = 3 configuration.keepAliveDuration = .seconds(30) @@ -406,84 +406,125 @@ final class PoolStateMachineTests: XCTestCase { let connection2 = MockConnection(id: 2) let connection1 = MockConnection(id: 1) + // Requests let request0 = MockRequest() let request1 = MockRequest() let request2 = MockRequest() - stateMachine.refillConnections() + // Initial Timers + let connection1KeepAliveTimer0 = TestPoolStateMachine.Timer(.init(timerID: 0, connectionID: 1, usecase: .keepAlive), duration: .seconds(30)) + let connection1KeepAliveTimer0CancellationToken = MockTimerCancellationToken(connection1KeepAliveTimer0) + let connection1IdleTimer1 = TestPoolStateMachine.Timer(.init(timerID: 1, connectionID: 1, usecase: .idleTimeout), duration: .seconds(60)) + let connection1IdleTimer1CancellationToken = MockTimerCancellationToken(connection1IdleTimer1) + let connection2KeepAliveTimer0 = TestPoolStateMachine.Timer(.init(timerID: 0, connectionID: 2, usecase: .keepAlive), duration: .seconds(30)) + let connection2KeepAliveTimer0CancellationToken = MockTimerCancellationToken(connection2KeepAliveTimer0) + let connection2IdleTimer1 = TestPoolStateMachine.Timer(.init(timerID: 1, connectionID: 2, usecase: .idleTimeout), duration: .seconds(60)) + let connection2IdleTimer1CancellationToken = MockTimerCancellationToken(connection2IdleTimer1) + let connection0KeepAliveTimer0 = TestPoolStateMachine.Timer(.init(timerID: 0, connectionID: 0, usecase: .keepAlive), duration: .seconds(30)) + let connection0KeepAliveTimer0CancellationToken = MockTimerCancellationToken(connection0KeepAliveTimer0) + let connection0IdleTimer1 = TestPoolStateMachine.Timer(.init(timerID: 1, connectionID: 0, usecase: .idleTimeout), duration: .seconds(60)) + let connection0IdleTimer1CancellationToken = MockTimerCancellationToken(connection0IdleTimer1) - stateMachine.leaseConnection(request1) - stateMachine.leaseConnection(request0) - stateMachine.leaseConnection(request2) + let requests = stateMachine.refillConnections() + XCTAssertEqual(requests.count, 0) - stateMachine.connectionEstablished(connection2, maxStreams: 1) - stateMachine.connectionEstablished(connection1, maxStreams: 1) - // do work... + // one connection exists + let lease1Result = stateMachine.leaseConnection(request1) + XCTAssertEqual(lease1Result.request, .none) + XCTAssertEqual(lease1Result.connection, .makeConnection(.init(connectionID: 0), [])) + + let lease2Result = stateMachine.leaseConnection(request0) + XCTAssertEqual(lease2Result.request, .none) + XCTAssertEqual(lease2Result.connection, .makeConnection(.init(connectionID: 1), [])) // second request + let lease3Result = stateMachine.leaseConnection(request2) + XCTAssertEqual(lease3Result.request, .none) + XCTAssertEqual(lease3Result.connection, .makeConnection(.init(connectionID: 2), [])) // third request + + // fulfil requests + let connectionEstablished1 = stateMachine.connectionEstablished(connection2, maxStreams: 1) + XCTAssertEqual(connectionEstablished1.request, .leaseConnection([request1], connection2)) + XCTAssertEqual(connectionEstablished1.connection, .none) + let connectionEstablished2 = stateMachine.connectionEstablished(connection1, maxStreams: 1) + XCTAssertEqual(connectionEstablished2.request, .leaseConnection([request0], connection1)) + XCTAssertEqual(connectionEstablished2.connection, .none) + // do work and release connections... // Optional("hello") // Optional("hello") - stateMachine.releaseConnection(connection2, streams: 1) - stateMachine.releaseConnection(connection1, streams: 1) - - let connection1KeepAliveTimer0 = TestPoolStateMachine.Timer(.init(timerID: 0, connectionID: 1, usecase: .keepAlive), duration: .seconds(30)) - let connection1KeepAliveTimer0CancellationToken = MockTimerCancellationToken(connection1KeepAliveTimer0) + let releaseResult1 = stateMachine.releaseConnection(connection2, streams: 1) + XCTAssertEqual(releaseResult1.request, .leaseConnection([request2], connection2)) // we still have a pending request + XCTAssertEqual(releaseResult1.connection, .none) + let releaseResult2 = stateMachine.releaseConnection(connection1, streams: 1) + XCTAssertEqual(releaseResult2.request, .none) // no more requests + XCTAssertEqual(releaseResult2.connection, .scheduleTimers([connection1KeepAliveTimer0, connection1IdleTimer1])) + + // schedule timers as requested _ = stateMachine.timerScheduled(connection1KeepAliveTimer0, cancelContinuation: connection1KeepAliveTimer0CancellationToken) - let connection1IdleTimer1 = TestPoolStateMachine.Timer(.init(timerID: 1, connectionID: 1, usecase: .idleTimeout), duration: .seconds(60)) - let connection1IdleTimer1CancellationToken = MockTimerCancellationToken(connection1IdleTimer1) _ = stateMachine.timerScheduled(connection1IdleTimer1, cancelContinuation: connection1IdleTimer1CancellationToken) // do more work... // Optional("hello") - stateMachine.releaseConnection(connection2, streams: 1) - - let connection2KeepAliveTimer0 = TestPoolStateMachine.Timer(.init(timerID: 0, connectionID: 2, usecase: .keepAlive), duration: .seconds(30)) - let connection2KeepAliveTimer0CancellationToken = MockTimerCancellationToken(connection2KeepAliveTimer0) + let releaseResult3 = stateMachine.releaseConnection(connection2, streams: 1) + XCTAssertEqual(releaseResult3.request, .none) + XCTAssertEqual(releaseResult3.connection, .scheduleTimers([connection2KeepAliveTimer0, connection2IdleTimer1])) + + // schedule timers as requested _ = stateMachine.timerScheduled(connection2KeepAliveTimer0, cancelContinuation: connection2KeepAliveTimer0CancellationToken) - let connection2IdleTimer1 = TestPoolStateMachine.Timer(.init(timerID: 1, connectionID: 2, usecase: .idleTimeout), duration: .seconds(60)) - let connection2IdleTimer1CancellationToken = MockTimerCancellationToken(connection2IdleTimer1) _ = stateMachine.timerScheduled(connection2IdleTimer1, cancelContinuation: connection2IdleTimer1CancellationToken) - stateMachine.connectionEstablished(connection0, maxStreams: 1) - - let connection0IdleTimer1 = TestPoolStateMachine.Timer(.init(timerID: 1, connectionID: 0, usecase: .idleTimeout), duration: .seconds(60)) - let connection0IdleTimer1CancellationToken = MockTimerCancellationToken(connection0IdleTimer1) - _ = stateMachine.timerScheduled(connection0IdleTimer1, cancelContinuation: connection0IdleTimer1CancellationToken) - let connection0KeepAliveTimer0 = TestPoolStateMachine.Timer(.init(timerID: 0, connectionID: 0, usecase: .keepAlive), duration: .seconds(30)) - let connection0KeepAliveTimer0CancellationToken = MockTimerCancellationToken(connection0KeepAliveTimer0) + let connection0Established = stateMachine.connectionEstablished(connection0, maxStreams: 1) + XCTAssertEqual(connection0Established.request, .none) // it's not needed anymore, as we requested 3 connections, and all requests have been fulfilled already + XCTAssertEqual(connection0Established.connection, .scheduleTimers([connection0KeepAliveTimer0, connection0IdleTimer1])) + +// _ = stateMachine.timerScheduled(connection0IdleTimer1, cancelContinuation: connection0IdleTimer1CancellationToken) _ = stateMachine.timerScheduled(connection0KeepAliveTimer0, cancelContinuation: connection0KeepAliveTimer0CancellationToken) // keep alive timers are triggered after 30s - stateMachine.timerTriggered(connection2KeepAliveTimer0) - stateMachine.timerTriggered(connection1KeepAliveTimer0) - stateMachine.timerTriggered(connection0KeepAliveTimer0) - + for (connection, timer, cancellationToken) in [ + (connection0, connection0KeepAliveTimer0, connection0KeepAliveTimer0CancellationToken), + (connection1, connection1KeepAliveTimer0, connection1KeepAliveTimer0CancellationToken), + (connection2, connection2KeepAliveTimer0, connection2KeepAliveTimer0CancellationToken) + ] { + let keepAliveResult = stateMachine.timerTriggered(timer) + XCTAssertEqual(keepAliveResult.request, .none) + XCTAssertEqual(keepAliveResult.connection, .runKeepAlive(connection, cancellationToken)) + } + // keep alive requests are done and new timers are scheduled - stateMachine.connectionKeepAliveDone(connection1) - let connection1KeepAliveTimer2 = TestPoolStateMachine.Timer(.init(timerID: 2, connectionID: 1, usecase: .keepAlive), duration: .seconds(30)) - let connection1KeepAliveTimer2CancellationToken = MockTimerCancellationToken(connection1KeepAliveTimer2) - _ = stateMachine.timerScheduled(connection1KeepAliveTimer2, cancelContinuation: connection1KeepAliveTimer2CancellationToken) - stateMachine.connectionKeepAliveDone(connection0) let connection0KeepAliveTimer2 = TestPoolStateMachine.Timer(.init(timerID: 2, connectionID: 0, usecase: .keepAlive), duration: .seconds(30)) let connection0KeepAliveTimer2CancellationToken = MockTimerCancellationToken(connection0KeepAliveTimer2) - _ = stateMachine.timerScheduled(connection0KeepAliveTimer2, cancelContinuation: connection0KeepAliveTimer2CancellationToken) - stateMachine.connectionKeepAliveDone(connection2) + let connection1KeepAliveTimer2 = TestPoolStateMachine.Timer(.init(timerID: 2, connectionID: 1, usecase: .keepAlive), duration: .seconds(30)) + let connection1KeepAliveTimer2CancellationToken = MockTimerCancellationToken(connection1KeepAliveTimer2) let connection2KeepAliveTimer2 = TestPoolStateMachine.Timer(.init(timerID: 2, connectionID: 2, usecase: .keepAlive), duration: .seconds(30)) let connection2KeepAliveTimer2CancellationToken = MockTimerCancellationToken(connection2KeepAliveTimer2) - _ = stateMachine.timerScheduled(connection2KeepAliveTimer2, cancelContinuation: connection2KeepAliveTimer2CancellationToken) + for (connection, newTimer, newTimerCancellationToken) in [ + (connection0, connection0KeepAliveTimer2, connection0KeepAliveTimer2CancellationToken), + (connection1, connection1KeepAliveTimer2, connection1KeepAliveTimer2CancellationToken), + (connection2, connection2KeepAliveTimer2, connection2KeepAliveTimer2CancellationToken) + ] { + let keepAliveResult = stateMachine.connectionKeepAliveDone(connection) + XCTAssertEqual(keepAliveResult.request, .none) + XCTAssertEqual(keepAliveResult.connection, .scheduleTimers([newTimer])) + _ = stateMachine.timerScheduled(newTimer, cancelContinuation: newTimerCancellationToken) + } // now connections might go idle or trigger another keep alive - stateMachine.timerTriggered(connection1IdleTimer1) + let connection1IdleResult = stateMachine.timerTriggered(connection1IdleTimer1) + XCTAssertEqual(connection1IdleResult.request, .none) + XCTAssertEqual(connection1IdleResult.connection, .closeConnection(connection1, [connection1KeepAliveTimer2CancellationToken, connection1IdleTimer1CancellationToken])) // Burst done: 1/50 - stateMachine.timerTriggered(connection1KeepAliveTimer2) - + let keepAliveTriggerAfterGoneResult = stateMachine.timerTriggered(connection1KeepAliveTimer2) + XCTAssertEqual(keepAliveTriggerAfterGoneResult, .none()) + // we want to start new work on all connections, but a few are occupied or closed already let request3 = MockRequest() let leaseResult = stateMachine.leaseConnection(request3) // this one works, connections are available XCTAssertEqual(leaseResult.request, .leaseConnection(.init(element: request3), connection0)) - XCTAssertEqual(leaseResult.connection, .none) + XCTAssertEqual(leaseResult.connection, .cancelTimers([connection0KeepAliveTimer2CancellationToken])) // a few more timers are getting triggered - + + // we wanted to cancel the timer, but it's triggered before we could cancel it let keepAliveTriggerOnBusyConnectionResult = stateMachine.timerTriggered(connection0KeepAliveTimer2) XCTAssertEqual(keepAliveTriggerOnBusyConnectionResult.request, .none) XCTAssertEqual(keepAliveTriggerOnBusyConnectionResult.connection, .none) @@ -491,7 +532,7 @@ final class PoolStateMachineTests: XCTestCase { let keepAliveTriggerResult = stateMachine.timerTriggered(connection2KeepAliveTimer2) XCTAssertEqual(keepAliveTriggerResult.request, .none) XCTAssertEqual(keepAliveTriggerResult.connection, .runKeepAlive(connection2, connection2KeepAliveTimer2CancellationToken)) - + let idleResult = stateMachine.timerTriggered(connection2IdleTimer1) XCTAssertEqual(idleResult.request, .none) XCTAssertEqual(idleResult.connection, .closeConnection(connection2, [connection2IdleTimer1CancellationToken]))