Skip to content
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

test: MemoryTests/AbsoluteExpirationRelativeToNowExpiresCorrectlyAsync is flaky #6166

Closed
kiminuo opened this issue Aug 10, 2021 · 1 comment · Fixed by #6167
Closed

test: MemoryTests/AbsoluteExpirationRelativeToNowExpiresCorrectlyAsync is flaky #6166

kiminuo opened this issue Aug 10, 2021 · 1 comment · Fixed by #6167
Assignees

Comments

@kiminuo
Copy link
Contributor

kiminuo commented Aug 10, 2021

From time to time, AbsoluteExpirationRelativeToNowExpiresCorrectlyAsync https://github.com/zkSNACKs/WalletWasabi/pull/6159/checks?check_run_id=3288453648 fails. I have tried to make it fail on Windows 10 and I couldn't. I tried on Linux and it seems it fails there much more often (like 1 / 1000 attempts or so).

I believe we hit dotnet/runtime#45585 issue here:

[Fact]
public async Task AbsoluteExpirationRelativeToNowExpiresCorrectlyAsync()
{
	// This should be buggy but it seems to work for us: https://github.com/alastairtree/LazyCache/issues/84
	using var cache = new MemoryCache(new MemoryCacheOptions());

	var result = await cache.AtomicGetOrCreateAsync(
		"key",
		new MemoryCacheEntryOptions { AbsoluteExpirationRelativeToNow = TimeSpan.FromMilliseconds(20) },
		() => Task.FromResult("foo"));
	Assert.Equal("foo", result);

	result = await cache.AtomicGetOrCreateAsync(
		"key",
		new MemoryCacheEntryOptions { AbsoluteExpirationRelativeToNow = TimeSpan.FromMilliseconds(20) },
		() => Task.FromResult("bar"));
	Assert.Equal("foo", result);

	await Task.Delay(TimeSpan.FromMilliseconds(21)); // <-- XXX: THIS CAN TAKE LESS THAN 21 milliseconds

	result = await cache.AtomicGetOrCreateAsync(
		"key",
		new MemoryCacheEntryOptions { AbsoluteExpirationRelativeToNow = TimeSpan.FromMilliseconds(20) },
		() => Task.FromResult("bar"));
	Assert.Equal("bar", result);
}

on my linux machine I get rarely 19 milliseconds instead of 21 on line XXX when measuring with Stopwatch and in that case the test fails.

I believe it would be better to use Thread.Sleep(21) instead of await Task.Delay(TimeSpan.FromMilliseconds(21)) here. So far with this change, it did not fail for me. So it may not fix the issue entirely but it may make it better.

@nopara73 @molnard @lontivero WDYT?

@kiminuo kiminuo assigned molnard, lontivero and kiminuo and unassigned lontivero and molnard Aug 10, 2021
@nopara73
Copy link
Contributor

Go ahead

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants