@@ -7,13 +7,13 @@ class ConnectionPoolTests : SQLiteTestCase {
7
7
super. setUp ( )
8
8
}
9
9
10
- func testConcurrentAccess ( ) {
10
+ func testConcurrentAccess2 ( ) {
11
11
12
12
let _ = try ? NSFileManager . defaultManager ( ) . removeItemAtPath ( " \( NSTemporaryDirectory ( ) ) /SQLite.swift Pool Tests.sqlite " )
13
13
let pool = try ! ConnectionPool ( . URI( " \( NSTemporaryDirectory ( ) ) /SQLite.swift Pool Tests.sqlite " ) )
14
14
15
15
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); " )
17
17
try ! conn. execute ( " DELETE FROM test " )
18
18
try ! conn. execute ( " INSERT INTO test(id,name) VALUES(0, 'test0') " )
19
19
try ! conn. execute ( " INSERT INTO test(id,name) VALUES(1, 'test1') " )
@@ -40,7 +40,7 @@ class ConnectionPoolTests : SQLiteTestCase {
40
40
41
41
let now = stmt. scalar ( x) as! String
42
42
if now != curr {
43
- print ( now)
43
+ // print(now)
44
44
curr = now
45
45
}
46
46
reads += 1
@@ -53,7 +53,7 @@ class ConnectionPoolTests : SQLiteTestCase {
53
53
54
54
}
55
55
56
- for x in 10 ..< 50000 {
56
+ for x in 10 ..< 5000 {
57
57
58
58
let name = " test " + String( x)
59
59
let idx = Int ( rand ( ) ) % 5
@@ -65,13 +65,55 @@ class ConnectionPoolTests : SQLiteTestCase {
65
65
XCTFail ( ( error as? CustomStringConvertible ) ? . description ?? " Unknown " )
66
66
}
67
67
68
- usleep ( 1500 )
68
+ usleep ( 500 )
69
69
}
70
70
71
71
quit = true
72
72
waitForExpectationsWithTimeout ( 1000 , handler: nil )
73
73
}
74
74
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
+
75
117
func testAutoRelease( ) {
76
118
77
119
let _ = try ? NSFileManager . defaultManager ( ) . removeItemAtPath ( " \( NSTemporaryDirectory ( ) ) /SQLite.swift Pool Tests.sqlite " )
0 commit comments