-
-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathRedisDatabaseTests.swift
More file actions
112 lines (89 loc) · 3.73 KB
/
Copy pathRedisDatabaseTests.swift
File metadata and controls
112 lines (89 loc) · 3.73 KB
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
import NIO
import Dispatch
@testable import Redis
import XCTest
private extension RedisClientConfig {
static func makeTest() -> RedisClientConfig {
var config = RedisClientConfig()
config.password = Environment.get("REDIS_PASSWORD")
return config
}
}
class RedisDatabaseTests: XCTestCase {
let defaultTimeout = 2.0
func testConnection() throws {
let group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
let config = RedisClientConfig.makeTest()
let database = try RedisDatabase(config: config)
let redis = try database.newConnection(on: group).wait()
defer { redis.close() }
try redis.set("hello", to: "world").wait()
let get = try redis.get("hello", as: String.self).wait()
XCTAssertEqual(get, "world")
try redis.delete("hello").wait()
XCTAssertNil(try redis.get("hello", as: String.self).wait())
}
func testDroppedConnection() throws {
let group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
let config = RedisClientConfig.makeTest()
let database = try RedisDatabase(config: config)
let redis = try database.newConnection(on: group).wait()
defer { redis.close() }
let timeout: UInt32 = 1
var dataReceived = false
var errorReceived = false
let command = RedisData.array(["brpop", "hello", "\(timeout)"].map { RedisData(bulk: $0) })
_ = redis.send(command).do { data in
dataReceived = true
}.catch { error in
errorReceived = true
}
// Close the connection
redis.close()
// Sleep for an extra second seconds to give the transaction time to complete
sleep(timeout+1)
XCTAssertEqual(dataReceived, false)
XCTAssertEqual(errorReceived, true)
}
func testSelect() throws {
let group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
let config = RedisClientConfig.makeTest()
let database = try RedisDatabase(config: config)
let redis = try database.newConnection(on: group).wait()
defer { redis.close() }
_ = try redis.select(2).wait()
try redis.set("hello", to: "world").wait()
let get = try redis.get("hello", as: String.self).wait()
XCTAssertEqual(get, "world")
_ = try redis.select(0).wait()
XCTAssertNil(try redis.get("hello", as: String.self).wait())
_ = try redis.select(2).wait()
let reget = try redis.get("hello", as: String.self).wait()
XCTAssertEqual(reget, "world")
try redis.delete("hello").wait()
XCTAssertNil(try redis.get("hello", as: String.self).wait())
}
func testSelectViaConfig() throws {
let group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
var config = RedisClientConfig.makeTest()
config.database = 2
let database = try RedisDatabase(config: config)
let redis = try database.newConnection(on: group).wait()
defer { redis.close() }
try redis.set("hello", to: "world").wait()
let get = try redis.get("hello", as: String.self).wait()
XCTAssertEqual(get, "world")
_ = try redis.select(0).wait()
XCTAssertNil(try redis.get("hello", as: String.self).wait())
_ = try redis.select(2).wait()
let reget = try redis.get("hello", as: String.self).wait()
XCTAssertEqual(reget, "world")
try redis.delete("hello").wait()
XCTAssertNil(try redis.get("hello", as: String.self).wait())
}
static let allTests = [
("testConnection", testConnection),
("testSelect", testSelect),
("testSelectViaConfig", testSelectViaConfig),
]
}