-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathConnectionTests.swift
executable file
·228 lines (196 loc) · 7.27 KB
/
ConnectionTests.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
//
// NatsClientTests.swift
// NatsSwiftTests
//
import XCTest
import NIO
import Logging
@testable import NatsSwift
class CoreNatsTests: XCTestCase {
static var allTests = [
("testPublish", testPublish),
("testPublishWithReply", testPublishWithReply),
("testSubscribe", testSubscribe),
("testConnect", testConnect),
("testReconnect", testReconnect),
("testUsernameAndPassword", testUsernameAndPassword),
("testTokenAuth", testTokenAuth)
]
var natsServer = NatsServer()
override func tearDown() {
super.tearDown()
natsServer.stop()
}
func testPublish() async throws {
natsServer.start()
logger.logLevel = .debug
let client = ClientOptions()
.url(URL(string: natsServer.clientURL)!)
.build()
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")
let iter = sub.makeAsyncIterator()
Task {
if let msg = await iter.next() {
XCTAssertEqual(msg.subject, "test")
expectation.fulfill()
}
}
wait(for: [expectation], timeout: 5.0)
await sub.complete()
}
func testPublishWithReply() async throws {
natsServer.start()
logger.logLevel = .debug
let client = ClientOptions()
.url(URL(string: natsServer.clientURL)!)
.build()
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")
let iter = sub.makeAsyncIterator()
Task {
if let msg = await iter.next() {
XCTAssertEqual(msg.subject, "test")
XCTAssertEqual(msg.replySubject, "reply")
expectation.fulfill()
}
}
wait(for: [expectation], timeout: 5.0)
await sub.complete()
}
func testSubscribe() async throws {
natsServer.start()
logger.logLevel = .debug
let client = ClientOptions().url(URL(string: natsServer.clientURL)!).build()
try await client.connect()
let sub = try await client.subscribe(to: "test")
try client.publish("msg".data(using: .utf8)!, subject: "test")
let iter = sub.makeAsyncIterator()
let message = await iter.next()
print( "payload: \(String(data:message!.payload!, encoding: .utf8)!)")
XCTAssertEqual(message?.payload, "msg".data(using: .utf8)!)
}
func testConnect() async throws {
natsServer.start()
logger.logLevel = .debug
let client = ClientOptions()
.url(URL(string: natsServer.clientURL)!)
.build()
try await client.connect()
XCTAssertNotNil(client, "Client should not be nil")
}
func testReconnect() async throws {
natsServer.start()
let port = natsServer.port!
logger.logLevel = .debug
let client = ClientOptions()
.url(URL(string: natsServer.clientURL)!)
.reconnectWait(1)
.build()
try await client.connect()
// Payload to publish
let payload = "hello".data(using: .utf8)!
var messagesReceived = 0
let sub = try! await client.subscribe(to: "foo")
// publish some messages
Task {
for _ in 0..<10 {
try client.publish(payload, subject: "foo")
}
}
// make sure sub receives messages
for await msg in sub {
messagesReceived += 1
if messagesReceived == 10 {
break
}
}
// restart the server
natsServer.stop()
sleep(1)
natsServer.start(port: port)
sleep(2)
// publish more messages, sub should receive them
Task {
for _ in 0..<10 {
try client.publish(payload, subject: "foo")
}
}
for await msg in sub {
messagesReceived += 1
if messagesReceived == 10 {
break
}
}
// Check if the total number of messages received matches the number sent
XCTAssertEqual(20, messagesReceived, "Mismatch in the number of messages sent and received")
}
func testUsernameAndPassword() async throws {
logger.logLevel = .debug
let currentFile = URL(fileURLWithPath: #file)
// Navigate up to the Tests directory
let testsDir = currentFile.deletingLastPathComponent().deletingLastPathComponent()
// Construct the path to the resource
let resourceURL = testsDir
.appendingPathComponent("Integration/Resources/creds.conf", isDirectory: false)
natsServer.start(cfg: resourceURL.path)
let client = ClientOptions()
.url(URL(string:natsServer.clientURL)!)
.username_and_password("derek", "s3cr3t")
.maxReconnects(5)
.build()
try await client.connect()
try client.publish("msg".data(using: .utf8)!, subject: "test")
try await client.flush()
try await client.subscribe(to: "test")
XCTAssertNotNil(client, "Client should not be nil")
// Test if client with bad credentials throws an error
let bad_creds_client = ClientOptions()
.url(URL(string:natsServer.clientURL)!)
.username_and_password("derek", "badpassword")
.maxReconnects(5)
.build()
do {
try await bad_creds_client.connect()
XCTFail("Should have thrown an error")
} catch {
XCTAssertNotNil(error, "Error should not be nil")
}
}
func testTokenAuth() async throws {
logger.logLevel = .debug
let currentFile = URL(fileURLWithPath: #file)
// Navigate up to the Tests directory
let testsDir = currentFile.deletingLastPathComponent().deletingLastPathComponent()
// Construct the path to the resource
let resourceURL = testsDir
.appendingPathComponent("Integration/Resources/token.conf", isDirectory: false)
natsServer.start(cfg: resourceURL.path)
let client = ClientOptions()
.url(URL(string:natsServer.clientURL)!)
.token("s3cr3t")
.maxReconnects(5)
.build()
try await client.connect()
try client.publish("msg".data(using: .utf8)!, subject: "test")
try await client.flush()
try await client.subscribe(to: "test")
XCTAssertNotNil(client, "Client should not be nil")
// Test if client with bad credentials throws an error
let bad_creds_client = ClientOptions()
.url(URL(string:natsServer.clientURL)!)
.token("badtoken")
.maxReconnects(5)
.build()
do {
try await bad_creds_client.connect()
XCTFail("Should have thrown an error")
} catch {
XCTAssertNotNil(error, "Error should not be nil")
}
}
}