1
- import ETCD
2
- import NIO
3
-
4
1
//===----------------------------------------------------------------------===//
5
2
//
6
3
// This source file is part of the swift-etcd-client-gsoc open source project
@@ -14,18 +11,22 @@ import NIO
14
11
// SPDX-License-Identifier: Apache-2.0
15
12
//
16
13
//===----------------------------------------------------------------------===//
14
+
15
+ import ETCD
16
+ import NIO
17
17
import XCTest
18
18
19
- final class EtcdClientTests : XCTestCase {
20
- var eventLoopGroup : EventLoopGroup !
21
- var etcdClient : EtcdClient !
19
+ final class IntegrationTests : XCTestCase {
20
+
21
+ private static let integrationTestEnabled = getBoolEnv ( " SWIFT_ETCD_CLIENT_INTEGRATION_TEST_ENABLED " ) ?? false
22
22
23
23
override func setUp( ) async throws {
24
- eventLoopGroup = MultiThreadedEventLoopGroup . singleton
25
- etcdClient = EtcdClient ( host: " localhost " , port: 2379 , eventLoopGroup: eventLoopGroup)
24
+ try XCTSkipUnless ( Self . integrationTestEnabled)
26
25
}
27
26
28
27
func testSetAndGetStringValue( ) async throws {
28
+ let etcdClient = EtcdClient . testClient
29
+
29
30
try await etcdClient. set ( " testKey " , value: " testValue " )
30
31
let key = " testKey " . data ( using: . utf8) !
31
32
let rangeRequest = RangeRequest ( key: key)
@@ -36,13 +37,17 @@ final class EtcdClientTests: XCTestCase {
36
37
}
37
38
38
39
func testGetNonExistentKey( ) async throws {
40
+ let etcdClient = EtcdClient . testClient
41
+
39
42
let key = " nonExistentKey " . data ( using: . utf8) !
40
43
let rangeRequest = RangeRequest ( key: key)
41
44
let result = try await etcdClient. getRange ( rangeRequest)
42
45
XCTAssertNil ( result)
43
46
}
44
47
45
48
func testDeleteKeyExists( ) async throws {
49
+ let etcdClient = EtcdClient . testClient
50
+
46
51
let key = " testKey "
47
52
let value = " testValue "
48
53
try await etcdClient. set ( key, value: value)
@@ -54,12 +59,14 @@ final class EtcdClientTests: XCTestCase {
54
59
55
60
let deleteRangeRequest = DeleteRangeRequest ( key: rangeRequestKey)
56
61
try await etcdClient. deleteRange ( deleteRangeRequest)
57
-
62
+
58
63
fetchedValue = try await etcdClient. getRange ( rangeRequest)
59
64
XCTAssertNil ( fetchedValue)
60
65
}
61
66
62
67
func testDeleteNonExistentKey( ) async throws {
68
+ let etcdClient = EtcdClient . testClient
69
+
63
70
let key = " testKey " . data ( using: . utf8) !
64
71
let rangeRequest = RangeRequest ( key: key)
65
72
@@ -68,12 +75,14 @@ final class EtcdClientTests: XCTestCase {
68
75
69
76
let deleteRangeRequest = DeleteRangeRequest ( key: key)
70
77
try await etcdClient. deleteRange ( deleteRangeRequest)
71
-
78
+
72
79
fetchedValue = try await etcdClient. getRange ( rangeRequest)
73
80
XCTAssertNil ( fetchedValue)
74
81
}
75
82
76
83
func testUpdateExistingKey( ) async throws {
84
+ let etcdClient = EtcdClient . testClient
85
+
77
86
let key = " testKey "
78
87
let value = " testValue "
79
88
try await etcdClient. set ( key, value: value)
@@ -95,14 +104,16 @@ final class EtcdClientTests: XCTestCase {
95
104
}
96
105
97
106
func testWatch( ) async throws {
107
+ let etcdClient = EtcdClient . testClient
108
+
98
109
let key = " testKey "
99
110
let value = " testValue " . data ( using: . utf8) !
100
111
101
112
try await etcdClient. put ( key, value: " foo " )
102
113
103
114
try await withThrowingTaskGroup ( of: Void . self) { group in
104
115
group. addTask {
105
- try await self . etcdClient. watch ( key) { watchAsyncSequence in
116
+ try await etcdClient. watch ( key) { watchAsyncSequence in
106
117
var iterator = watchAsyncSequence. makeAsyncIterator ( )
107
118
let events = try await iterator. next ( )
108
119
guard let events = events else {
@@ -122,8 +133,27 @@ final class EtcdClientTests: XCTestCase {
122
133
}
123
134
124
135
try await Task . sleep ( nanoseconds: 1_000_000_000 )
125
- try await self . etcdClient. put ( key, value: String ( data: value, encoding: . utf8) !)
136
+ try await etcdClient. put ( key, value: String ( data: value, encoding: . utf8) !)
126
137
group. cancelAll ( )
127
138
}
128
139
}
129
140
}
141
+
142
+ extension EtcdClient {
143
+ fileprivate static let testClient = EtcdClient (
144
+ host: ProcessInfo . processInfo. environment [ " ETCD_HOST " ] ?? " localhost " ,
145
+ port: getIntEnv ( " ETCD_PORT " ) ?? 2379 ,
146
+ eventLoopGroup: . singletonMultiThreadedEventLoopGroup
147
+ )
148
+ }
149
+
150
+ /// Returns true if `key` is a truthy string, otherwise returns false.
151
+ private func getBoolEnv( _ key: String ) -> Bool ? {
152
+ switch ProcessInfo . processInfo. environment [ key] ? . lowercased ( ) {
153
+ case . none: return nil
154
+ case " true " , " y " , " yes " , " on " , " 1 " : return true
155
+ default : return false
156
+ }
157
+ }
158
+
159
+ private func getIntEnv( _ key: String ) -> Int ? { ProcessInfo . processInfo. environment [ key] . flatMap ( Int . init ( _: ) ) }
0 commit comments