@@ -104,9 +104,9 @@ class KakapoDBTests: QuickSpec {
104
104
105
105
it ( " should insert a large number of elements " ) {
106
106
dispatch_apply ( 1000 , dispatch_queue_create ( " queue " , DISPATCH_QUEUE_CONCURRENT) , { _ in
107
- sut. insert ( { ( id) -> UserFactory in
107
+ sut. insert { ( id) -> UserFactory in
108
108
return UserFactory ( firstName: " Name " + String( id) , lastName: " Last Name " + String( id) , age: id, id: id)
109
- } )
109
+ }
110
110
} )
111
111
112
112
let userObjects = sut. findAll ( UserFactory . self)
@@ -142,9 +142,9 @@ class KakapoDBTests: QuickSpec {
142
142
}
143
143
144
144
it ( " shoud return the expected object after inserting it " ) {
145
- sut. insert ( { ( id) -> UserFactory in
145
+ sut. insert { ( id) -> UserFactory in
146
146
return UserFactory ( firstName: " Hector " , lastName: " Zarco " , age: 25 , id: id)
147
- } )
147
+ }
148
148
149
149
let user = sut. find ( UserFactory . self, id: 0 )
150
150
expect ( user? . firstName) . to ( match ( " Hector " ) )
@@ -153,9 +153,9 @@ class KakapoDBTests: QuickSpec {
153
153
}
154
154
155
155
it ( " should fail when inserting invalid id " ) {
156
- sut. insert ( { ( id) -> UserFactory in
156
+ sut. insert { ( id) -> UserFactory in
157
157
return UserFactory ( firstName: " Joan " , lastName: " Romano " , age: 25 , id: id)
158
- } )
158
+ }
159
159
160
160
// TODO: TEST THIS FATAL ERROR
161
161
// expect{ sut.insert({ (id) -> UserFactory in
@@ -164,9 +164,9 @@ class KakapoDBTests: QuickSpec {
164
164
}
165
165
166
166
it ( " should return the expected filtered element with valid id " ) {
167
- sut. insert ( { ( id) -> UserFactory in
167
+ sut. insert { ( id) -> UserFactory in
168
168
UserFactory ( firstName: " Hector " , lastName: " Zarco " , age: 25 , id: id)
169
- } )
169
+ }
170
170
171
171
let userArray = sut. filter ( UserFactory . self, includeElement: { ( item) -> Bool in
172
172
return item. id == 0
@@ -180,9 +180,9 @@ class KakapoDBTests: QuickSpec {
180
180
181
181
it ( " should return no objects for some inexisting filtering " ) {
182
182
sut. create ( UserFactory . self, number: 20 )
183
- sut. insert ( { ( id) -> UserFactory in
183
+ sut. insert { ( id) -> UserFactory in
184
184
return UserFactory ( firstName: " Hector " , lastName: " Zarco " , age: 25 , id: id)
185
- } )
185
+ }
186
186
187
187
let userArray = sut. filter ( UserFactory . self, includeElement: { ( item) -> Bool in
188
188
return item. lastName == " Manzella "
@@ -191,6 +191,94 @@ class KakapoDBTests: QuickSpec {
191
191
expect ( userArray. count) == 0
192
192
}
193
193
}
194
+
195
+ // 💀💀💀💀💀💀💀💀
196
+ describe ( " Database Operations Deadlock " ) {
197
+ let sut = KakapoDB ( )
198
+ let queue = dispatch_queue_create ( " com.kakapodb.testDeadlock " , DISPATCH_QUEUE_SERIAL)
199
+
200
+ it ( " should not deadlock when writing into database during a writing operation " ) {
201
+ let user = sut. insert { ( id) -> UserFactory in
202
+ sut. insert { ( id) -> UserFactory in
203
+ return UserFactory ( id: id, db: sut)
204
+ }
205
+
206
+ return UserFactory ( id: id, db: sut)
207
+ }
208
+
209
+ expect ( user) . toEventuallyNot ( beNil ( ) )
210
+ }
211
+
212
+ it ( " should not deadlock when synchronously writing from another queue into database during a writing operation " ) {
213
+ let user = sut. insert { ( id) -> UserFactory in
214
+ dispatch_sync ( queue) {
215
+ sut. insert { ( id) -> UserFactory in
216
+ return UserFactory ( id: id, db: sut)
217
+ }
218
+ }
219
+ return UserFactory ( id: id, db: sut)
220
+ }
221
+ expect ( user) . toEventuallyNot ( beNil ( ) )
222
+ }
223
+
224
+ it ( " should not deadlock when writing into database during a reading operation " ) {
225
+ let result = sut. filter ( UserFactory . self, includeElement: { ( _) -> Bool in
226
+ sut. create ( UserFactory)
227
+ return true
228
+ } )
229
+
230
+ expect ( result) . toEventuallyNot ( beNil ( ) )
231
+ }
232
+
233
+ it ( " should not deadlock when synchronously writing from another queue into database during a reading operation " ) {
234
+ let result = sut. filter ( UserFactory . self, includeElement: { ( _) -> Bool in
235
+ dispatch_sync ( queue) {
236
+ sut. create ( UserFactory)
237
+ }
238
+ return true
239
+ } )
240
+
241
+ expect ( result) . toEventuallyNot ( beNil ( ) )
242
+ }
243
+
244
+ it ( " should not deadlock when reading the database during a read operation " ) {
245
+ let result = sut. filter ( UserFactory . self, includeElement: { ( _) -> Bool in
246
+ sut. findAll ( UserFactory . self)
247
+ return true
248
+ } )
249
+
250
+ expect ( result) . toEventuallyNot ( beNil ( ) )
251
+ }
252
+
253
+ it ( " should not deadlock when synchronously reading the database from another queue during a reading operation " ) {
254
+ let result = sut. filter ( UserFactory . self, includeElement: { ( _) -> Bool in
255
+ dispatch_sync ( queue) {
256
+ sut. findAll ( UserFactory . self)
257
+ }
258
+ return true
259
+ } )
260
+
261
+ expect ( result) . toEventuallyNot ( beNil ( ) )
262
+ }
263
+
264
+ it ( " should not deadlock when reading the database during a write operation " ) {
265
+ let user = sut. insert { ( id) -> UserFactory in
266
+ sut. findAll ( UserFactory . self)
267
+ return UserFactory ( id: id, db: sut)
268
+ }
269
+ expect ( user) . toEventuallyNot ( beNil ( ) )
270
+ }
271
+
272
+ it ( " should not deadlock when synchronously reading the database from another queue during a write operation " ) {
273
+ let user = sut. insert { ( id) -> UserFactory in
274
+ dispatch_sync ( queue) {
275
+ sut. findAll ( UserFactory . self)
276
+ }
277
+ return UserFactory ( id: id, db: sut)
278
+ }
279
+ expect ( user) . toEventuallyNot ( beNil ( ) )
280
+ }
281
+ }
194
282
}
195
283
}
196
284
@@ -200,19 +288,19 @@ class KakapoDBPerformaceTests: XCTestCase {
200
288
201
289
func testMultipleSingleCreationPerformance( ) {
202
290
measureBlock {
203
- dispatch_apply ( 1000 , dispatch_get_global_queue ( DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0 ) , { _ in
291
+ dispatch_apply ( 1000 , dispatch_get_global_queue ( DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0 ) ) { _ in
204
292
self . sut. create ( UserFactory . self)
205
- } )
293
+ }
206
294
}
207
295
}
208
296
209
297
func testMultpleInsertions( ) {
210
298
measureBlock {
211
- dispatch_apply ( 1000 , dispatch_get_global_queue ( DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0 ) , { _ in
299
+ dispatch_apply ( 1000 , dispatch_get_global_queue ( DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0 ) ) { _ in
212
300
self . sut. insert { ( id) -> UserFactory in
213
301
return UserFactory ( id: id, db: self . sut)
214
302
}
215
- } )
303
+ }
216
304
}
217
305
}
218
306
}
0 commit comments