-
Notifications
You must be signed in to change notification settings - Fork 169
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
WebSockets #227
Closed
Closed
WebSockets #227
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ | |
import XCTest | ||
@testable import web3 | ||
import BigInt | ||
import NIO | ||
|
||
struct TransferMatchingSignatureEvent: ABIEvent { | ||
public static let name = "Transfer" | ||
|
@@ -31,9 +32,8 @@ struct TransferMatchingSignatureEvent: ABIEvent { | |
} | ||
} | ||
|
||
|
||
class EthereumClientTests: XCTestCase { | ||
var client: EthereumClient? | ||
var client: EthereumClientProtocol? | ||
var account: EthereumAccount? | ||
|
||
override func setUp() { | ||
|
@@ -110,7 +110,7 @@ class EthereumClientTests: XCTestCase { | |
|
||
func testEthGetCode() async { | ||
do { | ||
let code = try await client?.eth_getCode(address: EthereumAddress("0x112234455c3a32fd11230c42e7bccd4a84e02010")) | ||
let code = try await client?.eth_getCode(address: EthereumAddress("0x112234455c3a32fd11230c42e7bccd4a84e02010"), block: .Latest) | ||
XCTAssertNotNil(code, "Contract code not available") | ||
} catch { | ||
XCTFail("Expected code but failed \(error).") | ||
|
@@ -443,3 +443,154 @@ struct InvalidMethodB: ABIFunction { | |
func encode(to encoder: ABIFunctionEncoder) throws { | ||
} | ||
} | ||
|
||
class EthereumWebSocketClientTests: EthereumClientTests { | ||
var delegateExpectation: XCTestExpectation? | ||
|
||
override func setUp() { | ||
super.setUp() | ||
self.client = EthereumWebSocketClient(url: TestConfig.wssUrl, configuration: TestConfig.webSocketConfig) | ||
|
||
} | ||
#if os(Linux) | ||
// On Linux some tests are fail. Need investigation | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Need to add a TODO/issue/checklist in PR before merging |
||
#else | ||
func testWebSocketState() { | ||
guard let client = client as? EthereumWebSocketClient else { | ||
XCTFail("Expected client to be EthereumWebSocketClient") | ||
return | ||
} | ||
XCTAssertEqual(client.currentState, EthereumWebSocketClient.State.open) | ||
|
||
let clientClose = client.eventLoopGroup.next().makePromise(of: Void.self) | ||
client.exposeWebSocket()?.onClose.cascade(to: clientClose) | ||
client.disconnect() | ||
XCTAssertNoThrow(try clientClose.futureResult.wait()) | ||
XCTAssertEqual(client.currentState, EthereumWebSocketClient.State.closed) | ||
|
||
client.connect() | ||
XCTAssertEqual(client.currentState, EthereumWebSocketClient.State.open) | ||
} | ||
|
||
func testWebSocketNoAutomaticOpen() { | ||
self.client = EthereumWebSocketClient(url: TestConfig.wssUrl, configuration: .init(automaticOpen: false)) | ||
|
||
guard let client = client as? EthereumWebSocketClient else { | ||
XCTFail("Expected client to be EthereumWebSocketClient") | ||
return | ||
} | ||
|
||
XCTAssertEqual(client.currentState, EthereumWebSocketClient.State.closed) | ||
} | ||
|
||
func testWebSocketConnect() { | ||
self.client = EthereumWebSocketClient(url: TestConfig.wssUrl, configuration: .init(automaticOpen: false)) | ||
|
||
guard let client = client as? EthereumWebSocketClient else { | ||
XCTFail("Expected client to be EthereumWebSocketClient") | ||
return | ||
} | ||
|
||
XCTAssertEqual(client.currentState, EthereumWebSocketClient.State.closed) | ||
|
||
client.connect() | ||
|
||
XCTAssertEqual(client.currentState, EthereumWebSocketClient.State.open) | ||
XCTAssertTrue(!client.exposeWebSocket()!.isClosed) | ||
} | ||
|
||
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 { txHash in | ||
expectation?.fulfill() | ||
expectation = nil | ||
} | ||
|
||
await waitForExpectations(timeout: 5, handler: nil) | ||
|
||
XCTAssertNotEqual(subscription.id, "") | ||
XCTAssertEqual(subscription.type, .pendingTransactions) | ||
} 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 { header 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 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: .pendingTransactions) | ||
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) | ||
} catch { | ||
XCTFail("Expected subscription but failed \(error).") | ||
} | ||
} | ||
|
||
func testWebSocketUnsubscribe() async { | ||
do { | ||
guard let client = client as? EthereumWebSocketClient else { | ||
XCTFail("Expected client to be EthereumWebSocketClient") | ||
return | ||
} | ||
|
||
let subscription = try await client.subscribe(type: .newBlockHeaders) | ||
let result = try await client.unsubscribe(subscription) | ||
XCTAssertTrue(result) | ||
} catch { | ||
XCTFail("Expected subscription but failed \(error).") | ||
} | ||
} | ||
#endif | ||
} | ||
|
||
extension EthereumWebSocketClientTests: EthereumWebSocketClientDelegate { | ||
func onNewPendingTransaction(subscription: EthereumSubscription, txHash: String) { | ||
delegateExpectation?.fulfill() | ||
delegateExpectation = nil | ||
} | ||
|
||
func onNewBlockHeader(subscription: EthereumSubscription, header: EthereumHeader) { | ||
delegateExpectation?.fulfill() | ||
delegateExpectation = nil | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wondering if we should create different products in package definition to allow importing WSS only if required. What do you think @dnKaratzas ?