-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1466 from planetary-social/fix-socket-connection-bug
Fix socket connection bug
- Loading branch information
Showing
4 changed files
with
79 additions
and
2 deletions.
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
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
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
72 changes: 72 additions & 0 deletions
72
NosTests/Service/Relay/RelaySubscriptionManagerTests.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,72 @@ | ||
import XCTest | ||
import Starscream | ||
|
||
final class RelaySubscriptionManagerTests: XCTestCase { | ||
|
||
let relayOneURL = URL(string: "wss://relay.one")! | ||
let relayTwoURL = URL(string: "wss://relay.two")! | ||
let relayThreeURL = URL(string: "wss://relay.three")! | ||
|
||
// swiftlint:disable:next implicitly_unwrapped_optional | ||
var subject: RelaySubscriptionManagerActor! | ||
|
||
override func setUp() async throws { | ||
try await super.setUp() | ||
subject = RelaySubscriptionManagerActor() | ||
} | ||
|
||
func test_openSockets_givenDisconnectedSockets_startsConnectingAll() async throws { | ||
// Arrange | ||
_ = await subject.queueSubscription(with: Filter(), to: relayOneURL) | ||
_ = await subject.queueSubscription(with: Filter(), to: relayTwoURL) | ||
_ = await subject.queueSubscription(with: Filter(), to: relayThreeURL) | ||
|
||
// Act | ||
await subject.openSockets() | ||
|
||
// Assert | ||
var connections = await subject.socketConnections | ||
XCTAssertEqual(connections.count, 3) | ||
connections.values.forEach { XCTAssertEqual($0.state, .connecting) } | ||
} | ||
|
||
func test_openSockets_givenErroredSocket_startsConnectingAll() async throws { | ||
// Arrange | ||
_ = await subject.queueSubscription(with: Filter(), to: relayOneURL) | ||
_ = await subject.queueSubscription(with: Filter(), to: relayTwoURL) | ||
_ = await subject.queueSubscription(with: Filter(), to: relayThreeURL) | ||
await subject.openSockets() | ||
var request = URLRequest(url: relayTwoURL) | ||
request.timeoutInterval = 10 | ||
let socket = WebSocket(request: request, useCustomEngine: false) | ||
|
||
// Act | ||
await subject.trackError(socket: socket) | ||
try await Task.sleep(for: .seconds(1)) | ||
await subject.openSockets() | ||
|
||
// Assert | ||
var connections = await subject.socketConnections | ||
XCTAssertEqual(connections.count, 3) | ||
connections.values.forEach { XCTAssertEqual($0.state, .connecting) } | ||
} | ||
|
||
func test_openSockets_givenOneDisconnectedSocket_startsConnectingAll() async throws { | ||
// Arrange | ||
_ = await subject.queueSubscription(with: Filter(), to: relayOneURL) | ||
_ = await subject.queueSubscription(with: Filter(), to: relayTwoURL) | ||
_ = await subject.queueSubscription(with: Filter(), to: relayThreeURL) | ||
await subject.openSockets() | ||
|
||
var connections = await subject.socketConnections | ||
let connection = connections[relayThreeURL] | ||
connection?.state = .disconnected | ||
|
||
// Act | ||
await subject.openSockets() | ||
|
||
// Assert | ||
XCTAssertEqual(connections.count, 3) | ||
connections.values.forEach { XCTAssertEqual($0.state, .connecting) } | ||
} | ||
} |