Skip to content

Commit ccc20e5

Browse files
committed
Added alternate concurrency test
1 parent 66b82b0 commit ccc20e5

File tree

1 file changed

+47
-5
lines changed

1 file changed

+47
-5
lines changed

SQLiteTests/ConnectionPoolTests.swift

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ class ConnectionPoolTests : SQLiteTestCase {
77
super.setUp()
88
}
99

10-
func testConcurrentAccess() {
10+
func testConcurrentAccess2() {
1111

1212
let _ = try? NSFileManager.defaultManager().removeItemAtPath("\(NSTemporaryDirectory())/SQLite.swift Pool Tests.sqlite")
1313
let pool = try! ConnectionPool(.URI("\(NSTemporaryDirectory())/SQLite.swift Pool Tests.sqlite"))
1414

1515
let conn = pool.writable
16-
try! conn.execute("CREATE TABLE IF NOT EXISTS test(id INTEGER PRIMARY KEY, name TEXT)")
16+
try! conn.execute("DROP TABLE IF EXISTS test; CREATE TABLE test(id INTEGER PRIMARY KEY, name TEXT);")
1717
try! conn.execute("DELETE FROM test")
1818
try! conn.execute("INSERT INTO test(id,name) VALUES(0, 'test0')")
1919
try! conn.execute("INSERT INTO test(id,name) VALUES(1, 'test1')")
@@ -40,7 +40,7 @@ class ConnectionPoolTests : SQLiteTestCase {
4040

4141
let now = stmt.scalar(x) as! String
4242
if now != curr {
43-
print(now)
43+
//print(now)
4444
curr = now
4545
}
4646
reads += 1
@@ -53,7 +53,7 @@ class ConnectionPoolTests : SQLiteTestCase {
5353

5454
}
5555

56-
for x in 10..<50000 {
56+
for x in 10..<5000 {
5757

5858
let name = "test" + String(x)
5959
let idx = Int(rand()) % 5
@@ -65,13 +65,55 @@ class ConnectionPoolTests : SQLiteTestCase {
6565
XCTFail((error as? CustomStringConvertible)?.description ?? "Unknown")
6666
}
6767

68-
usleep(1500)
68+
usleep(500)
6969
}
7070

7171
quit = true
7272
waitForExpectationsWithTimeout(1000, handler: nil)
7373
}
7474

75+
func testConcurrentAccess() throws {
76+
77+
let _ = try? NSFileManager.defaultManager().removeItemAtPath("\(NSTemporaryDirectory())/SQLite.swift Pool Tests.sqlite")
78+
let pool = try! ConnectionPool(.URI("\(NSTemporaryDirectory())/SQLite.swift Pool Tests.sqlite"))
79+
80+
try! pool.writable.execute("DROP TABLE IF EXISTS test; CREATE TABLE test(value);")
81+
try! pool.writable.run("INSERT INTO test(value) VALUES(?)", 0)
82+
83+
let q = dispatch_queue_create("Readers/Writers", DISPATCH_QUEUE_CONCURRENT);
84+
var finished = false
85+
86+
for _ in 0..<5 {
87+
88+
dispatch_async(q) {
89+
90+
while !finished {
91+
92+
let val = pool.readable.scalar("SELECT value FROM test")
93+
assert(val != nil, "DB query returned nil result set")
94+
95+
}
96+
97+
}
98+
99+
}
100+
101+
for c in 0..<5000 {
102+
103+
try pool.writable.run("INSERT INTO test(value) VALUES(?)", c)
104+
105+
usleep(100);
106+
107+
}
108+
109+
finished = true
110+
111+
// Wait for readers to finish
112+
dispatch_barrier_sync(q) {
113+
}
114+
115+
}
116+
75117
func testAutoRelease() {
76118

77119
let _ = try? NSFileManager.defaultManager().removeItemAtPath("\(NSTemporaryDirectory())/SQLite.swift Pool Tests.sqlite")

0 commit comments

Comments
 (0)