Skip to content

Commit 6225a6f

Browse files
tofutimoysteinkrog
authored andcommitted
Fix #244 StressAsync test
Passes by switching to WAL Needs lock in PreparedSqlLiteInsertCommand Use Task.Run instead of Task.Factory.StartNew to avoid dangling tasks from inner async delegate (not properly awaited)
1 parent 31f24c3 commit 6225a6f

File tree

1 file changed

+26
-19
lines changed

1 file changed

+26
-19
lines changed

tests/AsyncTests.cs

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -173,10 +173,10 @@ public async Task StressAsync()
173173
// see http://stackoverflow.com/questions/12004426/sqlite-returns-sqlite-busy-in-wal-mode
174174
var journalMode = await globalConn.ExecuteScalarAsync<string>("PRAGMA journal_mode = wal"); // = wal");
175175
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);
180180
var busyTimeout = await globalConn.ExecuteScalarAsync<string>(
181181
string.Format("PRAGMA busy_timeout = {0}", defaultBusyTimeout));
182182
Debug.WriteLine("busy_timeout: " + busyTimeout);
@@ -188,13 +188,15 @@ public async Task StressAsync()
188188
var tasks = new List<Task>();
189189
for (int i = 0; i < n; i++)
190190
{
191-
tasks.Add(Task.Factory.StartNew(async delegate
191+
int taskId = i;
192+
193+
tasks.Add(Task.Run(async () =>
192194
{
193-
string taskDesc = "";
195+
string taskStep = "";
194196

195197
try
196198
{
197-
taskDesc = "CONNECT";
199+
taskStep = "CONNECT";
198200
SQLiteAsyncConnection conn = GetAsyncConnection();
199201

200202
// each connection retains the global journal_mode but somehow resets busy_timeout to 100
@@ -204,10 +206,10 @@ public async Task StressAsync()
204206

205207
var obj = new Customer
206208
{
207-
FirstName = i.ToString(),
209+
FirstName = taskId.ToString(),
208210
};
209211

210-
taskDesc = "INSERT";
212+
taskStep = "INSERT";
211213
await conn.InsertAsync(obj);
212214

213215
if (obj.Id == 0)
@@ -218,7 +220,7 @@ public async Task StressAsync()
218220
}
219221
}
220222

221-
taskDesc = "SELECT";
223+
taskStep = "SELECT";
222224
var obj3 = await (from c in conn.Table<Customer>() where c.Id == obj.Id select c).ToListAsync();
223225
Customer obj2 = obj3.FirstOrDefault();
224226
if (obj2 == null)
@@ -228,30 +230,35 @@ public async Task StressAsync()
228230
errors.Add("Failed query");
229231
}
230232
}
233+
234+
// Debug.WriteLine("task {0} with id {1} and name {2}", taskId, obj.Id, obj.FirstName);
231235
}
232236
catch (Exception ex)
233237
{
234238
lock (errors)
235239
{
236-
errors.Add(string.Format("{0}: {1}", taskDesc, ex.Message));
240+
errors.Add(string.Format("{0}: {1}", taskStep, ex.Message));
237241
}
238242
}
239243
}));
240244
}
241245

242246
await Task.WhenAll(tasks);
247+
Assert.AreEqual(n, tasks.Where(t => t.IsCompleted).Count());
243248

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+
//}
249254

250-
Assert.AreEqual(0, errors.Count);
255+
Assert.AreEqual(0, errors.Count, "Error in task runs");
251256

252-
// could be locked
253257
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");
255262
}
256263

257264
[Test]

0 commit comments

Comments
 (0)