Skip to content

Commit

Permalink
update peer test
Browse files Browse the repository at this point in the history
  • Loading branch information
MacOMNI committed Oct 24, 2024
1 parent 1d0befa commit b533b3a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
14 changes: 13 additions & 1 deletion Networking/Sources/Networking/Connection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ public protocol ConnectionInfoProtocol {
var remoteAddress: NetAddr { get }
}

enum ConnectionError: Error {
case receiveFailed
}

public final class Connection<Handler: StreamHandler>: Sendable, ConnectionInfoProtocol {
let connection: QuicConnection
let impl: PeerImpl<Handler>
Expand Down Expand Up @@ -49,7 +53,15 @@ public final class Connection<Handler: StreamHandler>: Sendable, ConnectionInfoP
response.append(nextData)
break
}
return response
guard response.count >= 4 else {
stream.close(abort: true)
throw ConnectionError.receiveFailed
}
let lengthData = response.prefix(4)
let length = UInt32(
littleEndian: lengthData.withUnsafeBytes { $0.loadUnaligned(as: UInt32.self) }
)
return response.dropFirst(4).prefix(Int(length))
}

@discardableResult
Expand Down
4 changes: 2 additions & 2 deletions Networking/Tests/NetworkingTests/PeerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ struct PeerTests {
let data1 = try await connection1.request(
MockRequest(kind: .typeA, data: Data("hello world".utf8))
)
#expect(data1.suffix(from: 4) == Data("hello world".utf8))
#expect(data1 == Data("hello world".utf8))

let connection2 = try peer2.connect(
to: NetAddr(ipAddress: "127.0.0.1", port: 8083)!, role: .validator
Expand All @@ -221,6 +221,6 @@ struct PeerTests {
let data2 = try await connection2.request(
MockRequest(kind: .typeB, data: Data("I am jam".utf8))
)
#expect(data2.suffix(from: 4) == Data("I am jam".utf8))
#expect(data2 == Data("I am jam".utf8))
}
}

0 comments on commit b533b3a

Please sign in to comment.