Skip to content
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
2 changes: 1 addition & 1 deletion .github/workflows/api-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@ jobs:
dotnet build /p:Configuration=Release /v:quiet /p:WarningLevel=1 QuantConnect.Lean.sln

# Run Projects tests
dotnet test ./Tests/bin/Release/QuantConnect.Tests.dll --blame-hang-timeout 7minutes --blame-crash --logger "console;verbosity=detailed" --filter "FullyQualifiedName=QuantConnect.Tests.API.ProjectTests|FullyQualifiedName=QuantConnect.Tests.API.ObjectStoreTests" -- TestRunParameters.Parameter\(name=\"log-handler\", value=\"ConsoleErrorLogHandler\"\)
dotnet test ./Tests/bin/Release/QuantConnect.Tests.dll --blame-hang-timeout 12minutes --blame-crash --logger "console;verbosity=detailed" --filter "FullyQualifiedName=QuantConnect.Tests.API.ProjectTests|FullyQualifiedName=QuantConnect.Tests.API.ObjectStoreTests" -- TestRunParameters.Parameter\(name=\"log-handler\", value=\"ConsoleErrorLogHandler\"\)
46 changes: 26 additions & 20 deletions Tests/Api/ApiTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ namespace QuantConnect.Tests.API
/// </summary>
public class ApiTestBase
{
private static readonly object _configurationLock = new();

internal int TestAccount;
internal string TestToken;
internal string TestOrganization;
Expand Down Expand Up @@ -151,7 +153,7 @@ public static bool IsValidJson(string jsonString)
/// <param name="projectId">Id of the project</param>
/// <param name="compileId">Id of the compilation of the project</param>
/// <returns></returns>
public static Compile WaitForCompilerResponse(Api.Api apiClient, int projectId, string compileId, int seconds = 60)
public static Compile WaitForCompilerResponse(Api.Api apiClient, int projectId, string compileId, int seconds = 300)
{
var compile = new Compile();
var finish = DateTime.UtcNow.AddSeconds(seconds);
Expand Down Expand Up @@ -189,7 +191,7 @@ public static void WaitForLiveAlgorithmToStop(Api.Api apiClient, int projectId,
/// <param name="projectId">Project id to scan</param>
/// <param name="backtestId">Backtest id previously started</param>
/// <returns>Completed backtest object</returns>
public static Backtest WaitForBacktestCompletion(Api.Api apiClient, int projectId, string backtestId, int secondsTimeout = 60, bool returnFailedBacktest = false)
public static Backtest WaitForBacktestCompletion(Api.Api apiClient, int projectId, string backtestId, int secondsTimeout = 300, bool returnFailedBacktest = false)
{
Backtest backtest;
var finish = DateTime.UtcNow.AddSeconds(secondsTimeout);
Expand Down Expand Up @@ -226,29 +228,33 @@ public static Backtest WaitForBacktestCompletion(Api.Api apiClient, int projectI
/// </summary>
internal static void ReloadConfiguration()
{
// nunit 3 sets the current folder to a temp folder we need it to be the test bin output folder
var dir = TestContext.CurrentContext.TestDirectory;
Environment.CurrentDirectory = dir;
Directory.SetCurrentDirectory(dir);
// reload config from current path
Config.Reset();

var environment = Environment.GetEnvironmentVariables();
foreach (DictionaryEntry entry in environment)
// fixtures running in parallel set up at once, and the configuration they reload is shared
lock (_configurationLock)
{
var envKey = entry.Key.ToString();
var value = entry.Value.ToString();
// nunit 3 sets the current folder to a temp folder we need it to be the test bin output folder
var dir = TestContext.CurrentContext.TestDirectory;
Environment.CurrentDirectory = dir;
Directory.SetCurrentDirectory(dir);
// reload config from current path
Config.Reset();

if (envKey.StartsWith("QC_", StringComparison.InvariantCulture))
var environment = Environment.GetEnvironmentVariables();
foreach (DictionaryEntry entry in environment)
{
var key = envKey.Substring(3).Replace("_", "-", StringComparison.InvariantCulture).ToLowerInvariant();
Log.Trace($"TestSetup(): Updating config setting '{key}' from environment var '{envKey}'");
Config.Set(key, value);
var envKey = entry.Key.ToString();
var value = entry.Value.ToString();

if (envKey.StartsWith("QC_", StringComparison.InvariantCulture))
{
var key = envKey.Substring(3).Replace("_", "-", StringComparison.InvariantCulture).ToLowerInvariant();
Log.Trace($"TestSetup(): Updating config setting '{key}' from environment var '{envKey}'");
Config.Set(key, value);
}
}
}

// resets the version among other things
Globals.Reset();
// resets the version among other things
Globals.Reset();
}
}
}
}
81 changes: 54 additions & 27 deletions Tests/Api/ObjectStoreTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using System;
using System.IO;
using System.Linq;
using System.Threading;
using QuantConnect.Api;

namespace QuantConnect.Tests.API
Expand All @@ -26,7 +27,8 @@ namespace QuantConnect.Tests.API
public class ObjectStoreTests : ApiTestBase
{
private const string _ciTestFolder = "/CI_TEST";
private const string _key = _ciTestFolder + "/Ricardo";
// Concurrent runs share the organization's object store, so each test writes under a key of its own
private const string _temporaryKeysFolder = "/CI_TEST_TEMPORARY";
private readonly byte[] _data = new byte[3] { 1, 2, 3 };

private static readonly string[] _keysToSetUp = new[]
Expand Down Expand Up @@ -62,9 +64,17 @@ public void SetUpObjectStoreFiles()
public void GetObjectStoreWorksAsExpected(string testName, List<string> keys, bool isSuccessExpected)
{
var path = Directory.GetCurrentDirectory() + "/StoreObjectFolder/";
var finish = DateTime.UtcNow.AddMinutes(6);
var result = ApiClient.GetObjectStore(TestOrganization, keys, path);
if (isSuccessExpected)
{
// Under load the download url can take longer than the client polls for it, a new request can get it sooner.
// Each call waits up to 5 minutes for its url, so a case can take up to 11: the workflow's hang timeout allows 12
while (!result && DateTime.UtcNow < finish)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Each GetObjectStore call can take up to 5 minutes (Api.cs:1387), and finish is only checked before a call. A retry that starts at about 5 minutes can therefore end at about 10, past the per-test --blame-hang-timeout 7minutes, which kills the test host and loses the rest of the run, where master fails a single assertion at 5 minutes. Either raise the hang timeout in the workflow to cover the longest retry, or retry only after a call that failed quickly, and give the real worst case in the description.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 054d5e0: the workflow's per-test --blame-hang-timeout goes from 7 to 12 minutes. A new attempt starts only before the 6-minute mark and each call waits up to 5 minutes, so the worst case for one of these cases is about 11. I kept the retry rather than limiting it to quick failures, because a slow url is the case the retry is for. The comment by the loop and the description now give that worst case.

{
Thread.Sleep(5000);
result = ApiClient.GetObjectStore(TestOrganization, keys, path);
}
Assert.IsTrue(result);
DirectoryAssert.Exists(path);
Assert.IsTrue(keys.Where(x => File.Exists(path + x) || Directory.Exists(path + x)).Any()); // For some test cases, just one of the keys is present in the Object Store.
Expand Down Expand Up @@ -99,43 +109,60 @@ public void GetObjectStorePropertiesWorksAsExpected(string key, bool isSuccessEx
[Test]
public void SetObjectStoreWorksAsExpected()
{
var result = ApiClient.DeleteObjectStore(TestOrganization, _key);
Assert.IsFalse(result.Success);
var key = $"{_temporaryKeysFolder}/{Guid.NewGuid():N}";
try
{
var result = ApiClient.DeleteObjectStore(TestOrganization, key);
Assert.IsFalse(result.Success);

result = ApiClient.SetObjectStore(TestOrganization, _key, _data);
Assert.IsTrue(result.Success);
result = ApiClient.SetObjectStore(TestOrganization, key, _data);
Assert.IsTrue(result.Success);

result = ApiClient.DeleteObjectStore(TestOrganization, _key);
Assert.IsTrue(result.Success);
result = ApiClient.DeleteObjectStore(TestOrganization, key);
Assert.IsTrue(result.Success);
}
finally
{
ApiClient.DeleteObjectStore(TestOrganization, key);
}
}

[Test]
public void DeleteObjectStoreWorksAsExpected()
{
var result = ApiClient.SetObjectStore(TestOrganization, _key + "/test1.txt", _data);
var result2 = ApiClient.SetObjectStore(TestOrganization, _key + "/test2.txt", _data);
Assert.IsTrue(result.Success);
var objectsBefore = ApiClient.ListObjectStore(TestOrganization, _key);
var stringRepresentation = objectsBefore.ToString();
Assert.IsTrue(ApiTestBase.IsValidJson(stringRepresentation));
var numberOfObjectsBefore = objectsBefore.Objects.Count;
var totalSizeBefore = objectsBefore.Objects.Select(o => o.Size).Sum();
var key = $"{_temporaryKeysFolder}/{Guid.NewGuid():N}";
try
{
var result = ApiClient.SetObjectStore(TestOrganization, key + "/test1.txt", _data);
var result2 = ApiClient.SetObjectStore(TestOrganization, key + "/test2.txt", _data);
Assert.IsTrue(result.Success);
var objectsBefore = ApiClient.ListObjectStore(TestOrganization, key);
var stringRepresentation = objectsBefore.ToString();
Assert.IsTrue(ApiTestBase.IsValidJson(stringRepresentation));
var numberOfObjectsBefore = objectsBefore.Objects.Count;
var totalSizeBefore = objectsBefore.Objects.Select(o => o.Size).Sum();

result = ApiClient.DeleteObjectStore(TestOrganization, _key + "/test1.txt");
Assert.IsTrue(result.Success);
result = ApiClient.DeleteObjectStore(TestOrganization, key + "/test1.txt");
Assert.IsTrue(result.Success);

ListObjectStoreResponse objectsAfter;
var time = DateTime.UtcNow;
do
{
objectsAfter = ApiClient.ListObjectStore(TestOrganization, _key);
} while (objectsAfter.Objects.Count == numberOfObjectsBefore && DateTime.UtcNow < time.AddMinutes(10));
ListObjectStoreResponse objectsAfter;
var time = DateTime.UtcNow;
do
{
Thread.Sleep(1000);
objectsAfter = ApiClient.ListObjectStore(TestOrganization, key);
} while (objectsAfter.Objects.Count == numberOfObjectsBefore && DateTime.UtcNow < time.AddMinutes(10));

var totalSizeAfter = objectsAfter.Objects.Select(o => o.Size).Sum();
Assert.IsTrue(totalSizeAfter < totalSizeBefore);
var totalSizeAfter = objectsAfter.Objects.Select(o => o.Size).Sum();
Assert.IsTrue(totalSizeAfter < totalSizeBefore);

result = ApiClient.DeleteObjectStore(TestOrganization, _key);
Assert.IsTrue(result.Success);
result = ApiClient.DeleteObjectStore(TestOrganization, key);
Assert.IsTrue(result.Success);
}
finally
{
ApiClient.DeleteObjectStore(TestOrganization, key);
}
}

[Test]
Expand Down
2 changes: 1 addition & 1 deletion Tests/Api/OptimizationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ private BaseOptimization GetOptimization(out int projectId)
},
estimatedCost: 0.06m,
nodeType: OptimizationNodes.O2_8,
parallelNodes: 12
parallelNodes: 6
);

return optimization;
Expand Down
Loading
Loading