@@ -17,7 +17,7 @@ struct UserFactory: Storable, Serializable {
17
17
let age : Int
18
18
let id : Int
19
19
20
- init ( id: Int ) {
20
+ init ( id: Int , db : KakapoDB ) {
21
21
self . init ( firstName: randomString ( ) , lastName: randomString ( ) , age: random ( ) , id: id)
22
22
}
23
23
@@ -34,7 +34,7 @@ struct CommentFactory: Storable {
34
34
let likes : Int
35
35
let id : Int
36
36
37
- init ( id: Int ) {
37
+ init ( id: Int , db : KakapoDB ) {
38
38
self . init ( text: randomString ( ) , likes: random ( ) , id: id)
39
39
}
40
40
@@ -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,16 +191,115 @@ class KakapoDBTests: QuickSpec {
191
191
expect ( userArray. count) == 0
192
192
}
193
193
}
194
+
195
+ describe ( " Database Operations Deadlock 💀💀💀💀💀💀💀💀 " ) {
196
+ let sut = KakapoDB ( )
197
+ let queue = dispatch_queue_create ( " com.kakapodb.testDeadlock " , DISPATCH_QUEUE_SERIAL)
198
+
199
+ it ( " should not deadlock when writing into database during a writing operation " ) {
200
+ let user = sut. insert { ( id) -> UserFactory in
201
+ sut. insert { ( id) -> UserFactory in
202
+ return UserFactory ( id: id, db: sut)
203
+ }
204
+
205
+ return UserFactory ( id: id, db: sut)
206
+ }
207
+
208
+ expect ( user) . toEventuallyNot ( beNil ( ) )
209
+ }
210
+
211
+ it ( " should not deadlock when synchronously writing from another queue into database during a writing operation " ) {
212
+ let user = sut. insert { ( id) -> UserFactory in
213
+ dispatch_sync ( queue) {
214
+ sut. insert { ( id) -> UserFactory in
215
+ return UserFactory ( id: id, db: sut)
216
+ }
217
+ }
218
+ return UserFactory ( id: id, db: sut)
219
+ }
220
+ expect ( user) . toEventuallyNot ( beNil ( ) )
221
+ }
222
+
223
+ it ( " should not deadlock when writing into database during a reading operation " ) {
224
+ let result = sut. filter ( UserFactory . self, includeElement: { ( _) -> Bool in
225
+ sut. create ( UserFactory)
226
+ return true
227
+ } )
228
+
229
+ expect ( result) . toEventuallyNot ( beNil ( ) )
230
+ }
231
+
232
+ it ( " should not deadlock when synchronously writing from another queue into database during a reading operation " ) {
233
+ let result = sut. filter ( UserFactory . self, includeElement: { ( _) -> Bool in
234
+ dispatch_sync ( queue) {
235
+ sut. create ( UserFactory)
236
+ }
237
+ return true
238
+ } )
239
+
240
+ expect ( result) . toEventuallyNot ( beNil ( ) )
241
+ }
242
+
243
+ it ( " should not deadlock when reading the database during a read operation " ) {
244
+ let result = sut. filter ( UserFactory . self, includeElement: { ( _) -> Bool in
245
+ sut. findAll ( UserFactory . self)
246
+ return true
247
+ } )
248
+
249
+ expect ( result) . toEventuallyNot ( beNil ( ) )
250
+ }
251
+
252
+ it ( " should not deadlock when synchronously reading the database from another queue during a reading operation " ) {
253
+ let result = sut. filter ( UserFactory . self, includeElement: { ( _) -> Bool in
254
+ dispatch_sync ( queue) {
255
+ sut. findAll ( UserFactory . self)
256
+ }
257
+ return true
258
+ } )
259
+
260
+ expect ( result) . toEventuallyNot ( beNil ( ) )
261
+ }
262
+
263
+ it ( " should not deadlock when reading the database during a write operation " ) {
264
+ let user = sut. insert { ( id) -> UserFactory in
265
+ sut. findAll ( UserFactory . self)
266
+ return UserFactory ( id: id, db: sut)
267
+ }
268
+ expect ( user) . toEventuallyNot ( beNil ( ) )
269
+ }
270
+
271
+ it ( " should not deadlock when synchronously reading the database from another queue during a write operation " ) {
272
+ let user = sut. insert { ( id) -> UserFactory in
273
+ dispatch_sync ( queue) {
274
+ sut. findAll ( UserFactory . self)
275
+ }
276
+ return UserFactory ( id: id, db: sut)
277
+ }
278
+ expect ( user) . toEventuallyNot ( beNil ( ) )
279
+ }
280
+ }
194
281
}
195
282
}
196
283
197
284
class KakapoDBPerformaceTests : XCTestCase {
285
+
198
286
func testMultipleSingleCreationPerformance( ) {
199
287
let sut = KakapoDB ( )
200
288
measureBlock {
201
- dispatch_apply ( 1000 , dispatch_get_global_queue ( DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0 ) , { _ in
289
+ dispatch_apply ( 1000 , dispatch_get_global_queue ( DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0 ) ) { _ in
202
290
sut. create ( UserFactory . self)
203
- } )
291
+ }
292
+ }
293
+ }
294
+
295
+ func testMultpleInsertionsPerformance( ) {
296
+ let sut = KakapoDB ( )
297
+ measureBlock {
298
+ dispatch_apply ( 1000 , dispatch_get_global_queue ( DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0 ) ) { _ in
299
+ sut. insert { ( id) -> UserFactory in
300
+ return UserFactory ( id: id, db: sut)
301
+ }
302
+ }
204
303
}
205
304
}
206
305
}
0 commit comments