Skip to content

Commit

Permalink
update TelemetryHandlers tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MacOMNI committed Jan 27, 2025
1 parent 5e80515 commit 486706a
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 2 deletions.
4 changes: 4 additions & 0 deletions Node/Sources/Node/NetworkingProtocol/Network.swift
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ public final class Network: Sendable {
public var peersCount: Int {
peer.peersCount
}

public var networkKey: String {
peer.publicKey.description
}

Check warning on line 98 in Node/Sources/Node/NetworkingProtocol/Network.swift

View check run for this annotation

Codecov / codecov/patch

Node/Sources/Node/NetworkingProtocol/Network.swift#L96-L98

Added lines #L96 - L98 were not covered by tests
}

struct HandlerDef: StreamHandler {
Expand Down
4 changes: 4 additions & 0 deletions Node/Sources/Node/NodeDataSource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,8 @@ extension NodeDataSource: TelemetryDataSource {
public func getPeersCount() async throws -> Int {
networkManager.peersCount
}

public func getNetworkKey() async throws -> String {
networkManager.network.networkKey
}

Check warning on line 93 in Node/Sources/Node/NodeDataSource.swift

View check run for this annotation

Codecov / codecov/patch

Node/Sources/Node/NodeDataSource.swift#L91-L93

Added lines #L91 - L93 were not covered by tests
}
1 change: 1 addition & 0 deletions RPC/Sources/RPC/DataSource/DataSource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public protocol ChainDataSource: Sendable {
public protocol TelemetryDataSource: Sendable {
func name() async throws -> String
func getPeersCount() async throws -> Int
func getNetworkKey() async throws -> String
}

public typealias DataSource = ChainDataSource & SystemDataSource & TelemetryDataSource
3 changes: 1 addition & 2 deletions RPC/Sources/RPC/Handlers/TelemetryHandlers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@ public enum TelemetryHandlers {
}

public func handle(request _: Request) async throws -> Response? {
// TODO: implement
nil
try await source.getNetworkKey()
}
}
}
72 changes: 72 additions & 0 deletions RPC/Tests/RPCTests/TelemetryHandlersTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import Blockchain
import Testing
import TracingUtils
import Vapor
import XCTVapor

@testable import RPC
@testable import Utils

struct TelemetryDummySource: TelemetryDataSource {
func name() async throws -> String {
"TestNode"
}

func getPeersCount() async throws -> Int {
42
}

func getNetworkKey() async throws -> String {
"Ed25519:TestKey"
}
}

final class TelemetryHandlersTests {
var app: Application

init() throws {
app = Application(.testing)

let rpcController = JSONRPCController(
handlers: TelemetryHandlers.getHandlers(source: TelemetryDummySource())
)
try app.register(collection: rpcController)
}

deinit {
app.shutdown()
}

@Test func name() throws {
let req = JSONRequest(jsonrpc: "2.0", method: "telemetry_name", params: nil, id: 1)
var buffer = ByteBuffer()
try buffer.writeJSONEncodable(req)
try app.test(.POST, "/", headers: ["Content-Type": "application/json"], body: buffer) { res in
#expect(res.status == .ok)
let resp = try res.content.decode(JSONResponse.self, using: JSONDecoder())
#expect((resp.result!.value as! Utils.JSON).string == "TestNode")
}
}

@Test func peersCount() throws {
let req = JSONRequest(jsonrpc: "2.0", method: "telemetry_peersCount", params: nil, id: 2)
var buffer = ByteBuffer()
try buffer.writeJSONEncodable(req)
try app.test(.POST, "/", headers: ["Content-Type": "application/json"], body: buffer) { res in
#expect(res.status == .ok)
let resp = try res.content.decode(JSONResponse.self, using: JSONDecoder())
#expect((resp.result!.value as! Utils.JSON).number == 42)
}
}

@Test func networkKey() throws {
let req = JSONRequest(jsonrpc: "2.0", method: "telemetry_networkKey", params: nil, id: 3)
var buffer = ByteBuffer()
try buffer.writeJSONEncodable(req)
try app.test(.POST, "/", headers: ["Content-Type": "application/json"], body: buffer) { res in
#expect(res.status == .ok)
let resp = try res.content.decode(JSONResponse.self, using: JSONDecoder())
#expect((resp.result!.value as! Utils.JSON).string == "Ed25519:TestKey")
}
}
}

0 comments on commit 486706a

Please sign in to comment.