Skip to content

Commit

Permalink
update test
Browse files Browse the repository at this point in the history
  • Loading branch information
MacOMNI committed Nov 3, 2024
1 parent 9698005 commit ab6497f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 32 deletions.
8 changes: 3 additions & 5 deletions Networking/Sources/Networking/Connection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,10 @@ public final class Connection<Handler: StreamHandler>: Sendable, ConnectionInfoP

public var needReconnect: Bool {
state.read {
switch $0 {
case .reconnect:
true
default:
false
if case .reconnect = $0 {
return true
}
return false
}
}

Expand Down
44 changes: 20 additions & 24 deletions Networking/Sources/Networking/Peer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,6 @@ public final class Peer<Handler: StreamHandler>: Sendable {
let connections = impl.connections.read { connections in
connections.byId.values
}

guard let messageData = try? message.encode() else {
impl.logger.warning("Failed to encode message: \(message)")
return
Expand Down Expand Up @@ -274,30 +273,27 @@ final class PeerImpl<Handler: StreamHandler>: Sendable {

// TODO: Add reconnection attempts & Apply exponential backoff delay
func reconnect(to address: NetAddr, role: PeerRole) throws {
Task {
try await Task.sleep(for: .microseconds(2000))
logger.debug("reconnecting", metadata: ["to address": "\(address)", "role": "\(role)"])
try connections.write { connections in
if connections.byAddr[address] != nil {
logger.warning("reconnecting to \(address) already connected")
return
}
let quicConn = try QuicConnection(
handler: PeerEventHandler(self),
registration: clientConfiguration.registration,
configuration: clientConfiguration
)
try quicConn.connect(to: address)
let conn = Connection(
quicConn,
impl: self,
role: role,
remoteAddress: address,
initiatedByLocal: true
)
connections.byAddr[address] = conn
connections.byId[conn.id] = conn
logger.debug("reconnecting", metadata: ["to address": "\(address)", "role": "\(role)"])
try connections.write { connections in
if connections.byAddr[address] != nil {
logger.warning("reconnecting to \(address) already connected")
return
}
let quicConn = try QuicConnection(
handler: PeerEventHandler(self),
registration: clientConfiguration.registration,
configuration: clientConfiguration
)
try quicConn.connect(to: address)
let conn = Connection(
quicConn,
impl: self,
role: role,
remoteAddress: address,
initiatedByLocal: true
)
connections.byAddr[address] = conn
connections.byId[conn.id] = conn
}
}

Expand Down
6 changes: 3 additions & 3 deletions Networking/Tests/NetworkingTests/PeerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ struct PeerTests {
struct MockEphemeralStreamHandler: EphemeralStreamHandler {
typealias StreamKind = EphemeralStreamKind
typealias Request = MockRequest<EphemeralStreamKind>
private let dataStorage = DataStorage()
private let dataStorage: PeerTests.DataStorage = DataStorage()

var lastReceivedData: Data? {
get async { await dataStorage.data.last }
Expand Down Expand Up @@ -302,13 +302,13 @@ struct PeerTests {
peer1.broadcast(
kind: .uniqueC, message: .init(kind: .uniqueC, data: messageData)
)
try? await Task.sleep(for: .milliseconds(5000))
try? await Task.sleep(for: .milliseconds(1000))
let lastReceivedData = await handler2.lastReceivedData
#expect(lastReceivedData == messageData)
}

@Test
func ConnectionNoNeedToReconnect() async throws {
func connectionNoNeedToReconnect() async throws {
let handler2 = MockPresentStreamHandler()
let messageData = Data("Post-recovery message".utf8)

Expand Down

0 comments on commit ab6497f

Please sign in to comment.