-
Notifications
You must be signed in to change notification settings - Fork 3
/
SwiftRedisTests.swift
654 lines (501 loc) · 26 KB
/
SwiftRedisTests.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
//
// SwiftRedisTests.swift
// SwiftRedisTests
//
// Created by Ron Perry on 11/9/15.
// Copyright © 2015 Ron Perry. All rights reserved.
//
import XCTest
@testable import SwiftRedis
class RedisInterfaceTests: XCTestCase
{
func testReadmeExample()
{
let redis = RedisInterface(host: ConnectionParams.serverAddress, port: ConnectionParams.serverPort, auth: ConnectionParams.auth)
// let redis = RedisInterface(host: <host-address String>, port: <port Int>, auth: <auth String>)
// Queue a request to initiate a connection.
// Once a connection is established, an AUTH command will be issued with the auth parameters specified above.
redis.connect()
// Queue a request to set a value for a key in the Redis database. This command will only
// execute after the connection is established and authenticated.
redis.setValueForKey("some:key", stringValue: "a value", completionHandler: { success, cmd in
// this completion handler will be executed after the SET command returns
if success {
print("value stored successfully")
} else {
print("value was not stored")
}
})
// Queue a request to get the value of a key in the Redis database. This command will only
// execute after the previous command is complete.
redis.getValueForKey("some:key", completionHandler: { success, key, data, cmd in
if success {
print("the stored data for \(key) is \(data!.stringVal)")
} else {
print("could not get value for \(key)")
}
})
// Queue a QUIT command (the connection will close when the QUIT command returns)
var quitComplete: Bool = false
let doneExpectation = expectation(description: "done")
redis.quit({success, cmd in
quitComplete = true
doneExpectation.fulfill()
})
waitForExpectations(timeout: 10, handler: { error in
XCTAssert(quitComplete)
})
}
func testSetAndGet()
{
let r = RedisInterface(host: ConnectionParams.serverAddress, port: ConnectionParams.serverPort, auth: ConnectionParams.auth)
r.connect()
let arr = [UInt8](repeating: 6, count: 230*1024)
let data1 = Data(bytes: arr)
let data3 = "hi".data(using: String.Encoding.utf8)!
let data2 = "hello, world".data(using: String.Encoding.utf8)!
// store data1
let storedExpectation1 = expectation(description: "testkey1 stored")
r.setDataForKey("testkey-getset", data: data1, completionHandler: { success, cmd in
XCTAssertTrue(success, "expecting success storing data for testkey1")
storedExpectation1.fulfill()
})
waitForExpectations(timeout: 20, handler: { error in
XCTAssertNil(error, "expecting operation to succeed")
})
// retrieve data1
let storedExpectation2 = expectation(description: "testkey1 retrieved")
r.getDataForKey("testkey-getset", completionHandler: { success, key, data, cmd in
storedExpectation2.fulfill()
XCTAssertTrue(success)
XCTAssert(key == "testkey-getset")
XCTAssert(data! == RedisResponse(dataVal: data1))
})
waitForExpectations(timeout: 20, handler: { error in
XCTAssertNil(error, "expecting operation to succeed")
})
// store data2
let storedExpectation3 = expectation(description: "testkey2 stored")
r.setDataForKey("testkey-getset", data: data2, completionHandler: { success, cmd in
XCTAssertTrue(success, "could not store testkey2")
storedExpectation3.fulfill()
})
waitForExpectations(timeout: 5, handler: { error in
XCTAssertNil(error, "Error")
})
// retrieve data2
let storedExpectation4 = expectation(description: "testkey2 stored")
r.getDataForKey("testkey-getset", completionHandler: { success, key, data, cmd in
storedExpectation4.fulfill()
XCTAssertTrue(success)
XCTAssert(key == "testkey-getset")
XCTAssert(data! == RedisResponse(dataVal: data2))
})
waitForExpectations(timeout: 5, handler: { error in
XCTAssertNil(error, "Error")
})
// store data3
let storedExpectation_d3a = expectation(description: "testkey3 stored")
r.setDataForKey("testkey-getset", data: data3, completionHandler: { success, cmd in
XCTAssertTrue(success, "could not store testkey3")
storedExpectation_d3a.fulfill()
})
waitForExpectations(timeout: 5, handler: { error in
XCTAssertNil(error, "Error")
})
// retrieve data3
let storedExpectation_d3b = expectation(description: "testkey3 stored")
r.getDataForKey("testkey-getset", completionHandler: { success, key, data, cmd in
storedExpectation_d3b.fulfill()
XCTAssertTrue(success)
XCTAssert(key == "testkey-getset")
XCTAssert(data! == RedisResponse(dataVal: data3))
})
waitForExpectations(timeout: 5, handler: { error in
XCTAssertNil(error, "Error")
})
// quit
let storedExpectation5 = expectation(description: "quit complete")
r.quit({ success in
storedExpectation5.fulfill()
})
waitForExpectations(timeout: 5, handler: { error in
XCTAssertNil(error, "Error")
})
}
func testSkipPendingCommandsAndQuit()
{
let r = RedisInterface(host: ConnectionParams.serverAddress, port: ConnectionParams.serverPort, auth: ConnectionParams.auth)
r.connect() // this stores an AUTH command in the queue. since we are running in a single thread,
// the command will not be sent before this routine reaches "waitForExpectation"
let storedExpectation = expectation(description: "a handler was called")
// queue a command that we do not expect will execute
r.setValueForKey("testkey1", stringValue: "a value", completionHandler: { success, cmd in
XCTAssertFalse(true, "not expecting this handler to be called, because the call to testSkipPendingCommandsAndQuit() should have removed the command from the queue")
storedExpectation.fulfill()
})
var quitHandlerCalled = false
r.skipPendingCommandsAndQuit({ success, cmd in
XCTAssertTrue(success == true, "expecting quit handler to succeed")
quitHandlerCalled = true
storedExpectation.fulfill()
})
waitForExpectations(timeout: 2, handler: { error in
XCTAssertNil(error, "expecting operation to succeed")
XCTAssertTrue(quitHandlerCalled, "expecting quit handler to have been called")
})
}
func testPubSub()
{
let riPublish = RedisInterface(host: ConnectionParams.serverAddress, port: ConnectionParams.serverPort, auth: ConnectionParams.auth)
riPublish.connect()
let riSubscribe = RedisInterface(host: ConnectionParams.serverAddress, port: ConnectionParams.serverPort, auth: ConnectionParams.auth)
riSubscribe.connect()
let expectingSubscribeToReturn1 = expectation(description: "subscribe operation returned once")
var expectingSubscribeToReturn2: XCTestExpectation? = nil
var expectingSubscribeToReturn3: XCTestExpectation? = nil
var subscribeReturnCount = 0
// subscribe to channel "testchannel"
// important assumption: no one else is subscribed to this channel!!!
riSubscribe.subscribe("testchannel", completionHandler: { success, channel, data, cmd in
// this completion handler should be called several times.
// the first time: to acknowledge that the subscribe operation was registered
// the next two times: in response to publish operations
switch subscribeReturnCount {
case 0:
XCTAssertTrue(success)
XCTAssert(channel == "testchannel")
XCTAssert(data! == RedisResponse(arrayVal: [
RedisResponse(dataVal: "subscribe".data(using: String.Encoding.utf8)),
RedisResponse(dataVal: "testchannel".data(using: String.Encoding.utf8)),
RedisResponse(intVal: 1)
]))
XCTAssertNotNil(expectingSubscribeToReturn1)
expectingSubscribeToReturn1.fulfill()
subscribeReturnCount += 1
case 1:
XCTAssertTrue(success)
XCTAssert(channel == "testchannel")
XCTAssert(data! == RedisResponse(arrayVal: [
RedisResponse(dataVal: "message".data(using: String.Encoding.utf8)),
RedisResponse(dataVal: "testchannel".data(using: String.Encoding.utf8)),
RedisResponse(dataVal: "publish op 1".data(using: String.Encoding.utf8)),
]))
XCTAssertNotNil(expectingSubscribeToReturn2)
expectingSubscribeToReturn2!.fulfill()
subscribeReturnCount += 1
case 2:
XCTAssertTrue(success)
XCTAssert(channel == "testchannel")
XCTAssert(data! == RedisResponse(arrayVal: [
RedisResponse(dataVal: "message".data(using: String.Encoding.utf8)),
RedisResponse(dataVal: "testchannel".data(using: String.Encoding.utf8)),
RedisResponse(dataVal: "publish op 2".data(using: String.Encoding.utf8)),
]))
XCTAssertNotNil(expectingSubscribeToReturn3)
expectingSubscribeToReturn3!.fulfill()
subscribeReturnCount+=1
default:
XCTAssert(false)
}
})
// wait for the subscribe operation to complete
waitForExpectations(timeout: 1, handler: { error in
XCTAssertNil(error, "expecting operation to succeed")
})
XCTAssertEqual(subscribeReturnCount, 1)
// -----
// publish something to the test channel
expectingSubscribeToReturn2 = expectation(description: "subscribe operation returned twice")
let expectingPublishToComplete1 = expectation(description: "publish operation 1 completed")
riPublish.publish("testchannel", value: "publish op 1", completionHandler: { success, key, data, cmd in
expectingPublishToComplete1.fulfill()
})
// wait for both the publish to complete, and the subscribe to return the 2nd time
waitForExpectations(timeout: 1, handler: { error in
XCTAssertNil(error, "expecting publish 1 to complete, and subscribe to return 2nd time")
})
XCTAssertEqual(subscribeReturnCount, 2)
// -----
// publish something else to the test channel
expectingSubscribeToReturn3 = expectation(description: "subscribe operation returned third time")
let expectingPublishToComplete2 = expectation(description: "publish operation 2 completed")
riPublish.publish("testchannel", value: "publish op 2", completionHandler: { success, key, data, cmd in
expectingPublishToComplete2.fulfill()
})
// wait for both the publish to complete, and the subscribe to return the 2nd time
waitForExpectations(timeout: 2, handler: { error in
XCTAssertNil(error, "expecting publish 2 to complete, and subscribe to return 3nd time")
})
XCTAssertEqual(subscribeReturnCount, 3)
}
}
class RedisConnectionTests: XCTestCase {
let authCmd = RedisCommand.Auth(ConnectionParams.auth, handler: nil)
func testAuthentication()
{
let r = RedisConnection(serverAddress: ConnectionParams.serverAddress, serverPort: ConnectionParams.serverPort)
r.connect()
// test that it works once
let storedExpectation1 = expectation(description: "set command handler activated")
let cmd = RedisCommand.Auth(ConnectionParams.auth, handler: { success, cmd in
XCTAssertTrue(success, "auth command expected to succeed")
XCTAssert(cmd.response! == RedisResponse(stringVal: "OK"), "expecting response from Redis to be OK")
storedExpectation1.fulfill()
})
r.setPendingCommand(cmd)
waitForExpectations(timeout: 2, handler: { error in
XCTAssertNil(error, "Error")
})
}
func testDisconnect()
{
let r = RedisConnection(serverAddress: ConnectionParams.serverAddress, serverPort: ConnectionParams.serverPort)
r.connect()
// test that it works once
let storedExpectation1 = expectation(description: "set command handler activated")
let cmd = RedisCommand.Auth(ConnectionParams.auth, handler: { success, cmd in
XCTAssertTrue(success, "auth command expected to succeed")
XCTAssert(cmd.response! == RedisResponse(stringVal: "OK"), "expecting response from Redis to be OK")
storedExpectation1.fulfill()
})
r.setPendingCommand(cmd)
waitForExpectations(timeout: 2, handler: { error in
XCTAssertNil(error, "Error")
})
r.disconnect()
r.connect()
// test that it works again
let storedExpectation2 = expectation(description: "set command handler activated second time")
let cmd2 = RedisCommand.Auth(ConnectionParams.auth, handler: { success, cmd in
XCTAssertTrue(success, "auth command expected to succeed again")
XCTAssert(cmd.response! == RedisResponse(stringVal: "OK"), "expecting second response from Redis to be OK")
storedExpectation2.fulfill()
})
r.setPendingCommand(cmd2)
waitForExpectations(timeout: 2, handler: { error in
XCTAssertNil(error, "Error")
})
}
func testSavingDataWithoutAuthentication()
{
let r = RedisConnection(serverAddress: ConnectionParams.serverAddress, serverPort: ConnectionParams.serverPort)
r.connect()
// test that it works once
let storedExpectation1 = expectation(description: "set command handler activated")
let cmd = RedisCommand.Set("A", valueToSet: "1", handler: { success, cmd in
XCTAssertFalse(success, "set command expected to fail")
XCTAssert(cmd.response! == RedisResponse(errorVal: "NOAUTH Authentication required"), "expecting response from Redis to be NOAUTH Authentication Required")
storedExpectation1.fulfill()
})
r.setPendingCommand(cmd)
waitForExpectations(timeout: 2, handler: { error in
XCTAssertNil(error, "Error")
})
// now ensure that works the second time too
let storedExpectation2 = expectation(description: "set command handler activated again")
let cmd2 = RedisCommand.Set("A", valueToSet: "1", handler: { success, cmd in
XCTAssertFalse(success, "set command expected to fail")
XCTAssert(cmd.response! == RedisResponse(errorVal: "NOAUTH Authentication required"), "expecting response from Redis to be NOAUTH Authentication Required")
storedExpectation2.fulfill()
})
r.setPendingCommand(cmd2)
waitForExpectations(timeout: 2, handler: { error in
XCTAssertNil(error, "Error")
})
}
}
class RedisParserTests: XCTestCase {
func testRedisResponse()
{
var r = RedisResponse(stringVal: "abc")
XCTAssert(r.responseType == .string)
r = RedisResponse(intVal: 3)
XCTAssert(r.responseType == .int)
r = RedisResponse(errorVal: "abc")
XCTAssert(r.responseType == .error)
r = RedisResponse(dataVal: Data())
XCTAssert(r.responseType == .data)
r = RedisResponse(dataVal: NSMutableData() as Data)
XCTAssert(r.responseType == .data)
r = RedisResponse(arrayVal: [RedisResponse(intVal: 1), RedisResponse(stringVal: "hi")])
XCTAssert(r.responseType == .array)
XCTAssert(RedisResponse(intVal: 1) == RedisResponse(intVal: 1))
XCTAssert(RedisResponse(intVal: 1) != RedisResponse(intVal: 2))
XCTAssert(RedisResponse(stringVal: "a") != RedisResponse(intVal: 2))
XCTAssert(RedisResponse(arrayVal: [RedisResponse(stringVal: "a"), RedisResponse(errorVal: "err")]) == RedisResponse(arrayVal: [RedisResponse(stringVal: "a"), RedisResponse(errorVal: "err")]))
XCTAssert(RedisResponse(arrayVal: [RedisResponse(stringVal: "a"), RedisResponse(errorVal: "err")]) != RedisResponse(arrayVal: [RedisResponse(stringVal: "a"), RedisResponse(errorVal: "err1")]))
}
func testRedisParser()
{
let parser = RedisResponseParser()
let resp1 = "+OK\r\n"
parser.storeReceivedData(resp1.data(using: String.Encoding.utf8)!)
XCTAssertEqual(parser.haveResponse, true)
XCTAssert(parser.lastResponse! == RedisResponse(stringVal: "OK"))
parser.storeReceivedString("+")
XCTAssertEqual(parser.haveResponse, false)
parser.storeReceivedString("OK")
XCTAssertEqual(parser.haveResponse, false)
parser.storeReceivedString("\r\n")
XCTAssertEqual(parser.haveResponse, true)
XCTAssert(parser.lastResponse! == RedisResponse(stringVal: "OK"))
parser.storeReceivedString("+")
XCTAssertEqual(parser.haveResponse, false)
parser.storeReceivedString("OK")
XCTAssertEqual(parser.haveResponse, false)
parser.storeReceivedString("\r")
XCTAssertEqual(parser.haveResponse, false)
parser.storeReceivedString("\r\n")
XCTAssertEqual(parser.haveResponse, true)
XCTAssert(parser.lastResponse! == RedisResponse(stringVal: "OK\r"))
parser.storeReceivedString(":476\r\n")
XCTAssert(parser.lastResponse! == RedisResponse(intVal: 476))
parser.storeReceivedString("$12\r\nabcde\r\nfghij\r\n")
let respData = "abcde\r\nfghij".data(using: String.Encoding.utf8)
XCTAssert(parser.lastResponse! == RedisResponse(dataVal: respData))
let data2 = "hello\r\n, world".data(using: String.Encoding.utf8)
parser.storeReceivedString("$\(data2!.count)\r\n")
parser.storeReceivedData(data2!)
XCTAssertEqual(parser.haveResponse, false)
parser.storeReceivedString("\r\n")
XCTAssertEqual(parser.haveResponse, true)
XCTAssert(parser.lastResponse! == RedisResponse(dataVal: data2))
parser.storeReceivedString(":123\r\n")
XCTAssert(parser.lastResponse! == RedisResponse(intVal: 123))
parser.storeReceivedString("$12\r\nabcde\r\nfghij")
XCTAssertEqual(parser.haveResponse, false)
parser.storeReceivedString("\r\n")
XCTAssertEqual(parser.haveResponse, true)
XCTAssert(parser.lastResponse! == RedisResponse(dataVal: respData))
}
func testArrayParsing()
{
let parser = RedisResponseParser()
parser.storeReceivedString("*3\r\n+subscribe\r\n+ev1\r\n:1\r\n")
XCTAssertEqual(parser.haveResponse, true)
if parser.haveResponse {
XCTAssert(parser.lastResponse! == RedisResponse(arrayVal: [
RedisResponse(stringVal: "subscribe"),
RedisResponse(stringVal: "ev1"),
RedisResponse(intVal: 1)
]))
}
}
func testAbortWhileParsing()
{
// this class allows us to test whether the parser correctly reports the abort to its delegate
class ParserDelegateForTestingAbort : RedisResponseParserDelegate {
var errorReported = false
var responseReported = false
var abortReported = false
func errorParsingResponse(_ error: String?) {
errorReported = true
}
func parseOperationAborted() {
abortReported = true
}
func receivedResponse(_ response: RedisResponse) {
responseReported = true
}
func reset() {
errorReported = false
abortReported = false
responseReported = false
}
}
let d = ParserDelegateForTestingAbort()
let parser = RedisResponseParser()
parser.setDelegate(d)
// in the middle of processing an array element
parser.storeReceivedString("*4\r\n+sub")
XCTAssertEqual(parser.haveResponse, false)
parser.abortParsing()
XCTAssertFalse(d.errorReported, "Parser should not report an error to delegate")
XCTAssertTrue(d.abortReported, "Parser should report an abort to delegate")
XCTAssertFalse(d.responseReported, "Parser should not report a response to delegate")
d.reset()
/// ensure can now process a full array
parser.storeReceivedString("*3\r\n+subscribe\r\n+ev1\r\n:1\r\n")
XCTAssertEqual(parser.haveResponse, true)
if parser.haveResponse {
XCTAssert(parser.lastResponse! == RedisResponse(arrayVal: [
RedisResponse(stringVal: "subscribe"),
RedisResponse(stringVal: "ev1"),
RedisResponse(intVal: 1)
]))
}
XCTAssertFalse(d.errorReported, "Parser should not report an error to delegate")
XCTAssertFalse(d.abortReported, "Parser should not report an abort to delegate")
XCTAssertTrue(d.responseReported, "Parser should report a response to delegate")
// in the middle of processing an array
d.reset()
parser.storeReceivedString("*4\r\n+sub\r\n")
XCTAssertEqual(parser.haveResponse, false)
parser.abortParsing()
XCTAssertFalse(d.errorReported, "Parser should not report an error to delegate")
XCTAssertTrue(d.abortReported, "Parser should report an abort to delegate")
XCTAssertFalse(d.responseReported, "Parser should not report a response to delegate")
// in the middle of processing a string
d.reset()
parser.storeReceivedString("+sub")
XCTAssertEqual(parser.haveResponse, false)
parser.abortParsing()
XCTAssertFalse(d.errorReported, "Parser should not report an error to delegate")
XCTAssertTrue(d.abortReported, "Parser should report an abort to delegate")
XCTAssertFalse(d.responseReported, "Parser should not report a response to delegate")
}
func testErrorHandling()
{
// TODO: test error handling
}
func testRedisBuffer()
{
let buf = RedisBuffer()
// simple test
buf.storeReceivedString("123")
XCTAssertEqual(buf.getNextStringOfSize(1), "1")
XCTAssertEqual(buf.getNextStringOfSize(1), "2")
XCTAssertEqual(buf.getNextStringOfSize(1), "3")
XCTAssertEqual(buf.getNextStringOfSize(1), nil)
// now get longer string back
buf.storeReceivedString("123")
XCTAssertEqual(buf.getNextStringOfSize(3), "123")
XCTAssertEqual(buf.getNextStringOfSize(1), nil)
// store in parts
buf.storeReceivedString("1")
buf.storeReceivedString("2")
buf.storeReceivedString("34")
XCTAssertEqual(buf.getNextStringOfSize(4), "1234")
XCTAssertEqual(buf.getNextStringOfSize(1), nil)
// ensure nil if not enough data
buf.storeReceivedString("123")
XCTAssertEqual(buf.getNextStringOfSize(4), nil)
XCTAssertEqual(buf.getNextStringOfSize(3), "123")
XCTAssertEqual(buf.getNextStringOfSize(1), nil)
// basic "CRLF" behavior
buf.storeReceivedString("abcde\r\n")
XCTAssertEqual(buf.getNextStringUntilCRLF(), "abcde")
XCTAssertEqual(buf.getNextStringUntilCRLF(), nil)
XCTAssertEqual(buf.getNextStringOfSize(1), nil)
// partial string behavior
buf.storeReceivedString("abcde\r")
XCTAssertEqual(buf.getNextStringUntilCRLF(), nil)
XCTAssertEqual(buf.getNextStringOfSize(1), "a")
buf.storeReceivedString("\n")
XCTAssertEqual(buf.getNextStringUntilCRLF(), "bcde")
XCTAssertEqual(buf.getNextStringOfSize(1), nil)
XCTAssertEqual(buf.getNextStringUntilCRLF(), nil)
}
func testRedisCommands()
{
let getCmd = RedisCommand.Get("aKey", handler: nil)
XCTAssertEqual(getCmd.getCommandString(), "*2\r\n$3\r\nGET\r\n$4\r\naKey\r\n".data(using: String.Encoding.utf8))
let authCmd = RedisCommand.Auth("12345", handler: nil)
XCTAssertEqual(authCmd.getCommandString(), "*2\r\n$4\r\nAUTH\r\n$5\r\n12345\r\n".data(using: String.Encoding.utf8))
let setCmd = RedisCommand.Set("aKey", valueToSet: "abc".data(using: String.Encoding.utf8)!, handler: nil)
XCTAssertEqual(setCmd.getCommandString(), "*3\r\n$3\r\nSET\r\n$4\r\naKey\r\n$3\r\nabc\r\n".data(using: String.Encoding.utf8))
let genericCmd = RedisCommand.Generic("SET", "mykey", "1", "EX", "3", handler: nil)
XCTAssertEqual(String(data: genericCmd.getCommandString()!, encoding: String.Encoding.utf8), "*5\r\n$3\r\nSET\r\n$5\r\nmykey\r\n$1\r\n1\r\n$2\r\nEX\r\n$1\r\n3\r\n")
}
}