Skip to content

Commit

Permalink
[CHANGE] Disable webSocketTests that might fail randomly
Browse files Browse the repository at this point in the history
  • Loading branch information
dioKaratzas committed Aug 13, 2024
1 parent 6bc506a commit 7fcb919
Showing 1 changed file with 100 additions and 97 deletions.
197 changes: 100 additions & 97 deletions Tests/Web3SwiftTests/Client/EthereumClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -474,103 +474,106 @@ class EthereumWebSocketClientTests: EthereumClientTests {
XCTAssertEqual(client.currentState, WebSocketState.open)
}

func testWebSocketPendingTransactions() async {
do {
guard let client = client as? EthereumWebSocketClient else {
XCTFail("Expected client to be EthereumWebSocketClient")
return
}

var expectation: XCTestExpectation? = self.expectation(description: "Pending Transaction")
let subscription = try await client.pendingTransactions { _ in
expectation?.fulfill()
expectation = nil
}

await waitForExpectations(timeout: 5, handler: nil)

XCTAssertNotEqual(subscription.id, "")
XCTAssertEqual(subscription.type, .newPendingTransactions)
} catch {
XCTFail("Expected subscription but failed \(error).")
}
}

func testWebSocketNewBlockHeaders() async {
do {
guard let client = client as? EthereumWebSocketClient else {
XCTFail("Expected client to be EthereumWebSocketClient")
return
}

var expectation: XCTestExpectation? = self.expectation(description: "New Block Headers")
let subscription = try await client.newBlockHeaders { _ in
expectation?.fulfill()
expectation = nil
}

// we need a high timeout as new block might take a while
await waitForExpectations(timeout: 2500, handler: nil)

XCTAssertNotEqual(subscription.id, "")
XCTAssertEqual(subscription.type, .newBlockHeaders)
} catch {
XCTFail("Expected subscription but failed \(error).")
}
}

func testWebSocketLogs() async {
do {
guard let client = client as? EthereumWebSocketClient else {
XCTFail("Expected client to be EthereumWebSocketClient")
return
}

var expectation: XCTestExpectation? = self.expectation(description: "Logs")
let type = EthereumSubscriptionType.logs(nil)
let subscription = try await client.logs { log in
print(log)
expectation?.fulfill()
expectation = nil
}

// we need a high timeout as new block might take a while
await waitForExpectations(timeout: 2500, handler: nil)

XCTAssertNotEqual(subscription.id, "")
XCTAssertEqual(subscription.type, type)
} catch {
XCTFail("Expected subscription but failed \(error).")
}
}

func testWebSocketSubscribe() async {
do {
guard let client = client as? EthereumWebSocketClient else {
XCTFail("Expected client to be EthereumWebSocketClient")
return
}
client.delegate = self

delegateExpectation = expectation(description: "onNewPendingTransaction delegate call")
var subscription = try await client.subscribe(type: .newPendingTransactions)
await waitForExpectations(timeout: 10)
_ = try await client.unsubscribe(subscription)

delegateExpectation = expectation(description: "onNewBlockHeader delegate call")
subscription = try await client.subscribe(type: .newBlockHeaders)
await waitForExpectations(timeout: 2500)
_ = try await client.unsubscribe(subscription)

delegateExpectation = expectation(description: "onLogs delegate call")
let type = EthereumSubscriptionType.logs(nil)
subscription = try await client.subscribe(type: type)
await waitForExpectations(timeout: 2500)
_ = try await client.unsubscribe(subscription)
} catch {
XCTFail("Expected subscription but failed \(error).")
}
}
// These tests may fail if the blockchain isn't producing new blocks within the expected timeframes.
/*
func testWebSocketPendingTransactions() async {
do {
guard let client = client as? EthereumWebSocketClient else {
XCTFail("Expected client to be EthereumWebSocketClient")
return
}

var expectation: XCTestExpectation? = self.expectation(description: "Pending Transaction")
let subscription = try await client.pendingTransactions { _ in
expectation?.fulfill()
expectation = nil
}

await waitForExpectations(timeout: 5, handler: nil)

XCTAssertNotEqual(subscription.id, "")
XCTAssertEqual(subscription.type, .newPendingTransactions)
} catch {
XCTFail("Expected subscription but failed \(error).")
}
}

func testWebSocketNewBlockHeaders() async {
do {
guard let client = client as? EthereumWebSocketClient else {
XCTFail("Expected client to be EthereumWebSocketClient")
return
}

var expectation: XCTestExpectation? = self.expectation(description: "New Block Headers")
let subscription = try await client.newBlockHeaders { _ in
expectation?.fulfill()
expectation = nil
}

// we need a high timeout as new block might take a while
await waitForExpectations(timeout: 2500, handler: nil)

XCTAssertNotEqual(subscription.id, "")
XCTAssertEqual(subscription.type, .newBlockHeaders)
} catch {
XCTFail("Expected subscription but failed \(error).")
}
}

func testWebSocketLogs() async {
do {
guard let client = client as? EthereumWebSocketClient else {
XCTFail("Expected client to be EthereumWebSocketClient")
return
}

var expectation: XCTestExpectation? = self.expectation(description: "Logs")
let type = EthereumSubscriptionType.logs(nil)
let subscription = try await client.logs { log in
print(log)
expectation?.fulfill()
expectation = nil
}

// we need a high timeout as new block might take a while
await waitForExpectations(timeout: 2500, handler: nil)

XCTAssertNotEqual(subscription.id, "")
XCTAssertEqual(subscription.type, type)
} catch {
XCTFail("Expected subscription but failed \(error).")
}
}

func testWebSocketSubscribe() async {
do {
guard let client = client as? EthereumWebSocketClient else {
XCTFail("Expected client to be EthereumWebSocketClient")
return
}
client.delegate = self

delegateExpectation = expectation(description: "onNewPendingTransaction delegate call")
var subscription = try await client.subscribe(type: .newPendingTransactions)
await waitForExpectations(timeout: 10)
_ = try await client.unsubscribe(subscription)

delegateExpectation = expectation(description: "onNewBlockHeader delegate call")
subscription = try await client.subscribe(type: .newBlockHeaders)
await waitForExpectations(timeout: 2500)
_ = try await client.unsubscribe(subscription)

delegateExpectation = expectation(description: "onLogs delegate call")
let type = EthereumSubscriptionType.logs(nil)
subscription = try await client.subscribe(type: type)
await waitForExpectations(timeout: 2500)
_ = try await client.unsubscribe(subscription)
} catch {
XCTFail("Expected subscription but failed \(error).")
}
}
*/

func testWebSocketUnsubscribe() async {
do {
Expand Down

0 comments on commit 7fcb919

Please sign in to comment.