Skip to content

Commit

Permalink
Merge pull request #404 from Lombiq/issue/LMBQ-377
Browse files Browse the repository at this point in the history
LMBQ-377: Issue/lmbq 377
  • Loading branch information
DemeSzabolcs authored Aug 24, 2024
2 parents 37a3776 + 9fe37ae commit 3c473ba
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 7 deletions.
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

0 comments on commit 3c473ba

Please sign in to comment.