Skip to content

Commit

Permalink
Shorten time needed for unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
spkl committed Aug 16, 2024
1 parent 07164fc commit 4954959
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 18 deletions.
26 changes: 13 additions & 13 deletions test/CLI.IPC.Test.Singleton/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@ internal class Program
{
private static string AssemblyDir => Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!;

private const int HostAliveTime_Seconds = 5;
private static readonly TimeSpan HostAliveTime = TimeSpan.FromSeconds(2);

private const int HostShutdownGracePeriod_Milliseconds = 250;
private static readonly TimeSpan HostShutdownGracePeriod = TimeSpan.FromSeconds(0.25);

private const int PollingPeriod_Milliseconds = 250;
private static readonly TimeSpan PollingPeriod = TimeSpan.FromSeconds(0.25);

private const int TimeoutThreshold_Seconds = 10;
private static readonly TimeSpan TimeoutThreshold = TimeSpan.FromSeconds(10);

private const int HostStartupDelay_Seconds = 1;
private static readonly TimeSpan HostStartupDelay = TimeSpan.FromSeconds(0.5);

private const int ClientConnectionDuration_Seconds = 1;
private static readonly TimeSpan ClientConnectionDuration = TimeSpan.FromSeconds(0.5);

private const string ARG_HOST = "host";
private const string ARG_STATIC_TIME = "staticTime";
Expand Down Expand Up @@ -53,22 +53,22 @@ static void Main(string[] args)

if (args[0] == ARG_HOST)
{
Thread.Sleep(TimeSpan.FromSeconds(HostStartupDelay_Seconds));
Thread.Sleep(Program.HostStartupDelay);

Host h = Host.Start(s.Transport, new ClientConnectionHandler());
s.ReportInstanceRunning();

if (timeoutBehavior == ARG_STATIC_TIME)
{
Thread.Sleep(TimeSpan.FromSeconds(Program.HostAliveTime_Seconds));
Thread.Sleep(Program.HostAliveTime);
}
else
{
h.WaitUntilUnusedFor(TimeSpan.FromSeconds(Program.HostAliveTime_Seconds));
h.WaitUntilUnusedFor(Program.HostAliveTime);
}

s.ReportInstanceShuttingDown();
Thread.Sleep(Program.HostShutdownGracePeriod_Milliseconds);
Thread.Sleep(Program.HostShutdownGracePeriod);
h.Shutdown();
h.WaitUntilAllClientsDisconnected();
}
Expand All @@ -83,9 +83,9 @@ private class StartupBehavior : IStartupBehavior
{
public string NegotiationFileBasePath => Path.Combine(Program.AssemblyDir, "s");

public TimeSpan PollingPeriod { get; } = TimeSpan.FromMilliseconds(Program.PollingPeriod_Milliseconds);
public TimeSpan PollingPeriod { get; } = Program.PollingPeriod;

public TimeSpan TimeoutThreshold { get; } = TimeSpan.FromSeconds(Program.TimeoutThreshold_Seconds);
public TimeSpan TimeoutThreshold { get; } = Program.TimeoutThreshold;

private readonly string timeoutBehavior;

Expand Down Expand Up @@ -127,7 +127,7 @@ public void HandleCall(IClientConnection connection)

connection.Out.Write("PID " + processId);

Thread.Sleep(TimeSpan.FromSeconds(Program.ClientConnectionDuration_Seconds));
Thread.Sleep(Program.ClientConnectionDuration);

connection.Exit(0);
}
Expand Down
2 changes: 1 addition & 1 deletion test/CLI.IPC.Test/ExecutionTests/MultipleClientsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ private class ClientConnectionHandler : IClientConnectionHandler

public void HandleCall(IClientConnection connection)
{
Thread.Sleep(2000);
Thread.Sleep(500);
connection.Out.Write("Done");
connection.Exit(0);
}
Expand Down
8 changes: 4 additions & 4 deletions test/CLI.IPC.Test/ExecutionTests/SingletonTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ internal class SingletonTest : TestBase
{
/// <summary>
/// Approach:
/// - staticTime: Host lifetime is 5 seconds, so if we continuously create new clients for 7 seconds, there should be exactly two host processes.
/// - untilUnused: Host lifetime is "last used time" plus 5 seconds, so there should be only one host process.
/// - staticTime: Host lifetime is 2 seconds, so if we continuously create new clients for 3.5 seconds, there should be exactly two host processes.
/// - untilUnused: Host lifetime is "last used time" plus 2 seconds, so there should be only one host process.
/// </summary>
[Test]
[TestCase("staticTime", 2)]
Expand All @@ -37,7 +37,7 @@ public void TestSingleton(string timeoutBehavior, int expectedProcesses)
ConcurrentQueue<int> exitCodes = new ConcurrentQueue<int>();
ConcurrentQueue<string> stdOutputs = new ConcurrentQueue<string>();
ConcurrentQueue<string> errOutputs = new ConcurrentQueue<string>();
TimeSpan testDuration = TimeSpan.FromSeconds(7);
TimeSpan testDuration = TimeSpan.FromSeconds(3.5);
DateTime testStart = DateTime.Now;
int startedProcesses = 0;

Expand Down Expand Up @@ -72,7 +72,7 @@ public void TestSingleton(string timeoutBehavior, int expectedProcesses)
t.Join();
}

Thread.Sleep(TimeSpan.FromSeconds(3)); // Grace period for host shutdown.
Thread.Sleep(TimeSpan.FromSeconds(1)); // Grace period for host shutdown.

// assert
TestContext.Out.WriteLine($"{startedProcesses} processes were started.");
Expand Down

0 comments on commit 4954959

Please sign in to comment.