@@ -42,99 +42,83 @@ class ConnectionPoolTestCase: BaseTestCase {
42
42
XCTFail ( " Execution should not reach this point " )
43
43
} catch let error as SQLiteError {
44
44
// Then
45
- XCTAssertEqual ( error. code, SQLITE_CANTOPEN, " error code should be `SQLITE_CANTOPEN` " )
45
+ XCTAssertEqual ( error. code, SQLITE_CANTOPEN)
46
46
} catch {
47
47
XCTFail ( " Failed with an unknown error type: \( error) " )
48
48
}
49
49
}
50
50
51
- func testThatDequeueingConnectionCreatesNewConnection( ) {
52
- do {
53
- // Given
54
- let pool = ConnectionPool ( storageLocation: storageLocation)
55
- pool. availableConnections. removeAll ( )
51
+ func testThatDequeueingConnectionCreatesNewConnection( ) throws {
52
+ // Given
53
+ let pool = ConnectionPool ( storageLocation: storageLocation)
54
+ pool. availableConnections. removeAll ( )
56
55
57
- // When
58
- let connection = try pool. dequeueConnectionForUse ( )
56
+ // When
57
+ let connection = try pool. dequeueConnectionForUse ( )
59
58
60
- // Then
61
- XCTAssertFalse ( pool. availableConnections. contains ( connection) , " available connections should not contain connection " )
62
- XCTAssertTrue ( pool. busyConnections. contains ( connection) , " busy connections should contain connection " )
63
- } catch {
64
- XCTFail ( " Test encountered unexpected error: \( error) " )
65
- }
59
+ // Then
60
+ XCTAssertFalse ( pool. availableConnections. contains ( connection) )
61
+ XCTAssertTrue ( pool. busyConnections. contains ( connection) )
66
62
}
67
63
68
- func testThatDequeueingConnectionCreatesNewConnectionAndExecutesPreparationClosure( ) {
69
- do {
70
- // Given
71
- let pool = ConnectionPool (
72
- storageLocation: storageLocation,
73
- connectionPreparation: { connection in
74
- try connection. execute ( " PRAGMA synchronous = 1 " )
75
- }
76
- )
64
+ func testThatDequeueingConnectionCreatesNewConnectionAndExecutesPreparationClosure( ) throws {
65
+ // Given
66
+ let pool = ConnectionPool (
67
+ storageLocation: storageLocation,
68
+ connectionPreparation: { connection in
69
+ try connection. execute ( " PRAGMA synchronous = 1 " )
70
+ }
71
+ )
77
72
78
- pool. availableConnections. removeAll ( )
73
+ pool. availableConnections. removeAll ( )
79
74
80
- var synchronous : Int ?
75
+ var synchronous : Int ?
81
76
82
- // When
83
- let connection = try pool. dequeueConnectionForUse ( )
84
-
85
- try pool. execute { connection in
86
- synchronous = try connection. query ( " PRAGMA synchronous " )
87
- }
77
+ // When
78
+ let connection = try pool. dequeueConnectionForUse ( )
88
79
89
- // Then
90
- XCTAssertFalse ( pool. availableConnections. contains ( connection) )
91
- XCTAssertTrue ( pool. busyConnections. contains ( connection) )
92
- XCTAssertEqual ( synchronous, 1 )
93
- } catch {
94
- XCTFail ( " Test encountered unexpected error: \( error) " )
80
+ try pool. execute { connection in
81
+ synchronous = try connection. query ( " PRAGMA synchronous " )
95
82
}
83
+
84
+ // Then
85
+ XCTAssertFalse ( pool. availableConnections. contains ( connection) )
86
+ XCTAssertTrue ( pool. busyConnections. contains ( connection) )
87
+ XCTAssertEqual ( synchronous, 1 )
96
88
}
97
89
98
- func testThatEnqueueConnectionRemovesBusyConnectionAndInsertsAvailableConnection( ) {
99
- do {
100
- // Given
101
- let pool = ConnectionPool ( storageLocation: storageLocation)
102
- let connection = try pool. dequeueConnectionForUse ( )
90
+ func testThatEnqueueConnectionRemovesBusyConnectionAndInsertsAvailableConnection( ) throws {
91
+ // Given
92
+ let pool = ConnectionPool ( storageLocation: storageLocation)
93
+ let connection = try pool. dequeueConnectionForUse ( )
103
94
104
- let beforeEnqueueAvailableConnections = pool. availableConnections
105
- let beforeEnqueueBusyConnections = pool. busyConnections
95
+ let beforeEnqueueAvailableConnections = pool. availableConnections
96
+ let beforeEnqueueBusyConnections = pool. busyConnections
106
97
107
- // When
108
- pool. enqueueConnectionForReuse ( connection)
98
+ // When
99
+ pool. enqueueConnectionForReuse ( connection)
109
100
110
- // Then
111
- XCTAssertFalse ( beforeEnqueueAvailableConnections. contains ( connection) , " available connections should not contain connection before enqueue " )
112
- XCTAssertTrue ( beforeEnqueueBusyConnections. contains ( connection) , " busy connections should contain connection before enqueue " )
101
+ // Then
102
+ XCTAssertFalse ( beforeEnqueueAvailableConnections. contains ( connection) )
103
+ XCTAssertTrue ( beforeEnqueueBusyConnections. contains ( connection) )
113
104
114
- XCTAssertTrue ( pool. availableConnections. contains ( connection) , " available connections should contain connection after enqueue " )
115
- XCTAssertFalse ( pool. busyConnections. contains ( connection) , " busy connections should not contain connection after enqueue " )
116
- } catch {
117
- XCTFail ( " Test encountered unexpected error: \( error) " )
118
- }
105
+ XCTAssertTrue ( pool. availableConnections. contains ( connection) )
106
+ XCTAssertFalse ( pool. busyConnections. contains ( connection) )
119
107
}
120
108
121
- func testThatConnectionPoolCanExecuteReadOnlyClosure( ) {
122
- do {
123
- // Given
124
- let pool = ConnectionPool ( storageLocation: storageLocation)
125
-
126
- var count : Int ?
109
+ func testThatConnectionPoolCanExecuteReadOnlyClosure( ) throws {
110
+ // Given
111
+ let pool = ConnectionPool ( storageLocation: storageLocation)
127
112
128
- // When
129
- try pool. execute { connection in
130
- count = try connection. query ( " SELECT count(*) FROM sqlite_master where type = 'table' " )
131
- }
113
+ var count : Int ?
132
114
133
- // Then
134
- XCTAssertEqual ( count, 0 )
135
- } catch {
136
- XCTFail ( " Test encountered unexpected error: \( error) " )
115
+ // When
116
+ try pool. execute { connection in
117
+ count = try connection. query ( " SELECT count(*) FROM sqlite_master where type = 'table' " )
137
118
}
119
+
120
+ // Then
121
+ XCTAssertEqual ( count, 0 )
138
122
}
139
123
140
124
func testThatConnectionPoolFailsToExecuteWriteClosure( ) {
@@ -150,99 +134,91 @@ class ConnectionPoolTestCase: BaseTestCase {
150
134
XCTFail ( " Execution should not reach this point " )
151
135
} catch let error as SQLiteError {
152
136
// Then
153
- XCTAssertEqual ( error. code, SQLITE_READONLY, " error code should equal SQLITE_READONLY " )
137
+ XCTAssertEqual ( error. code, SQLITE_READONLY)
154
138
} catch {
155
139
XCTFail ( " Failed with an unknown error type: \( error) " )
156
140
}
157
141
}
158
142
159
- func testThatConnectionPoolCanExecuteMultipleClosuresInParallel( ) {
160
- do {
161
- // Given
162
- let connection = try Connection ( storageLocation: storageLocation)
163
- try TestTables . createAndPopulateAgentsTable ( using: connection)
164
- let pool = ConnectionPool ( storageLocation: storageLocation)
165
-
166
- let range = 0 ..< 128
167
- let expectations : [ XCTestExpectation ] = range. map { expectation ( description: " read: \( $0) " ) }
168
- var counts : [ Int ] = [ ]
169
-
170
- let queue = DispatchQueue ( label: " test_serial_queue " )
171
- let utilityQueue = DispatchQueue . global ( qos: . utility)
172
-
173
- // When
174
- range. forEach { index in
175
- utilityQueue. async {
176
- do {
177
- try pool. execute { connection in
178
- guard let count: Int = try connection. query ( " SELECT count(*) FROM agents " ) else { return }
179
- queue. sync { counts. append ( count) }
180
- }
181
- } catch {
182
- // No-op
143
+ func testThatConnectionPoolCanExecuteMultipleClosuresInParallel( ) throws {
144
+ // Given
145
+ let connection = try Connection ( storageLocation: storageLocation)
146
+ try TestTables . createAndPopulateAgentsTable ( using: connection)
147
+ let pool = ConnectionPool ( storageLocation: storageLocation)
148
+
149
+ let range = 0 ..< 128
150
+ let expectations : [ XCTestExpectation ] = range. map { expectation ( description: " read: \( $0) " ) }
151
+ var counts : [ Int ] = [ ]
152
+
153
+ let queue = DispatchQueue ( label: " test_serial_queue " )
154
+ let utilityQueue = DispatchQueue . global ( qos: . utility)
155
+
156
+ // When
157
+ range. forEach { index in
158
+ utilityQueue. async {
159
+ do {
160
+ try pool. execute { connection in
161
+ guard let count: Int = try connection. query ( " SELECT count(*) FROM agents " ) else { return }
162
+ queue. sync { counts. append ( count) }
183
163
}
184
-
185
- expectations [ index ] . fulfill ( )
164
+ } catch {
165
+ // No-op
186
166
}
187
- }
188
-
189
- waitForExpectations ( timeout: 10.0 , handler: nil )
190
167
191
- // Then
192
- XCTAssertEqual ( counts. count, range. count)
193
-
194
- for count in counts {
195
- XCTAssertEqual ( count, 2 )
168
+ expectations [ index] . fulfill ( )
196
169
}
197
- } catch {
198
- XCTFail ( " Test encountered unexpected error: \( error) " )
199
170
}
200
- }
201
171
202
- func testThatConnectionPoolDrainDelayWorksAsExpected( ) {
203
- do {
204
- // Given
205
- let connection = try Connection ( storageLocation: storageLocation)
206
- try TestTables . createAndPopulateAgentsTable ( using: connection)
207
- let pool = ConnectionPool ( storageLocation: storageLocation, availableConnectionDrainDelay: 0.1 )
172
+ waitForExpectations ( timeout: 10.0 , handler: nil )
208
173
209
- let range = 0 ..< 10
210
- let expectations : [ XCTestExpectation ] = range. map { expectation ( description: " read: \( $0) " ) }
211
- var counts : [ Int ] = [ ]
174
+ // Then
175
+ XCTAssertEqual ( counts. count, range. count)
212
176
213
- let queue = DispatchQueue ( label: " test_serial_queue " )
214
- let utilityQueue = DispatchQueue . global ( qos: . utility)
177
+ for count in counts {
178
+ XCTAssertEqual ( count, 2 )
179
+ }
180
+ }
215
181
216
- // When
217
- range. forEach { index in
218
- utilityQueue. async {
219
- do {
220
- try pool. execute { connection in
221
- guard let count: Int = try connection. query ( " SELECT count(*) FROM agents " ) else { return }
222
- queue. sync { counts. append ( count) }
223
- }
224
- } catch {
225
- // No-op
182
+ func testThatConnectionPoolDrainDelayWorksAsExpected( ) throws {
183
+ // Given
184
+ let connection = try Connection ( storageLocation: storageLocation)
185
+ try TestTables . createAndPopulateAgentsTable ( using: connection)
186
+ let pool = ConnectionPool ( storageLocation: storageLocation, availableConnectionDrainDelay: 0.1 )
187
+
188
+ let range = 0 ..< 10
189
+ let expectations : [ XCTestExpectation ] = range. map { expectation ( description: " read: \( $0) " ) }
190
+ var counts : [ Int ] = [ ]
191
+
192
+ let queue = DispatchQueue ( label: " test_serial_queue " )
193
+ let utilityQueue = DispatchQueue . global ( qos: . utility)
194
+
195
+ // When
196
+ range. forEach { index in
197
+ utilityQueue. async {
198
+ do {
199
+ try pool. execute { connection in
200
+ guard let count: Int = try connection. query ( " SELECT count(*) FROM agents " ) else { return }
201
+ queue. sync { counts. append ( count) }
226
202
}
227
-
228
- expectations [ index ] . fulfill ( )
203
+ } catch {
204
+ // No-op
229
205
}
206
+
207
+ expectations [ index] . fulfill ( )
230
208
}
209
+ }
231
210
232
- let drainExpectation = expectation ( description: " drain timer should drain available connections " )
233
- DispatchQueue . main. asyncAfter ( seconds: 0.2 ) { drainExpectation. fulfill ( ) }
211
+ let drainExpectation = expectation ( description: " drain timer should drain available connections " )
212
+ DispatchQueue . main. asyncAfter ( seconds: 0.2 ) { drainExpectation. fulfill ( ) }
234
213
235
- waitForExpectations ( timeout: 10.0 , handler: nil )
214
+ waitForExpectations ( timeout: 10.0 , handler: nil )
236
215
237
- // Then
238
- XCTAssertEqual ( pool. availableConnections. count, 1 , " available connections count should be back down to 1 " )
239
- XCTAssertEqual ( counts. count, range. count, " counts array should have equal number of items as range " )
216
+ // Then
217
+ XCTAssertEqual ( pool. availableConnections. count, 1 , " available connections count should be back down to 1 " )
218
+ XCTAssertEqual ( counts. count, range. count, " counts array should have equal number of items as range " )
240
219
241
- for (index, count) in counts. enumerated ( ) {
242
- XCTAssertEqual ( count, 2 , " count should be equal to 2 at index: \( index) " )
243
- }
244
- } catch {
245
- XCTFail ( " Test encountered unexpected error: \( error) " )
220
+ for (index, count) in counts. enumerated ( ) {
221
+ XCTAssertEqual ( count, 2 , " count should be equal to 2 at index: \( index) " )
246
222
}
247
223
}
248
224
}
0 commit comments