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

LMBQ-377: More logging for CloudflareHelper #404

Merged
merged 6 commits into from
Aug 24, 2024
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 1 addition & 2 deletions Lombiq.Tests.UI.Samples/xunit.runner.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@
"$schema": "https://xunit.net/schema/current/xunit.runner.schema.json",
"parallelizeAssembly": false,
"parallelizeTestCollections": true,
"maxParallelThreads": 3,
"stopOnFail": true
"maxParallelThreads": 3
}
3 changes: 2 additions & 1 deletion Lombiq.Tests.UI/CloudflareRemoteUITestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ async Task ChangeConfigurationForCloudflareAsync(OrchardCoreUITestExecutorConfig
return CloudflareHelper.ExecuteWrappedInIpAccessRuleManagementAsync(
() => base.ExecuteTestAsync(baseUri, testAsync, browser, ChangeConfigurationForCloudflareAsync),
CloudflareAccountId,
CloudflareApiToken);
CloudflareApiToken,
_testOutputHelper);
}
}
37 changes: 33 additions & 4 deletions Lombiq.Tests.UI/Helpers/CloudflareHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Xunit.Abstractions;

namespace Lombiq.Tests.UI.Helpers;

Expand All @@ -23,12 +24,17 @@ internal static class CloudflareHelper
public static async Task ExecuteWrappedInIpAccessRuleManagementAsync(
Func<Task> testAsync,
string cloudflareAccountId,
string cloudflareApiToken)
string cloudflareApiToken,
ITestOutputHelper testOutputHelper)
{
testOutputHelper.WriteLineTimestampedAndDebug(
"Current Cloudflare IP Access Rule reference count before entering semaphore: {0}.", _referenceCount);

await _semaphore.WaitAsync();
Interlocked.Increment(ref _referenceCount);

Debug.WriteLine("Current reference count at the start of the test: {0}.", _referenceCount);
testOutputHelper.WriteLineTimestampedAndDebug(
"Current Cloudflare IP Access Rule reference count after entering semaphore: {0}.", _referenceCount);

try
{
Expand All @@ -41,7 +47,7 @@ public static async Task ExecuteWrappedInIpAccessRuleManagementAsync(

if (_ipAccessRuleId == null)
{
Debug.WriteLine("Creating an IP Access Rule for the IP {0}.", (object)_currentIp);
testOutputHelper.WriteLineTimestampedAndDebug("Creating a Cloudflare IP Access Rule for the IP {0}.", _currentIp);

// Delete any pre-existing rules for the current IP first.
string preexistingRuleId = null;
Expand Down Expand Up @@ -88,6 +94,9 @@ await ReliabilityHelper.DoWithRetriesAndCatchesAsync(
});

ThrowIfNotSuccess(ruleCheckRequestResult, _currentIp, "didn't get activated");

testOutputHelper.WriteLineTimestampedAndDebug(
"Created a Cloudflare IP Access Rule for the IP {0} (Rule ID: {1}).", _currentIp, _ipAccessRuleId);
}
}
finally
Expand All @@ -101,16 +110,36 @@ await ReliabilityHelper.DoWithRetriesAndCatchesAsync(
}
finally
{
testOutputHelper.WriteLineTimestampedAndDebug(
"Current Cloudflare IP Access Rule reference count after the test (including this test): {0}.", _referenceCount);

// Clean up the IP access rule.
if (_ipAccessRuleId != null && Interlocked.Decrement(ref _referenceCount) == 0)
{
Debug.WriteLine("Removing the IP Access Rule. Current reference count: {0}.", _referenceCount);
testOutputHelper.WriteLineTimestampedAndDebug(
"Removing the Cloudflare IP Access Rule for the IP {0} (Rule ID: {1}) since this test has the last reference to it.",
_currentIp,
_ipAccessRuleId);

var oldIpAccessRuleId = _ipAccessRuleId;

var deleteSucceededResult = await DeleteIpAccessRuleWithRetriesAsync(cloudflareAccountId, _ipAccessRuleId);

if (deleteSucceededResult.IsSuccess) _ipAccessRuleId = null;

ThrowIfNotSuccess(deleteSucceededResult, _currentIp, "couldn't be deleted");

testOutputHelper.WriteLineTimestampedAndDebug(
"Removed the Cloudflare IP Access Rule for the IP {0} (Rule ID: {1}) since this test had the last reference to it.",
_currentIp,
oldIpAccessRuleId);
}
else
{
testOutputHelper.WriteLineTimestampedAndDebug(
"Not removing the Cloudflare IP Access Rule for the IP {0} (Rule ID: {1}) since the current reference count is NOT 0.",
_currentIp,
_ipAccessRuleId);
}
}
}
Expand Down