Skip to content

Commit

Permalink
Merge pull request #1466 from planetary-social/fix-socket-connection-bug
Browse files Browse the repository at this point in the history
Fix socket connection bug
  • Loading branch information
joshuatbrown authored Sep 6, 2024
2 parents 855c000 + 7864627 commit 720c5a7
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]

### Release Notes
- Fixed a bug where Nos sometimes wouldn't reconnect to relays.
- Added nos.lol to the default relay list for new accounts and removed relay.snort.social.
- Show quoted notes in note cards.
- Added quote-reposting.
Expand Down
4 changes: 4 additions & 0 deletions Nos.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@
C913DA0E2AEB3265003BDD6D /* WarningView.swift in Sources */ = {isa = PBXBuildFile; fileRef = C913DA0D2AEB3265003BDD6D /* WarningView.swift */; };
C91400252B2A3ABF009B13B4 /* SQLiteStoreTestCase.swift in Sources */ = {isa = PBXBuildFile; fileRef = C91400232B2A3894009B13B4 /* SQLiteStoreTestCase.swift */; };
C91565C12B2368FA0068EECA /* ViewInspector in Frameworks */ = {isa = PBXBuildFile; productRef = C91565C02B2368FA0068EECA /* ViewInspector */; };
C9246C1C2C8A42A0005495CE /* RelaySubscriptionManagerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = C9246C1B2C8A42A0005495CE /* RelaySubscriptionManagerTests.swift */; };
C92DF80529C25DE900400561 /* URL+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = C92DF80429C25DE900400561 /* URL+Extensions.swift */; };
C92DF80629C25DE900400561 /* URL+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = C92DF80429C25DE900400561 /* URL+Extensions.swift */; };
C92DF80829C25FA900400561 /* SquareImage.swift in Sources */ = {isa = PBXBuildFile; fileRef = C92DF80729C25FA900400561 /* SquareImage.swift */; };
Expand Down Expand Up @@ -701,6 +702,7 @@
C913DA0B2AEB2EBF003BDD6D /* FetchRequestPublisher.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FetchRequestPublisher.swift; sourceTree = "<group>"; };
C913DA0D2AEB3265003BDD6D /* WarningView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WarningView.swift; sourceTree = "<group>"; };
C91400232B2A3894009B13B4 /* SQLiteStoreTestCase.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SQLiteStoreTestCase.swift; sourceTree = "<group>"; };
C9246C1B2C8A42A0005495CE /* RelaySubscriptionManagerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RelaySubscriptionManagerTests.swift; sourceTree = "<group>"; };
C92A04DD2A58B02B00C844B8 /* Nos 11.xcdatamodel */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcdatamodel; path = "Nos 11.xcdatamodel"; sourceTree = "<group>"; };
C92AB3352B599DD0005B3FFB /* doc */ = {isa = PBXFileReference; lastKnownFileType = folder; path = doc; sourceTree = "<group>"; };
C92DF80429C25DE900400561 /* URL+Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "URL+Extensions.swift"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -985,6 +987,7 @@
isa = PBXGroup;
children = (
0320C0FA2BFE43A600C4C080 /* RelayServiceTests.swift */,
C9246C1B2C8A42A0005495CE /* RelaySubscriptionManagerTests.swift */,
);
path = Relay;
sourceTree = "<group>";
Expand Down Expand Up @@ -2479,6 +2482,7 @@
A3B943D8299D758F00A15A08 /* Keychain.swift in Sources */,
035729B92BE416A6005FEE85 /* GiftWrapperTests.swift in Sources */,
50E2EB7B2C8617C800D4B360 /* NSRegularExpression+Replacement.swift in Sources */,
C9246C1C2C8A42A0005495CE /* RelaySubscriptionManagerTests.swift in Sources */,
032634702C10C40B00E489B5 /* NostrBuildAPIClientTests.swift in Sources */,
0315B5F02C7E451C0020E707 /* MockMediaService.swift in Sources */,
C9646EAA29B7A506007239A4 /* Analytics.swift in Sources */,
Expand Down
4 changes: 2 additions & 2 deletions Nos/Service/Relay/RelaySubscriptionManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -131,15 +131,15 @@ actor RelaySubscriptionManagerActor: RelaySubscriptionManager {
switch connection.state {
case .errored(let error):
if error.nextRetry > Date.now {
return
continue
} else {
fallthrough
}
case .disconnected:
connection.socket.connect()
connection.state = .connecting
case .connected, .connecting, .authenticating:
return
continue
}
}
}
Expand Down
72 changes: 72 additions & 0 deletions NosTests/Service/Relay/RelaySubscriptionManagerTests.swift
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) }
}
}

0 comments on commit 720c5a7

Please sign in to comment.