Skip to content

Commit 7a2996c

Browse files
committed
Merge pull request #25 from devlucky/feature/relantionshipInit
Feature/relantionship init
2 parents 9ef798f + ad87aea commit 7a2996c

File tree

5 files changed

+259
-54
lines changed

5 files changed

+259
-54
lines changed

KakapoExample/KakapoExample.xcodeproj/xcshareddata/xcbaselines/DE9352951CAAF21300AD534C.xcbaseline/050470C2-82B5-4F27-AA37-056529EACDE9.plist

+12-2
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,19 @@
2121
<key>com.apple.XCTPerformanceMetric_WallClockTime</key>
2222
<dict>
2323
<key>baselineAverage</key>
24-
<real>0.32172</real>
24+
<real>0.26126</real>
2525
<key>baselineIntegrationDisplayName</key>
26-
<string>09 apr 2016, 17:24:03</string>
26+
<string>14 apr 2016, 23:17:28</string>
27+
</dict>
28+
</dict>
29+
<key>testMultpleInsertions()</key>
30+
<dict>
31+
<key>com.apple.XCTPerformanceMetric_WallClockTime</key>
32+
<dict>
33+
<key>baselineAverage</key>
34+
<real>0.2452</real>
35+
<key>baselineIntegrationDisplayName</key>
36+
<string>14 apr 2016, 23:17:28</string>
2737
</dict>
2838
</dict>
2939
</dict>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>classNames</key>
6+
<dict>
7+
<key>KakapoDBPerformaceTests</key>
8+
<dict>
9+
<key>testMultipleSingleCreationPerformance()</key>
10+
<dict>
11+
<key>com.apple.XCTPerformanceMetric_WallClockTime</key>
12+
<dict>
13+
<key>baselineAverage</key>
14+
<real>0.076964</real>
15+
<key>baselineIntegrationDisplayName</key>
16+
<string>15 apr 2016, 17:50:14</string>
17+
</dict>
18+
</dict>
19+
<key>testMultpleInsertions()</key>
20+
<dict>
21+
<key>com.apple.XCTPerformanceMetric_WallClockTime</key>
22+
<dict>
23+
<key>baselineAverage</key>
24+
<real>0.076157</real>
25+
<key>baselineIntegrationDisplayName</key>
26+
<string>15 apr 2016, 17:35:42</string>
27+
</dict>
28+
</dict>
29+
<key>testMultpleInsertionsPerformance()</key>
30+
<dict>
31+
<key>com.apple.XCTPerformanceMetric_WallClockTime</key>
32+
<dict>
33+
<key>baselineAverage</key>
34+
<real>0.068985</real>
35+
<key>baselineIntegrationDisplayName</key>
36+
<string>15 apr 2016, 17:48:35</string>
37+
</dict>
38+
</dict>
39+
</dict>
40+
</dict>
41+
</dict>
42+
</plist>

KakapoExample/KakapoExample.xcodeproj/xcshareddata/xcbaselines/DE9352951CAAF21300AD534C.xcbaseline/Info.plist

+31
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,37 @@
3535
<string>com.apple.platform.iphonesimulator</string>
3636
</dict>
3737
</dict>
38+
<key>995E9444-5013-4083-9883-E2B5947553ED</key>
39+
<dict>
40+
<key>localComputer</key>
41+
<dict>
42+
<key>busSpeedInMHz</key>
43+
<integer>100</integer>
44+
<key>cpuCount</key>
45+
<integer>1</integer>
46+
<key>cpuKind</key>
47+
<string>Intel Core i7</string>
48+
<key>cpuSpeedInMHz</key>
49+
<integer>2500</integer>
50+
<key>logicalCPUCoresPerPackage</key>
51+
<integer>8</integer>
52+
<key>modelCode</key>
53+
<string>MacBookPro11,4</string>
54+
<key>physicalCPUCoresPerPackage</key>
55+
<integer>4</integer>
56+
<key>platformIdentifier</key>
57+
<string>com.apple.platform.macosx</string>
58+
</dict>
59+
<key>targetArchitecture</key>
60+
<string>x86_64</string>
61+
<key>targetDevice</key>
62+
<dict>
63+
<key>modelCode</key>
64+
<string>iPhone8,2</string>
65+
<key>platformIdentifier</key>
66+
<string>com.apple.platform.iphonesimulator</string>
67+
</dict>
68+
</dict>
3869
</dict>
3970
</dict>
4071
</plist>

KakapoExample/KakapoTests/KakapoDBTests.swift

+113-14
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ struct UserFactory: Storable, Serializable {
1717
let age: Int
1818
let id: Int
1919

20-
init(id: Int) {
20+
init(id: Int, db: KakapoDB) {
2121
self.init(firstName: randomString(), lastName: randomString(), age: random(), id: id)
2222
}
2323

@@ -34,7 +34,7 @@ struct CommentFactory: Storable {
3434
let likes: Int
3535
let id: Int
3636

37-
init(id: Int) {
37+
init(id: Int, db: KakapoDB) {
3838
self.init(text: randomString(), likes: random(), id: id)
3939
}
4040

@@ -104,9 +104,9 @@ class KakapoDBTests: QuickSpec {
104104

105105
it("should insert a large number of elements") {
106106
dispatch_apply(1000, dispatch_queue_create("queue", DISPATCH_QUEUE_CONCURRENT), { _ in
107-
sut.insert({ (id) -> UserFactory in
107+
sut.insert { (id) -> UserFactory in
108108
return UserFactory(firstName: "Name " + String(id), lastName: "Last Name " + String(id), age: id, id: id)
109-
})
109+
}
110110
})
111111

112112
let userObjects = sut.findAll(UserFactory.self)
@@ -142,9 +142,9 @@ class KakapoDBTests: QuickSpec {
142142
}
143143

144144
it("shoud return the expected object after inserting it") {
145-
sut.insert({ (id) -> UserFactory in
145+
sut.insert { (id) -> UserFactory in
146146
return UserFactory(firstName: "Hector", lastName: "Zarco", age:25, id: id)
147-
})
147+
}
148148

149149
let user = sut.find(UserFactory.self, id: 0)
150150
expect(user?.firstName).to(match("Hector"))
@@ -153,9 +153,9 @@ class KakapoDBTests: QuickSpec {
153153
}
154154

155155
it("should fail when inserting invalid id") {
156-
sut.insert({ (id) -> UserFactory in
156+
sut.insert { (id) -> UserFactory in
157157
return UserFactory(firstName: "Joan", lastName: "Romano", age:25, id: id)
158-
})
158+
}
159159

160160
// TODO: TEST THIS FATAL ERROR
161161
// expect{ sut.insert({ (id) -> UserFactory in
@@ -164,9 +164,9 @@ class KakapoDBTests: QuickSpec {
164164
}
165165

166166
it("should return the expected filtered element with valid id") {
167-
sut.insert({ (id) -> UserFactory in
167+
sut.insert { (id) -> UserFactory in
168168
UserFactory(firstName: "Hector", lastName: "Zarco", age:25, id: id)
169-
})
169+
}
170170

171171
let userArray = sut.filter(UserFactory.self, includeElement: { (item) -> Bool in
172172
return item.id == 0
@@ -180,9 +180,9 @@ class KakapoDBTests: QuickSpec {
180180

181181
it("should return no objects for some inexisting filtering") {
182182
sut.create(UserFactory.self, number: 20)
183-
sut.insert({ (id) -> UserFactory in
183+
sut.insert { (id) -> UserFactory in
184184
return UserFactory(firstName: "Hector", lastName: "Zarco", age:25, id: id)
185-
})
185+
}
186186

187187
let userArray = sut.filter(UserFactory.self, includeElement: { (item) -> Bool in
188188
return item.lastName == "Manzella"
@@ -191,16 +191,115 @@ class KakapoDBTests: QuickSpec {
191191
expect(userArray.count) == 0
192192
}
193193
}
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+
}
194281
}
195282
}
196283

197284
class KakapoDBPerformaceTests: XCTestCase {
285+
198286
func testMultipleSingleCreationPerformance() {
199287
let sut = KakapoDB()
200288
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
202290
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+
}
204303
}
205304
}
206305
}

0 commit comments

Comments
 (0)