Add IoTestsAsync
MODIFY
WalletWasabi.Tests/StoreTests.cs
WalletWasabi.Tests/StoreTests.cs
@@ -321,5 +321,92 @@ namespace WalletWasabi.Tests
ioman1.DeleteMe();
}
+
+ [Fact]
+ public async Task IoTestsAsync()
+ {
+ var file1 = Path.Combine(Global.DataDir, nameof(IoTestsAsync), $"file.dat");
+
+ IoManager ioman1 = new IoManager(file1);
+ ioman1.DeleteMe();
+ await ioman1.WriteAllLinesAsync(new string[0]);
+
+ string RandomString()
+ {
+ StringBuilder builder = new StringBuilder();
+ var rnd = new Random();
+ char ch;
+ for (int i = 0; i < rnd.Next(10, 100); i++)
+ {
+ ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * rnd.NextDouble() + 65)));
+ builder.Append(ch);
+ }
+ return builder.ToString();
+ };
+
+ var list = new List<string>();
+ async Task WriteNextLineAsync()
+ {
+ var next = RandomString();
+ lock (list)
+ {
+ list.Add(next);
+ }
+ using (await ioman1.Mutex.LockAsync())
+ {
+ var lines = (await ioman1.ReadAllLinesAsync()).ToList();
+ lines.Add(next);
+ await ioman1.WriteAllLinesAsync(lines);
+ }
+ };
+
+ var t1 = new Thread(() =>
+ {
+ for (var i = 0; i < 500; i++)
+ {
+ /* We have to block the Thread.
+ * If we use async/await pattern then Join() function at the end will indicate that the Thread is finished -
+ * which is not true bacause the WriteNextLineAsync() is not yet finished. The reason is that await will return execution
+ * the to the calling thread it is detected as the thread is done. t1 and t2 and t3 will still run in parallel!
+ */
+ WriteNextLineAsync().Wait();
+ }
+ });
+ var t2 = new Thread(() =>
+ {
+ for (var i = 0; i < 500; i++)
+ {
+ WriteNextLineAsync().Wait();
+ }
+ });
+ var t3 = new Thread(() =>
+ {
+ for (var i = 0; i < 500; i++)
+ {
+ WriteNextLineAsync().Wait();
+ }
+ });
+
+ t1.Start();
+ t2.Start();
+ t3.Start();
+ await Task.Delay(100);
+ t1.Join();
+ t2.Join();
+ t3.Join();
+ Assert.False(t1.IsAlive);
+ Assert.False(t2.IsAlive);
+ Assert.False(t3.IsAlive);
+
+ var alllines = File.ReadAllLines(file1);
+ Assert.NotEmpty(alllines);
+
+ /* Lines were added to the list and to the file parallel so the two data should be equal.
+ * If we "substract" them from each other we should get empty array.
+ */
+
+ var diff = alllines.Except(list);
+ Assert.Empty(diff);
+ }
}
}

Add IoTestsAsync