Skip to content

Commit 5dc0abb

Browse files
committed
Merge pull request #40 from MehdiK/lean-StoryCache
Reduces memory footprint by disposing unnecessary objects in StoryCache
2 parents a53fe0c + 45782ee commit 5dc0abb

File tree

9 files changed

+45
-11
lines changed

9 files changed

+45
-11
lines changed

Samples/TestStack.BDDfy.Samples/AsyncExample.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,19 @@ private async Task<Sut> CreateSut()
3434
[Test]
3535
public void Run()
3636
{
37-
var engine = this.LazyBDDfy();
38-
var exception = Assert.Throws<Exception>(() => engine.Run());
39-
Assert.AreEqual("Exception in async void method!!", exception.Message);
37+
try
38+
{
39+
// we need TestObject for this test; so I disable StoryCache processor for this one test
40+
Configuration.Configurator.Processors.StoryCache.Disable();
41+
var engine = this.LazyBDDfy();
42+
var exception = Assert.Throws<Exception>(() => engine.Run());
43+
44+
Assert.AreEqual("Exception in async void method!!", exception.Message);
45+
}
46+
finally
47+
{
48+
Configuration.Configurator.Processors.StoryCache.Enable();
49+
}
4050
}
4151

4252
internal class Sut

TestStack.BDDfy.Tests/Disposer/DisposingScenarios.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,18 @@ public void Execute(ThrowingMethods throwingMethods)
5757
var bddifier = scenario.LazyBDDfy();
5858
try
5959
{
60+
// we need TestObject for this test; so I disable StoryCache processor for this one test
61+
BDDfy.Configuration.Configurator.Processors.StoryCache.Disable();
6062
bddifier.Run();
6163
}
6264
catch (Exception)
6365
{
6466
// there will be an exception but we do not care about it
6567
}
68+
finally
69+
{
70+
BDDfy.Configuration.Configurator.Processors.StoryCache.Enable();
71+
}
6672
var story = bddifier.Story;
6773

6874
Assert.That(story.Scenarios.All(s => ((DisposableScenario)s.TestObject).Disposed), Is.False);

TestStack.BDDfy.Tests/Story/StoryClassAndScenarioClassAreTheSame.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,17 @@ void andTheNarrativeIsReturnedAsExpected()
5252
[Test]
5353
public void Execute()
5454
{
55-
this.BDDfy();
55+
try
56+
{
57+
// we need TestObject for this test; so I disable StoryCache processor for this one test
58+
BDDfy.Configuration.Configurator.Processors.StoryCache.Disable();
59+
60+
this.BDDfy();
61+
}
62+
finally
63+
{
64+
BDDfy.Configuration.Configurator.Processors.StoryCache.Enable();
65+
}
5666
}
5767
}
5868
}

TestStack.BDDfy/Core/Engine.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,13 @@ public Story Run()
3838

3939
try
4040
{
41-
//run processors in the right order regardless of the order they are provided to the Bddifer
42-
foreach (var processor in processors.Where(p => p.ProcessType != ProcessType.Finally).OrderBy(p => (int)p.ProcessType))
41+
//run processors in the right order regardless of the order they are provided to the Bddfier
42+
foreach (var processor in processors.Where(p => p.ProcessType < ProcessType.Disposal).OrderBy(p => (int)p.ProcessType))
4343
processor.Process(_story);
4444
}
4545
finally
4646
{
47-
foreach (var finallyProcessor in processors.Where(p => p.ProcessType == ProcessType.Finally))
47+
foreach (var finallyProcessor in processors.Where(p => p.ProcessType >= ProcessType.Disposal).OrderBy(p => (int)p.ProcessType))
4848
finallyProcessor.Process(_story);
4949
}
5050

TestStack.BDDfy/Core/ExecutionStep.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public ExecutionStep(
2222
}
2323

2424
public Guid Id { get; private set; }
25-
Action<object> StepAction { get; set; }
25+
internal Action<object> StepAction { get; set; }
2626
public bool Asserts { get; private set; }
2727
public bool ShouldReport { get; private set; }
2828
public string StepTitle { get; private set; }

TestStack.BDDfy/Core/ProcessType.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ public enum ProcessType
88
Report = 4,
99
AfterReport = 5,
1010
ProcessExceptions = 6,
11-
Finally = 7,
11+
Disposal = 7,
12+
Finally = 8
1213
}
1314
}

TestStack.BDDfy/Core/Scenario.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public Scenario(object testObject, IEnumerable<ExecutionStep> steps, string scen
1818

1919
public string Title { get; private set; }
2020
public TimeSpan Duration { get { return new TimeSpan(_steps.Sum(x => x.Duration.Ticks)); } }
21-
public object TestObject { get; private set; }
21+
public object TestObject { get; internal set; }
2222
public Guid Id { get; private set; }
2323

2424
private readonly List<ExecutionStep> _steps;

TestStack.BDDfy/Processors/Disposer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public class Disposer : IProcessor
77
{
88
public ProcessType ProcessType
99
{
10-
get { return ProcessType.Finally; }
10+
get { return ProcessType.Disposal; }
1111
}
1212

1313
public void Process(Story story)

TestStack.BDDfy/Processors/StoryCache.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ public ProcessType ProcessType
1414

1515
public void Process(Story story)
1616
{
17+
foreach (var scenario in story.Scenarios)
18+
{
19+
scenario.TestObject = null;
20+
foreach (var step in scenario.Steps)
21+
step.StepAction = null;
22+
}
23+
1724
Cache.Add(story);
1825
}
1926

0 commit comments

Comments
 (0)