From 96c2ade7ff0784f95952514b650243766d21fe46 Mon Sep 17 00:00:00 2001 From: benbierens Date: Thu, 27 Apr 2023 15:55:33 +0200 Subject: [PATCH] Allows for the use of UseLongTimeouts in combination with test-case attribute --- DistTestCore/DistTest.cs | 26 ++++++++++++++++++++++++++ DistTestCore/Timing.cs | 10 +++------- Tests/BasicTests/DownloadTests.cs | 26 +++++++------------------- Tests/BasicTests/UploadTests.cs | 25 +++++++------------------ 4 files changed, 43 insertions(+), 44 deletions(-) diff --git a/DistTestCore/DistTest.cs b/DistTestCore/DistTest.cs index edce42d..624958f 100644 --- a/DistTestCore/DistTest.cs +++ b/DistTestCore/DistTest.cs @@ -5,6 +5,7 @@ using DistTestCore.Metrics; using KubernetesWorkflow; using Logging; using NUnit.Framework; +using System.Reflection; using Utils; namespace DistTestCore @@ -13,15 +14,23 @@ namespace DistTestCore public abstract class DistTest { private readonly Configuration configuration = new Configuration(); + private readonly Assembly[] testAssemblies; private FixtureLog fixtureLog = null!; private TestLifecycle lifecycle = null!; private DateTime testStart = DateTime.MinValue; + public DistTest() + { + var assemblies = AppDomain.CurrentDomain.GetAssemblies(); + testAssemblies = assemblies.Where(a => a.FullName!.ToLowerInvariant().Contains("test")).ToArray(); + } + [OneTimeSetUp] public void GlobalSetup() { // Previous test run may have been interrupted. // Begin by cleaning everything up. + Timing.UseLongTimeouts = false; fixtureLog = new FixtureLog(configuration.GetLogConfig()); try @@ -48,6 +57,8 @@ namespace DistTestCore [SetUp] public void SetUpDistTest() { + Timing.UseLongTimeouts = ShouldUseLongTimeouts(); + if (GlobalTestFailure.HasFailed) { Assert.Inconclusive("Skip test: Previous test failed during clean up."); @@ -58,6 +69,21 @@ namespace DistTestCore } } + private bool ShouldUseLongTimeouts() + { + // Don't be fooled! TestContext.CurrentTest.Test allows you easy access to the attributes of the current test. + // But this doesn't work for tests making use of [TestCase]. So instead, we use reflection here to figure out + // if the attribute is present. + var currentTest = TestContext.CurrentContext.Test; + var className = currentTest.ClassName; + var methodName = currentTest.MethodName; + + var testClasses = testAssemblies.SelectMany(a => a.GetTypes()).Where(c => c.FullName == className).ToArray(); + var testMethods = testClasses.SelectMany(c => c.GetMethods()).Where(m => m.Name == methodName).ToArray(); + + return testMethods.Any(m => m.GetCustomAttribute() != null); + } + [TearDown] public void TearDownDistTest() { diff --git a/DistTestCore/Timing.cs b/DistTestCore/Timing.cs index 67653ec..3cfc1bc 100644 --- a/DistTestCore/Timing.cs +++ b/DistTestCore/Timing.cs @@ -6,15 +6,12 @@ namespace DistTestCore [AttributeUsage(AttributeTargets.Method, AllowMultiple = false)] public class UseLongTimeoutsAttribute : PropertyAttribute { - public UseLongTimeoutsAttribute() - : base(Timing.UseLongTimeoutsKey) - { - } } public static class Timing { - public const string UseLongTimeoutsKey = "UseLongTimeouts"; + public static bool UseLongTimeouts { get; set; } + public static TimeSpan HttpCallTimeout() { @@ -48,8 +45,7 @@ namespace DistTestCore private static ITimeSet GetTimes() { - var testProperties = TestContext.CurrentContext.Test.Properties; - if (testProperties.ContainsKey(UseLongTimeoutsKey)) return new LongTimeSet(); + if (UseLongTimeouts) return new LongTimeSet(); return new DefaultTimeSet(); } } diff --git a/Tests/BasicTests/DownloadTests.cs b/Tests/BasicTests/DownloadTests.cs index e26ec7f..ede6847 100644 --- a/Tests/BasicTests/DownloadTests.cs +++ b/Tests/BasicTests/DownloadTests.cs @@ -1,28 +1,16 @@ using DistTestCore; -using KubernetesWorkflow; using NUnit.Framework; + namespace Tests.ParallelTests { [TestFixture] public class DownloadTests : DistTest { - [Test] - public void ThreeNodeDownloads() - { - ParallelDownload(3, 5000.MB()); - } - [Test] - public void FiveNodeDownloads() - { - ParallelDownload(5, 1000.MB()); - } - [Test] - public void TenNodeDownloads() - { - ParallelDownload(10, 256.MB()); - } - - void ParallelDownload(int numberOfNodes, ByteSize filesize) + [TestCase(3, 50)] + [TestCase(5, 750)] + [TestCase(10, 25)] + [UseLongTimeouts] + public void ParallelDownload(int numberOfNodes, int filesizeMb) { var group = SetupCodexNodes(numberOfNodes); var host = SetupCodexNode(); @@ -32,7 +20,7 @@ namespace Tests.ParallelTests host.ConnectToPeer(node); } - var testFile = GenerateTestFile(filesize); + var testFile = GenerateTestFile(filesizeMb.MB()); var contentId = host.UploadFile(testFile); var list = new List>(); diff --git a/Tests/BasicTests/UploadTests.cs b/Tests/BasicTests/UploadTests.cs index 5562170..72afc23 100644 --- a/Tests/BasicTests/UploadTests.cs +++ b/Tests/BasicTests/UploadTests.cs @@ -1,27 +1,16 @@ using DistTestCore; -using KubernetesWorkflow; using NUnit.Framework; + namespace Tests.ParallelTests { [TestFixture] public class UploadTests : DistTest { - [Test] - public void ThreeNodeUploads() - { - ParallelUpload(3, 50.MB()); - } - [Test] - public void FiveNodeUploads() - { - ParallelUpload(5, 750.MB()); - } - [Test] - public void TenNodeUploads() - { - ParallelUpload(10, 25.MB()); - } - void ParallelUpload(int numberOfNodes, ByteSize filesize) + [TestCase(3, 50)] + [TestCase(5, 750)] + [TestCase(10, 25)] + [UseLongTimeouts] + public void ParallelUpload(int numberOfNodes, int filesizeMb) { var group = SetupCodexNodes(numberOfNodes); var host = SetupCodexNode(); @@ -36,7 +25,7 @@ namespace Tests.ParallelTests for (int i = 0; i < group.Count(); i++) { - testfiles.Add(GenerateTestFile(filesize)); + testfiles.Add(GenerateTestFile(filesizeMb.MB())); var n = i; contentIds.Add(Task.Run(() => { return host.UploadFile(testfiles[n]); })); }