Skip to content

Commit

Permalink
fix test util
Browse files Browse the repository at this point in the history
  • Loading branch information
trudyhood committed Mar 20, 2024
1 parent de108a7 commit 40bbe26
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Tests/VpnHood.Test/Tests/AccessTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public async Task Server_reject_expired_access_at_runtime()
await using var server = TestHelper.CreateServer(fileAccessManagerOptions);

// create a short expiring token
var accessToken = TestHelper.CreateAccessToken(server, expirationTime: DateTime.Now.AddMinutes(-1));
var accessToken = TestHelper.CreateAccessToken(server, expirationTime: DateTime.Now.AddSeconds(1));

// connect and download
await using var client = await TestHelper.CreateClient(accessToken, throwConnectException: false);
Expand Down
19 changes: 10 additions & 9 deletions VpnHood.Common/Utils/VhTestUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,26 @@ public AssertException(string message, Exception innerException)
}
}

public static async Task<bool> WaitForValue<TValue>(object? expectedValue, Func<TValue?> valueFactory, int timeout = 5000)
private static async Task WaitForValue<TValue>(object? expectedValue, Func<TValue?> valueFactory, int timeout = 5000)
{
const int waitTime = 100;
for (var elapsed = 0; elapsed < timeout; elapsed += waitTime)
{
if (Equals(expectedValue, valueFactory()))
return true;
return;

await Task.Delay(waitTime);
}

return false;
throw new TimeoutException();
}

private static async Task WaitForValue<TValue>(object? expectedValue, Task<TValue?> task, int timeout = 5000)
private static async Task WaitForValue<TValue>(object? expectedValue, Func<Task<TValue?>> valueFactory, int timeout = 5000)
{
const int waitTime = 100;
for (var elapsed = 0; elapsed < timeout; elapsed += waitTime)
{
if (Equals(expectedValue, await task))
if (Equals(expectedValue, await valueFactory()))
return;

await Task.Delay(waitTime);
Expand All @@ -47,6 +47,7 @@ private static async Task WaitForValue<TValue>(object? expectedValue, Task<TValu
throw new TimeoutException();
}


private static void AssertEquals(object? expected, object? actual, string? message)
{
message ??= "Unexpected Value";
Expand All @@ -61,20 +62,20 @@ public static async Task AssertEqualsWait<TValue>(object? expectedValue, Func<TV
AssertEquals(expectedValue, valueFactory(), message);
}

public static Task AssertEqualsWait<TValue>(object? expectedValue, Func<Task<TValue?>> valueFactory,
public static async Task AssertEqualsWait<TValue>(object? expectedValue, Func<Task<TValue?>> valueFactory,
string? message = null, int timeout = 5000)
{
return AssertEqualsWait(expectedValue, valueFactory(), message, timeout);
await WaitForValue(expectedValue, valueFactory, timeout);
AssertEquals(expectedValue, await valueFactory(), message);
}

public static async Task AssertEqualsWait<TValue>(object? expectedValue, Task<TValue?> task,
string? message = null, int timeout = 5000)
{
await WaitForValue(expectedValue, task, timeout);
await WaitForValue(expectedValue, () => task, timeout);
AssertEquals(expectedValue, await task, message);
}


public static Task AssertApiException(HttpStatusCode expectedStatusCode, Task task, string? message = null)
{
return AssertApiException((int)expectedStatusCode, task, message);
Expand Down

0 comments on commit 40bbe26

Please sign in to comment.