Skip to content

Commit 6a3df77

Browse files
committed
Split LoggingHooks
1 parent e6af4ac commit 6a3df77

File tree

3 files changed

+71
-63
lines changed

3 files changed

+71
-63
lines changed

ParallelExecution/ReqnrollCalculator.Specs.StepDefinitions/CalculatorStepDefinitions.cs

Lines changed: 2 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,18 @@
1-
using System.Collections;
2-
using System.Diagnostics;
3-
using System.Reflection;
41
using Reqnroll;
52

63
namespace ReqnrollCalculator.Specs.StepDefinitions;
74

85
[Binding]
96
public sealed class CalculatorStepDefinitions
107
{
11-
private static readonly Stopwatch Stopwatch = new();
12-
private static readonly object LockObj = new();
138
private static readonly Random Rnd = new(42);
14-
private static readonly string LogFileName = $"parallel_log_{typeof(CalculatorStepDefinitions).Assembly.GetName().Name}.txt";
15-
16-
private static void AppendLogMessage(string message, ITestRunner? testRunner = null, bool newLineAfter = false)
17-
{
18-
var timestamp = Stopwatch.Elapsed;
19-
lock (LockObj)
20-
{
21-
var line = $"{timestamp:c} {testRunner?.TestWorkerId ?? "-"}: {message}";
22-
File.AppendAllLines(LogFileName, newLineAfter ? [line, ""] : [line]);
23-
}
24-
}
259

2610
private const bool AddRandomWait = true;
27-
private const int MinWaitMs = 100;
28-
private const int MaxWaitMs = 150;
11+
private const int MinWaitMs = 300;
12+
private const int MaxWaitMs = 350;
2913

3014
private readonly Calculator _calculator = new();
3115

32-
[BeforeTestRun]
33-
public static void RunStart()
34-
{
35-
Stopwatch.Start();
36-
AppendLogMessage("BeforeTestRun");
37-
}
38-
39-
[AfterTestRun]
40-
public static void RunEnd()
41-
{
42-
AppendLogMessage("AfterTestRun", newLineAfter: true);
43-
}
44-
45-
[BeforeFeature]
46-
public static void FeatureStart(FeatureContext featureContext, ITestRunner testRunner)
47-
{
48-
AppendLogMessage($" BeforeFeature: {featureContext.FeatureInfo.Title}", testRunner);
49-
}
50-
51-
[AfterFeature]
52-
public static void FeatureEnd(FeatureContext featureContext, ITestRunner testRunner)
53-
{
54-
AppendLogMessage($" AfterFeature: {featureContext.FeatureInfo.Title}", testRunner);
55-
}
56-
57-
private string GetScenarioTitle(ScenarioContext scenarioContext)
58-
{
59-
if (scenarioContext.ScenarioInfo.Arguments == null || scenarioContext.ScenarioInfo.Arguments.Count == 0)
60-
return scenarioContext.ScenarioInfo.Title;
61-
62-
return $"{scenarioContext.ScenarioInfo.Title} / {string.Join(",", scenarioContext.ScenarioInfo.Arguments.OfType<DictionaryEntry>().Select(a => a.Value))}";
63-
}
64-
65-
[BeforeScenario]
66-
public void ScenarioStart(FeatureContext featureContext, ScenarioContext scenarioContext, ITestRunner testRunner)
67-
{
68-
AppendLogMessage($" BeforeScenario: {featureContext.FeatureInfo.Title} / {GetScenarioTitle(scenarioContext)}", testRunner);
69-
}
70-
71-
[AfterScenario]
72-
public void ScenarioEnd(FeatureContext featureContext, ScenarioContext scenarioContext, ITestRunner testRunner)
73-
{
74-
AppendLogMessage($" AfterScenario: {featureContext.FeatureInfo.Title} / {GetScenarioTitle(scenarioContext)}", testRunner);
75-
}
76-
7716
[Given("the first number is {int}")]
7817
public void GivenTheFirstNumberIs(int number)
7918
{
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
using Reqnroll;
2+
using System.Collections;
3+
using System.Diagnostics;
4+
5+
namespace ReqnrollCalculator.Specs.StepDefinitions;
6+
7+
[Binding]
8+
public class LoggingHooks
9+
{
10+
private static readonly Stopwatch Stopwatch = new();
11+
private static readonly object LockObj = new();
12+
private static readonly string LogFileName = $"parallel_log_{typeof(CalculatorStepDefinitions).Assembly.GetName().Name}.txt";
13+
14+
private static void AppendLogMessage(string message, ITestRunner? testRunner = null, bool newLineAfter = false)
15+
{
16+
var timestamp = Stopwatch.Elapsed;
17+
lock (LockObj)
18+
{
19+
var line = $"{timestamp:c} {testRunner?.TestWorkerId ?? "-"}: {message}";
20+
File.AppendAllLines(LogFileName, newLineAfter ? [line, ""] : [line]);
21+
}
22+
}
23+
24+
[BeforeTestRun]
25+
public static void RunStart()
26+
{
27+
Stopwatch.Start();
28+
AppendLogMessage("BeforeTestRun");
29+
}
30+
31+
[AfterTestRun]
32+
public static void RunEnd()
33+
{
34+
AppendLogMessage("AfterTestRun", newLineAfter: true);
35+
}
36+
37+
[BeforeFeature]
38+
public static void FeatureStart(FeatureContext featureContext, ITestRunner testRunner)
39+
{
40+
AppendLogMessage($" BeforeFeature: {featureContext.FeatureInfo.Title}", testRunner);
41+
}
42+
43+
[AfterFeature]
44+
public static void FeatureEnd(FeatureContext featureContext, ITestRunner testRunner)
45+
{
46+
AppendLogMessage($" AfterFeature: {featureContext.FeatureInfo.Title}", testRunner);
47+
}
48+
49+
private string GetScenarioTitle(ScenarioContext scenarioContext)
50+
{
51+
if (scenarioContext.ScenarioInfo.Arguments == null || scenarioContext.ScenarioInfo.Arguments.Count == 0)
52+
return scenarioContext.ScenarioInfo.Title;
53+
54+
return $"{scenarioContext.ScenarioInfo.Title} / {string.Join(",", scenarioContext.ScenarioInfo.Arguments.OfType<DictionaryEntry>().Select(a => a.Value))}";
55+
}
56+
57+
[BeforeScenario]
58+
public void ScenarioStart(FeatureContext featureContext, ScenarioContext scenarioContext, ITestRunner testRunner)
59+
{
60+
AppendLogMessage($" BeforeScenario: {featureContext.FeatureInfo.Title} / {GetScenarioTitle(scenarioContext)}", testRunner);
61+
}
62+
63+
[AfterScenario]
64+
public void ScenarioEnd(FeatureContext featureContext, ScenarioContext scenarioContext, ITestRunner testRunner)
65+
{
66+
AppendLogMessage($" AfterScenario: {featureContext.FeatureInfo.Title} / {GetScenarioTitle(scenarioContext)}", testRunner);
67+
}
68+
}

ParallelExecution/ReqnrollCalculator.Specs.StepDefinitions/ReqnrollCalculator.Specs.StepDefinitions.projitems

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@
1010
</PropertyGroup>
1111
<ItemGroup>
1212
<Compile Include="$(MSBuildThisFileDirectory)CalculatorStepDefinitions.cs" />
13+
<Compile Include="$(MSBuildThisFileDirectory)LoggingHooks.cs" />
1314
</ItemGroup>
1415
</Project>

0 commit comments

Comments
 (0)