Skip to content

Commit

Permalink
Improve Core NATS tests
Browse files Browse the repository at this point in the history
Signed-off-by: Tomasz Pietrek <tomasz@nats.io>
  • Loading branch information
Jarema committed Dec 29, 2023
1 parent 6c7e0ce commit 80962f2
Showing 1 changed file with 38 additions and 7 deletions.
45 changes: 38 additions & 7 deletions Tests/NatsSwiftTests/Integration/ConnectionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ import NIO
import Logging
@testable import NatsSwift

class ConnectionTests: XCTestCase {
class CoreNatsTests: XCTestCase {

static var allTests = [
("testNewClient", testNewClient)
("testPublish", testPublish),
("testPublishWithReply", testPublishWithReply),
("testConnect", testConnect)
]
var natsServer = NatsServer()

Expand All @@ -20,21 +22,50 @@ class ConnectionTests: XCTestCase {
natsServer.stop()
}

func testNewClient() async throws {
func testPublish() async throws {
natsServer.start()
logger.logLevel = .debug
print("Testing new client")
logger.debug("Testing new client with log")
let client = Client(url: URL(string: natsServer.clientURL)!)
try await client.connect()
let sub = try await client.subscribe(to: "test")

try client.publish("msg".data(using: .utf8)!, subject: "test")
let expectation = XCTestExpectation(description: "Should receive message in 5 seconsd")
Task {
if let msg = await sub.next() {
XCTAssertEqual(msg.subject, "test")
expectation.fulfill()
}
}
wait(for: [expectation], timeout: 5.0)
await sub.complete()
}

if let msg = await sub.next() {
print("Received on \(msg.subject): \(msg.payload!.toString()!)")
func testPublishWithReply() async throws {
natsServer.start()
logger.logLevel = .debug
let client = Client(url: URL(string: natsServer.clientURL)!)
try await client.connect()
let sub = try await client.subscribe(to: "test")

try client.publish("msg".data(using: .utf8)!, subject: "test", reply: "reply")
let expectation = XCTestExpectation(description: "Should receive message in 5 seconsd")
Task {
if let msg = await sub.next() {
XCTAssertEqual(msg.subject, "test")
XCTAssertEqual(msg.replySubject, "reply")
expectation.fulfill()
}
}
wait(for: [expectation], timeout: 5.0)
await sub.complete()
}

func testConnect() async throws {
natsServer.start()
logger.logLevel = .debug
let client = Client(url: URL(string: natsServer.clientURL)!)
try await client.connect()
XCTAssertNotNil(client, "Client should not be nil")
}
}

0 comments on commit 80962f2

Please sign in to comment.