-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_async_stream_from_swift.swift
186 lines (163 loc) Β· 5.18 KB
/
test_async_stream_from_swift.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import Foundation
import ffibre
/* Some bug in UniFFI not marking the `uniffi::Error` as `Swift.Error`... */
extension FfiNetworkingError: Swift.Error {}
extension FfiNetworkingResponse {
init(data: Data, urlResponse: URLResponse) {
guard let httpUrlResponse = urlResponse as? HTTPURLResponse else {
fatalError("Expected URLResponse to always be HTTPURLResponse")
}
self.init(statusCode: UInt16(httpUrlResponse.statusCode), body: data)
}
}
extension Transaction: CustomStringConvertible {
public var description: String {
"""
Transaction(
epoch: \(self.epoch),
round: \(self.round),
txID: \(self.txId),
fee: \(self.feePaid)
)
"""
}
}
extension FfiNetworkingOutcome {
static func fail(error: Swift.Error, data: Data? = nil, urlResponse: URLResponse? = nil) -> Self {
func message() -> String? {
data.map { String(data: $0, encoding: .utf8) } ?? nil
}
func statusCode() -> UInt16? {
urlResponse.map { $0 as? HTTPURLResponse ?? nil }?.map { UInt16($0.statusCode) } ?? nil
}
return .failure(
error: .requestFailed(
statusCode: statusCode(),
urlSessionUnderlyingError: String(describing: error),
errorMessageFromGateway: message()
)
)
}
static func with(
data: Data?,
urlResponse: URLResponse?,
error: Swift.Error?
) -> Self {
if let error {
return .fail(error: error, data: data, urlResponse: urlResponse)
}
guard let data else { fatalError("If error is nil data SHOULD be present if error is nil.") }
guard let urlResponse else {
fatalError("Expected URLResponse to always be present if error is nil and data is some.")
}
return .success(value: FfiNetworkingResponse(data: data, urlResponse: urlResponse))
}
}
extension FfiNetworkingRequest {
// Convert `[Rust]FfiNetworkingRequest` to `[Swift]URLRequest`
func urlRequest(url: URL) -> URLRequest {
var request = URLRequest(url: url)
request.httpMethod = self.method
request.httpBody = self.body
request.allHTTPHeaderFields = self.headers
return request
}
func urlRequest() throws -> URLRequest {
guard let url = URL(string: self.url) else {
throw FfiNetworkingError.failedToCreateUrlFrom(string: self.url)
}
return self.urlRequest(url: url)
}
}
// Conform `[Swift]URLSession` to `[Rust]FfiNetworkingExecutor`
extension URLSession: FfiNetworkingExecutor {
public func executeNetworkingRequest(
request rustRequest: FfiNetworkingRequest,
listenerRustSide: FfiNetworkingOutcomeListener
) throws {
guard let url = URL(string: rustRequest.url) else {
throw FfiNetworkingError.failedToCreateUrlFrom(string: rustRequest.url)
}
let task = dataTask(with: rustRequest.urlRequest(url: url)) { data, urlResponse, error in
let result = FfiNetworkingOutcome.with(
data: data,
urlResponse: urlResponse,
error: error
)
listenerRustSide.notifyOutcome(result: result)
}
task.resume()
}
}
extension AsyncStream where Element: Equatable {
static func new(
label: String,
nextElement: @escaping () async throws -> Element
) -> (
stream: Self, cancel: () -> Void
) {
var cancel: (() -> Void)!
let stream = AsyncStream<Element> { (continuation: AsyncStream<Element>.Continuation) in
let task = Task {
var last: Element?
while !Task.isCancelled {
try Task.checkCancellation()
let value = try await nextElement()
if value != last {
continuation.yield(value)
} else {
print("SWIFT π \(label) duplicate ignored")
}
last = value
try await Task.sleep(for: .seconds(7))
}
continuation.finish()
}
cancel = {
task.cancel()
}
continuation.onTermination = { termination in
task.cancel()
}
}
return (stream, cancel)
}
}
extension GatewayClient {
func txStream(label: String) -> (stream: AsyncStream<Transaction>, cancel: () -> Void) {
AsyncStream.new(label: label) { [unowned self] in await self.getLatestTransactionsOrPanic() }
}
}
func test_async_stream() async throws {
let gatewayClient = GatewayClient(
networkAntenna: URLSession.shared
)
await withDiscardingTaskGroup { taskGroup in
taskGroup.addTask {
let label = "FOO"
let (stream, cancel) = gatewayClient.txStream(label: label)
for await tx in stream.prefix(3) {
print("ππ π SWIFT \(label) async value from stream: \(tx)")
print("ππ π SWIFT β¨ cancelling \(label) task (and breaking...)")
cancel();break
}
}
taskGroup.addTask {
let label = "BAR"
let (stream, _) = gatewayClient.txStream(label: label)
for await tx in stream.prefix(3) {
print("ππ πSWIFT \(label) async value from stream: \(tx)")
}
}
}
}
func test() async throws {
print("ππ SWIFT 'test_test_async_stream' start")
defer { print("ππ SWIFT 'test_test_async_stream' done") }
do {
try await test_async_stream()
} catch {
print("π β SWIFT 'test_async_stream' error: \(String(describing: error))")
}
}
try! await test()