Skip to content

Commit

Permalink
[tests] These are the worst tests ever
Browse files Browse the repository at this point in the history
This should make them deterministically green. We should really
be awaiting the returned tasks to guarantee the data has fully
written.

As the internals have become more and more asynchronous, the lack
of 'await' on the individual tasks has started making the test
racey.
  • Loading branch information
alanmcgovern committed Feb 25, 2021
1 parent 882f536 commit 99d8c5a
Showing 1 changed file with 18 additions and 9 deletions.
27 changes: 18 additions & 9 deletions src/MonoTorrent.Tests/Client/DiskManagerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -316,20 +316,24 @@ public async Task ReadPieceTwo ()
public async Task ReadRate ()
{
var buffer = new byte[Piece.BlockSize];
await diskManager.UpdateSettingsAsync (new EngineSettingsBuilder { MaximumDiskReadRate = Piece.BlockSize }.ToSettings ());
await diskManager.UpdateSettingsAsync (new EngineSettingsBuilder { MaximumDiskReadRate = Piece.BlockSize, DiskCacheBytes = 0 }.ToSettings ());
await diskManager.Tick (1000);

var tasks = new List<Task> ();
for (int i = 0; i < SpeedMonitor.DefaultAveragePeriod + 1; i++)
tasks.Add (diskManager.ReadAsync (fileData, new BlockInfo (0, 0, Piece.BlockSize), buffer).AsTask ());

while (diskManager.PendingReadBytes > 0)
while (diskManager.PendingReadBytes > 0) {
await diskManager.Tick (1000);
var done = await Task.WhenAny (tasks).WithTimeout ();
await done.WithTimeout ();
tasks.Remove (done);
}

await Task.WhenAll (tasks).WithTimeout ();

// We should be reading at about 1 block per second.
Assert.AreEqual (Piece.BlockSize, diskManager.ReadRate, "#1");
Assert.AreEqual ((SpeedMonitor.DefaultAveragePeriod + 1) * Piece.BlockSize, diskManager.TotalBytesRead, "#2");
await Task.WhenAll (tasks).WithTimeout ();
}

[Test]
Expand Down Expand Up @@ -504,19 +508,24 @@ public async Task WritePiece_ReverseOrder ()
public async Task WriteRate ()
{
var buffer = new byte[Piece.BlockSize];
await diskManager.UpdateSettingsAsync (new EngineSettingsBuilder { MaximumDiskWriteRate = Piece.BlockSize }.ToSettings ());
await diskManager.UpdateSettingsAsync (new EngineSettingsBuilder { MaximumDiskWriteRate = Piece.BlockSize, DiskCacheBytes = 0 }.ToSettings ());
await diskManager.Tick (1000);

var writes = new List<Task> ();
var tasks = new List<Task> ();
for (int i = 0; i < SpeedMonitor.DefaultAveragePeriod + 1; i++)
writes.Add (diskManager.WriteAsync (fileData, new BlockInfo (i / (fileData.PieceLength / Piece.BlockSize), i, Piece.BlockSize), buffer).AsTask ());
while (diskManager.PendingWriteBytes > 0)
tasks.Add (diskManager.WriteAsync (fileData, new BlockInfo (i / (fileData.PieceLength / Piece.BlockSize), i, Piece.BlockSize), buffer).AsTask ());
while (diskManager.PendingWriteBytes > 0) {
await diskManager.Tick (1000).WithTimeout ();
var done = await Task.WhenAny (tasks).WithTimeout ();
await done.WithTimeout ();
tasks.Remove (done);
}

await Task.WhenAll (tasks).WithTimeout ();

// We should be writing at about 1 block per second.
Assert.AreEqual (Piece.BlockSize, diskManager.WriteRate, "#1");
Assert.AreEqual ((SpeedMonitor.DefaultAveragePeriod + 1) * Piece.BlockSize, diskManager.TotalBytesWritten, "#2");
await Task.WhenAll (writes).WithTimeout ();
}
}
}

0 comments on commit 99d8c5a

Please sign in to comment.