Skip to content

Commit

Permalink
Custom level of parallel tests
Browse files Browse the repository at this point in the history
  • Loading branch information
LukeButters committed Feb 4, 2024
1 parent 18975d0 commit 37886d5
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 1 deletion.
4 changes: 3 additions & 1 deletion source/Octopus.Tentacle.Tests.Integration/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using NUnit.Framework;
using Octopus.Tentacle.Tests.Integration.Support.TestAttributes;

[assembly: Parallelizable(ParallelScope.All)]
[assembly: Parallelizable(ParallelScope.All)]
[assembly: CustomLevelOfParallelism]
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using NUnit.Framework;
using Octopus.Tentacle.Tests.Integration.Support.TestAttributes;

namespace Octopus.Tentacle.Tests.Integration.Support
{
public class HowManyParallelTests : IntegrationTest
{
[Test]
public void HowManyTestsAreRunningInParallel()
{
// Only exists to make it easy to find out how many tests are running in parallel.
Logger.Information("The number of parallel tests are: {LevelOfParallelism} on a machine with {NumberOfCpuCores} cpu cores.",
CustomLevelOfParallelismAttribute.LevelOfParallelism(),
Environment.ProcessorCount);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using System;
using NUnit.Framework;
using NUnit.Framework.Api;
using Octopus.Tentacle.Util;

namespace Octopus.Tentacle.Tests.Integration.Support.TestAttributes
{
/// <summary>
/// Halibut tests take time but don't use lots of CPU.
///
/// For local development by default increase the level of parallelism to 2x the number of Cores.
///
/// This can be overriden with the environment variable "CustomLevelOfParallelism" e.g.
/// CustomLevelOfParallelism=256
///
/// When run in the build the default level is always used.
/// </summary>
[AttributeUsage( AttributeTargets.Assembly, AllowMultiple=false, Inherited=false )]
public class CustomLevelOfParallelismAttribute : PropertyAttribute
{
public CustomLevelOfParallelismAttribute() : base(LevelOfParallelismAttributePropertyName(), LevelOfParallelism())
{
}

public static int LevelOfParallelism()
{
if (TeamCityDetection.IsRunningInTeamCity())
{
return LevelOfParallelismFromEnvVar() ?? NUnitTestAssemblyRunner.DefaultLevelOfParallelism * 2;
}

return LevelOfParallelismFromEnvVar() ?? NUnitTestAssemblyRunner.DefaultLevelOfParallelism * 2;
}

static int? LevelOfParallelismFromEnvVar()
{
var nunitLevelOfParallelismSetting = Environment.GetEnvironmentVariable("CustomLevelOfParallelism");
if (!string.IsNullOrEmpty(nunitLevelOfParallelismSetting))
{
if (int.TryParse(nunitLevelOfParallelismSetting, out var level))
{
return level;
}
}

return null;
}

static string LevelOfParallelismAttributePropertyName()
{
string propertyName = typeof(LevelOfParallelismAttribute).Name;
if ( propertyName.EndsWith( "Attribute" ) )
propertyName = propertyName.Substring( 0, propertyName.Length - 9 );
return propertyName;
}
}
}

0 comments on commit 37886d5

Please sign in to comment.