Skip to content

Commit

Permalink
Use TaskCompletionSource instead of Task.Delay
Browse files Browse the repository at this point in the history
  • Loading branch information
Arlodotexe committed May 18, 2024
2 parents bc6070e + a720611 commit 88e1e99
Showing 1 changed file with 12 additions and 17 deletions.
29 changes: 12 additions & 17 deletions tests/OwlCore.Storage.Tests/SystemIO/IFolderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,30 +43,27 @@ public async Task FolderWatcherOnFileCreate()
var folder = await CreateModifiableFolderAsync();

await using var watcher = await folder.GetFolderWatcherAsync();
bool collectionChanged = false;
watcher.CollectionChanged += (sender, args) => collectionChanged = true;
var collectionChangedTaskCompletionSource = new TaskCompletionSource();
watcher.CollectionChanged += (sender, args) => collectionChangedTaskCompletionSource.SetResult();

await folder.CreateFileAsync(GetHashCode().ToString(), overwrite: true);

// Await a small delay before asserting
await Task.Delay(2000);
Assert.IsTrue(collectionChanged, "CollectionChanged was not raised on file create");

await collectionChangedTaskCompletionSource.Task;
}

[TestMethod]
[Timeout(2000)]
public async Task FolderWatcherOnFolderCreate()
{
var folder = await CreateModifiableFolderAsync();

await using var watcher = await folder.GetFolderWatcherAsync();
bool collectionChanged = false;
watcher.CollectionChanged += (sender, args) => collectionChanged = true;
var collectionChangedTaskCompletionSource = new TaskCompletionSource();
watcher.CollectionChanged += (sender, args) => collectionChangedTaskCompletionSource.SetResult();

await folder.CreateFolderAsync(GetHashCode().ToString(), overwrite: true);

// Await a small delay before asserting
await Task.Delay(2000);
Assert.IsTrue(collectionChanged, "CollectionChanged was not raised on folder create");
await collectionChangedTaskCompletionSource.Task;
}

[TestMethod]
Expand All @@ -76,13 +73,11 @@ public async Task FolderWatcherOnDelete()
var existingItem = await folder.GetItemsAsync(StorableType.File).FirstAsync();

await using var watcher = await folder.GetFolderWatcherAsync();
bool collectionChanged = false;
watcher.CollectionChanged += (sender, args) => collectionChanged = true;
var collectionChangedTaskCompletionSource = new TaskCompletionSource();
watcher.CollectionChanged += (sender, args) => collectionChangedTaskCompletionSource.SetResult();

await folder.DeleteAsync(existingItem);

// Await a small delay before asserting
await Task.Delay(2000);
Assert.IsTrue(collectionChanged, "CollectionChanged was not raised on delete");

await collectionChangedTaskCompletionSource.Task;
}
}

0 comments on commit 88e1e99

Please sign in to comment.