@@ -17,15 +17,9 @@ import NIO
17
17
import XCTest
18
18
19
19
final class EtcdClientTests : XCTestCase {
20
- var eventLoopGroup : EventLoopGroup !
21
- var etcdClient : EtcdClient !
22
-
23
- override func setUp( ) async throws {
24
- eventLoopGroup = MultiThreadedEventLoopGroup . singleton
25
- etcdClient = EtcdClient ( host: " localhost " , port: 2379 , eventLoopGroup: eventLoopGroup)
26
- }
27
-
28
20
func testSetAndGetStringValue( ) async throws {
21
+ let etcdClient = EtcdClient . testClient
22
+
29
23
try await etcdClient. set ( " testKey " , value: " testValue " )
30
24
let key = " testKey " . data ( using: . utf8) !
31
25
let rangeRequest = RangeRequest ( key: key)
@@ -36,13 +30,17 @@ final class EtcdClientTests: XCTestCase {
36
30
}
37
31
38
32
func testGetNonExistentKey( ) async throws {
33
+ let etcdClient = EtcdClient . testClient
34
+
39
35
let key = " nonExistentKey " . data ( using: . utf8) !
40
36
let rangeRequest = RangeRequest ( key: key)
41
37
let result = try await etcdClient. getRange ( rangeRequest)
42
38
XCTAssertNil ( result)
43
39
}
44
40
45
41
func testDeleteKeyExists( ) async throws {
42
+ let etcdClient = EtcdClient . testClient
43
+
46
44
let key = " testKey "
47
45
let value = " testValue "
48
46
try await etcdClient. set ( key, value: value)
@@ -60,6 +58,8 @@ final class EtcdClientTests: XCTestCase {
60
58
}
61
59
62
60
func testDeleteNonExistentKey( ) async throws {
61
+ let etcdClient = EtcdClient . testClient
62
+
63
63
let key = " testKey " . data ( using: . utf8) !
64
64
let rangeRequest = RangeRequest ( key: key)
65
65
@@ -74,6 +74,8 @@ final class EtcdClientTests: XCTestCase {
74
74
}
75
75
76
76
func testUpdateExistingKey( ) async throws {
77
+ let etcdClient = EtcdClient . testClient
78
+
77
79
let key = " testKey "
78
80
let value = " testValue "
79
81
try await etcdClient. set ( key, value: value)
@@ -95,14 +97,16 @@ final class EtcdClientTests: XCTestCase {
95
97
}
96
98
97
99
func testWatch( ) async throws {
100
+ let etcdClient = EtcdClient . testClient
101
+
98
102
let key = " testKey "
99
103
let value = " testValue " . data ( using: . utf8) !
100
104
101
105
try await etcdClient. put ( key, value: " foo " )
102
106
103
107
try await withThrowingTaskGroup ( of: Void . self) { group in
104
108
group. addTask {
105
- try await self . etcdClient. watch ( key) { watchAsyncSequence in
109
+ try await etcdClient. watch ( key) { watchAsyncSequence in
106
110
var iterator = watchAsyncSequence. makeAsyncIterator ( )
107
111
let events = try await iterator. next ( )
108
112
guard let events = events else {
@@ -122,8 +126,16 @@ final class EtcdClientTests: XCTestCase {
122
126
}
123
127
124
128
try await Task . sleep ( nanoseconds: 1_000_000_000 )
125
- try await self . etcdClient. put ( key, value: String ( data: value, encoding: . utf8) !)
129
+ try await etcdClient. put ( key, value: String ( data: value, encoding: . utf8) !)
126
130
group. cancelAll ( )
127
131
}
128
132
}
129
133
}
134
+
135
+ extension EtcdClient {
136
+ fileprivate static let testClient = EtcdClient (
137
+ host: " localhost " ,
138
+ port: 2379 ,
139
+ eventLoopGroup: . singletonMultiThreadedEventLoopGroup
140
+ )
141
+ }
0 commit comments