Skip to content

Commit

Permalink
Run swift-format
Browse files Browse the repository at this point in the history
Signed-off-by: Tomasz Pietrek <tomasz@nats.io>
  • Loading branch information
Jarema committed Mar 12, 2024
1 parent 2807b6e commit b555f87
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 30 deletions.
10 changes: 5 additions & 5 deletions Sources/Example/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,32 +21,32 @@ let sub = try await nats.subscribe(to: "foo.>")

let loop = Task {
print("starting message loop...")

for try await msg in sub {

if msg.subject == "foo.done" {
break
}

if let payload = msg.payload {
print("received \(msg.subject): \(String(data: payload, encoding: .utf8) ?? "")")
}

if let headers = msg.headers {
if let headerValue = headers.get(try! HeaderName("X-Example")) {
print(" header: X-Example: \(headerValue.description)")
}
}
}

print("message loop done...")
}

print("publishing data...")
for i in 1...3 {
var headers = HeaderMap()
headers.append(try! HeaderName("X-Example"), HeaderValue("example value"))

if let data = "data\(i)".data(using: .utf8) {
try nats.publish(data, subject: "foo.\(i)", headers: headers)
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/NatsSwift/ConcurrentQueue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ import NIOConcurrencyHelpers
internal class ConcurrentQueue<T> {
private var elements: [T] = []
private let lock = NIOLock()

func enqueue(_ element: T) {
lock.lock()
defer { lock.unlock() }
elements.append(element)
}

func dequeue() -> T? {
lock.lock()
defer { lock.unlock() }
Expand Down
24 changes: 12 additions & 12 deletions Sources/NatsSwift/NatsClient/NatsClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -88,23 +88,23 @@ extension Client {
}

public func request(
_ payload: Data, to: String, timeout: TimeInterval = 5, headers: HeaderMap? = nil
) async throws -> NatsMessage{
_ payload: Data, to: String, timeout: TimeInterval = 5, headers: HeaderMap? = nil
) async throws -> NatsMessage {
logger.debug("request")
guard let connectionHandler = self.connectionHandler else {
throw NatsClientError("internal error: empty connection handler")
}
do {
let inbox = "_INBOX.\(UUID().uuidString)"
let response = try await connectionHandler.subscribe(inbox)
try connectionHandler.write(operation: ClientOp.publish((to, inbox, payload, headers)))
connectionHandler.channel?.flush()
if let message = await response.makeAsyncIterator().next() {
return message
} else {
throw NatsClientError("response subscription closed")

}
let response = try await connectionHandler.subscribe(inbox)
try connectionHandler.write(operation: ClientOp.publish((to, inbox, payload, headers)))
connectionHandler.channel?.flush()
if let message = await response.makeAsyncIterator().next() {
return message
} else {
throw NatsClientError("response subscription closed")

}
} catch {
throw NatsClientError("failed to send request")
}
Expand Down Expand Up @@ -133,6 +133,6 @@ extension Client {
}
let ping = RttCommand.makeFrom(channel: connectionHandler.channel)
connectionHandler.sendPing(ping)
return try await ping.getRoundTripTime ()
return try await ping.getRoundTripTime()
}
}
8 changes: 4 additions & 4 deletions Sources/NatsSwift/RttCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@ import NIOCore
internal class RttCommand {
let startTime = ContinuousClock().now
let promise: EventLoopPromise<Duration>?

static func makeFrom(channel: Channel?) -> RttCommand {
RttCommand(promise: channel?.eventLoop.makePromise(of: Duration.self))
}

private init(promise: EventLoopPromise<Duration>?) {
self.promise = promise
}

func setRoundTripTime() {
let now: ContinuousClock.Instant = ContinuousClock().now
let rtt: Duration = now - startTime
promise?.succeed(rtt)
}

func getRoundTripTime() async throws -> Duration {
try await promise?.futureResult.get() ?? Duration.zero
}
Expand Down
14 changes: 7 additions & 7 deletions Tests/NatsSwiftTests/Integration/ConnectionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ class CoreNatsTests: XCTestCase {
XCTFail("Expected error from connect")
}

func testLameDuckMode() async throws {
func testLameDuckMode() async throws {
natsServer.start()
logger.logLevel = .debug

Expand All @@ -458,13 +458,13 @@ class CoreNatsTests: XCTestCase {
let client = ClientOptions().url(URL(string: natsServer.clientURL)!).build()

Task {
let service = try await client.subscribe(to: "service")
for await message in service {
try client.publish("reply".data(using: .utf8)!, subject: message.replySubject!)
}
let service = try await client.subscribe(to: "service")
for await message in service {
try client.publish("reply".data(using: .utf8)!, subject: message.replySubject!)
}

let response = try await client.request("request".data(using: .utf8)!, to: "service")
XCTAssertEqual(response.payload, "reply".data(using: .utf8)!)
let response = try await client.request("request".data(using: .utf8)!, to: "service")
XCTAssertEqual(response.payload, "reply".data(using: .utf8)!)
}
}
}

0 comments on commit b555f87

Please sign in to comment.