@@ -173,10 +173,10 @@ public async Task StressAsync()
173
173
// see http://stackoverflow.com/questions/12004426/sqlite-returns-sqlite-busy-in-wal-mode
174
174
var journalMode = await globalConn . ExecuteScalarAsync < string > ( "PRAGMA journal_mode = wal" ) ; // = wal");
175
175
Debug . WriteLine ( "journal_mode: " + journalMode ) ;
176
- var synchronous = await globalConn . ExecuteScalarAsync < string > ( "PRAGMA synchronous" ) ; // 2 = FULL
177
- Debug . WriteLine ( "synchronous: " + synchronous ) ;
178
- var pageSize = await globalConn . ExecuteScalarAsync < string > ( "PRAGMA page_size" ) ; // 1024 default
179
- Debug . WriteLine ( "page_size: " + pageSize ) ;
176
+ // var synchronous = await globalConn.ExecuteScalarAsync<string>("PRAGMA synchronous"); // 2 = FULL
177
+ // Debug.WriteLine("synchronous: " + synchronous);
178
+ // var pageSize = await globalConn.ExecuteScalarAsync<string>("PRAGMA page_size"); // 1024 default
179
+ // Debug.WriteLine("page_size: " + pageSize);
180
180
var busyTimeout = await globalConn . ExecuteScalarAsync < string > (
181
181
string . Format ( "PRAGMA busy_timeout = {0}" , defaultBusyTimeout ) ) ;
182
182
Debug . WriteLine ( "busy_timeout: " + busyTimeout ) ;
@@ -188,13 +188,15 @@ public async Task StressAsync()
188
188
var tasks = new List < Task > ( ) ;
189
189
for ( int i = 0 ; i < n ; i ++ )
190
190
{
191
- tasks . Add ( Task . Factory . StartNew ( async delegate
191
+ int taskId = i ;
192
+
193
+ tasks . Add ( Task . Run ( async ( ) =>
192
194
{
193
- string taskDesc = "" ;
195
+ string taskStep = "" ;
194
196
195
197
try
196
198
{
197
- taskDesc = "CONNECT" ;
199
+ taskStep = "CONNECT" ;
198
200
SQLiteAsyncConnection conn = GetAsyncConnection ( ) ;
199
201
200
202
// each connection retains the global journal_mode but somehow resets busy_timeout to 100
@@ -204,10 +206,10 @@ public async Task StressAsync()
204
206
205
207
var obj = new Customer
206
208
{
207
- FirstName = i . ToString ( ) ,
209
+ FirstName = taskId . ToString ( ) ,
208
210
} ;
209
211
210
- taskDesc = "INSERT" ;
212
+ taskStep = "INSERT" ;
211
213
await conn . InsertAsync ( obj ) ;
212
214
213
215
if ( obj . Id == 0 )
@@ -218,7 +220,7 @@ public async Task StressAsync()
218
220
}
219
221
}
220
222
221
- taskDesc = "SELECT" ;
223
+ taskStep = "SELECT" ;
222
224
var obj3 = await ( from c in conn . Table < Customer > ( ) where c . Id == obj . Id select c ) . ToListAsync ( ) ;
223
225
Customer obj2 = obj3 . FirstOrDefault ( ) ;
224
226
if ( obj2 == null )
@@ -228,30 +230,35 @@ public async Task StressAsync()
228
230
errors . Add ( "Failed query" ) ;
229
231
}
230
232
}
233
+
234
+ // Debug.WriteLine("task {0} with id {1} and name {2}", taskId, obj.Id, obj.FirstName);
231
235
}
232
236
catch ( Exception ex )
233
237
{
234
238
lock ( errors )
235
239
{
236
- errors . Add ( string . Format ( "{0}: {1}" , taskDesc , ex . Message ) ) ;
240
+ errors . Add ( string . Format ( "{0}: {1}" , taskStep , ex . Message ) ) ;
237
241
}
238
242
}
239
243
} ) ) ;
240
244
}
241
245
242
246
await Task . WhenAll ( tasks ) ;
247
+ Assert . AreEqual ( n , tasks . Where ( t => t . IsCompleted ) . Count ( ) ) ;
243
248
244
- int j = 0 ;
245
- foreach ( var error in errors )
246
- {
247
- Debug . WriteLine ( "{0} {1}" , j ++ , error ) ;
248
- }
249
+ // int j = 0;
250
+ // foreach (var error in errors)
251
+ // {
252
+ // Debug.WriteLine("{0} {1}", j++, error);
253
+ // }
249
254
250
- Assert . AreEqual ( 0 , errors . Count ) ;
255
+ Assert . AreEqual ( 0 , errors . Count , "Error in task runs" ) ;
251
256
252
- // could be locked
253
257
int count = await globalConn . Table < Customer > ( ) . CountAsync ( ) ;
254
- Assert . AreEqual ( n , count ) ;
258
+ Assert . AreEqual ( n , count , "Not enough items in table" ) ;
259
+
260
+ // TODO: get out of wal mode - currently fails with 'database is locked'
261
+ // journalMode = await globalConn.ExecuteScalarAsync<string>("PRAGMA journal_mode = delete");
255
262
}
256
263
257
264
[ Test ]
0 commit comments