-
-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8fbf8ff
commit 358fa59
Showing
6 changed files
with
129 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import Atomics | ||
|
||
public struct ConnectionIDGenerator: ConnectionIDGeneratorProtocol { | ||
static let globalGenerator = ConnectionIDGenerator() | ||
|
||
private let atomic: ManagedAtomic<Int> | ||
|
||
public init() { | ||
self.atomic = .init(0) | ||
} | ||
|
||
public func next() -> Int { | ||
return self.atomic.loadThenWrappingIncrement(ordering: .relaxed) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
@available(macOS 13.0, iOS 16.0, tvOS 16.0, watchOS 9.0, *) | ||
public struct NoOpKeepAliveBehavior<Connection: PooledConnection>: ConnectionKeepAliveBehavior { | ||
public var keepAliveFrequency: Duration? { nil } | ||
|
||
public func runKeepAlive(for connection: Connection) async throws {} | ||
|
||
public init(connectionType: Connection.Type) {} | ||
} |
22 changes: 22 additions & 0 deletions
22
Tests/ConnectionPoolModuleTests/ConnectionIDGeneratorTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import _ConnectionPoolModule | ||
import XCTest | ||
|
||
final class ConnectionIDGeneratorTests: XCTestCase { | ||
func testGenerateConnectionIDs() async { | ||
let idGenerator = ConnectionIDGenerator() | ||
|
||
XCTAssertEqual(idGenerator.next(), 0) | ||
XCTAssertEqual(idGenerator.next(), 1) | ||
XCTAssertEqual(idGenerator.next(), 2) | ||
|
||
await withTaskGroup(of: Void.self) { taskGroup in | ||
for _ in 0..<1000 { | ||
taskGroup.addTask { | ||
_ = idGenerator.next() | ||
} | ||
} | ||
} | ||
|
||
XCTAssertEqual(idGenerator.next(), 1003) | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
Tests/ConnectionPoolModuleTests/Mocks/MockConnection.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import DequeModule | ||
@testable import _ConnectionPoolModule | ||
|
||
// Sendability enforced through the lock | ||
final class MockConnection: PooledConnection, @unchecked Sendable { | ||
typealias ID = Int | ||
|
||
let id: ID | ||
|
||
private enum State { | ||
case running([@Sendable ((any Error)?) -> ()]) | ||
case closing([@Sendable ((any Error)?) -> ()]) | ||
case closed | ||
} | ||
|
||
private let lock = NIOLock() | ||
private var _state = State.running([]) | ||
|
||
init(id: Int) { | ||
self.id = id | ||
} | ||
|
||
func onClose(_ closure: @escaping @Sendable ((any Error)?) -> ()) { | ||
let enqueued = self.lock.withLock { () -> Bool in | ||
switch self._state { | ||
case .closed: | ||
return false | ||
|
||
case .running(var callbacks): | ||
callbacks.append(closure) | ||
self._state = .running(callbacks) | ||
return true | ||
|
||
case .closing(var callbacks): | ||
callbacks.append(closure) | ||
self._state = .closing(callbacks) | ||
return true | ||
} | ||
} | ||
|
||
if !enqueued { | ||
closure(nil) | ||
} | ||
} | ||
|
||
func close() { | ||
self.lock.withLock { | ||
switch self._state { | ||
case .running(let callbacks): | ||
self._state = .closing(callbacks) | ||
|
||
case .closing, .closed: | ||
break | ||
} | ||
} | ||
} | ||
|
||
func closeIfClosing() { | ||
let callbacks = self.lock.withLock { () -> [@Sendable ((any Error)?) -> ()] in | ||
switch self._state { | ||
case .running, .closed: | ||
return [] | ||
|
||
case .closing(let callbacks): | ||
self._state = .closed | ||
return callbacks | ||
} | ||
} | ||
|
||
for callback in callbacks { | ||
callback(nil) | ||
} | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
Tests/ConnectionPoolModuleTests/NoKeepAliveBehaviorTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import _ConnectionPoolModule | ||
import XCTest | ||
|
||
@available(macOS 13.0, iOS 16.0, tvOS 16.0, watchOS 9.0, *) | ||
final class NoKeepAliveBehaviorTests: XCTestCase { | ||
func testNoKeepAlive() { | ||
let keepAliveBehavior = NoOpKeepAliveBehavior(connectionType: MockConnection.self) | ||
XCTAssertNil(keepAliveBehavior.keepAliveFrequency) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.