Skip to content

Fix a flaky Extensions.ML test. #4458

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 6 additions & 10 deletions test/Microsoft.Extensions.ML.Tests/FileLoaderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

using System;
using System.IO;
using System.Threading.Tasks;
using System.Threading;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
Expand Down Expand Up @@ -48,10 +48,8 @@ public void can_load_model()
Assert.True(prediction.Sentiment);
}

//TODO: This is a quick test to give coverage of the main scenarios. Refactoring and re-implementing of tests should happen.
//Right now this screams of probably flakeyness
[Fact]
public async Task can_reload_model()
public void can_reload_model()
{
var services = new ServiceCollection()
.AddOptions()
Expand All @@ -61,16 +59,14 @@ public async Task can_reload_model()
var loaderUnderTest = ActivatorUtilities.CreateInstance<FileLoaderMock>(sp);
loaderUnderTest.Start("testdata.txt", true);

var changed = false;
var changeTokenRegistration = ChangeToken.OnChange(
using AutoResetEvent changed = new AutoResetEvent(false);
using IDisposable changeTokenRegistration = ChangeToken.OnChange(
() => loaderUnderTest.GetReloadToken(),
() => changed = true);
() => changed.Set());

File.WriteAllText("testdata.txt", "test");

await Task.Delay(1000);

Assert.True(changed);
Assert.True(changed.WaitOne(1000), "FileLoader ChangeToken didn't fire before the allotted time.");
}


Expand Down
20 changes: 10 additions & 10 deletions test/Microsoft.Extensions.ML.Tests/UriLoaderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ public void can_reload_model()
var loaderUnderTest = ActivatorUtilities.CreateInstance<UriLoaderMock>(sp);
loaderUnderTest.Start(new Uri("http://microsoft.com"), TimeSpan.FromMilliseconds(1));

var changed = false;
var changeTokenRegistration = ChangeToken.OnChange(
using AutoResetEvent changed = new AutoResetEvent(false);
using IDisposable changeTokenRegistration = ChangeToken.OnChange(
() => loaderUnderTest.GetReloadToken(),
() => changed = true);
Thread.Sleep(30);
Assert.True(changed);
() => changed.Set());

Assert.True(changed.WaitOne(1000), "UriLoader ChangeToken didn't fire before the allotted time.");
}

[Fact]
Expand All @@ -62,12 +62,12 @@ public void no_reload_no_change()

loaderUnderTest.Start(new Uri("http://microsoft.com"), TimeSpan.FromMilliseconds(1));

var changed = false;
var changeTokenRegistration = ChangeToken.OnChange(
using AutoResetEvent changed = new AutoResetEvent(false);
using IDisposable changeTokenRegistration = ChangeToken.OnChange(
() => loaderUnderTest.GetReloadToken(),
() => changed = true);
Thread.Sleep(30);
Assert.False(changed);
() => changed.Set());

Assert.False(changed.WaitOne(100), "UriLoader ChangeToken fired but shouldn't have.");
}
}

Expand Down