Skip to content

Commit

Permalink
Ensure that CucumberMessages doesn't run when no Sink is registered.
Browse files Browse the repository at this point in the history
  • Loading branch information
clrudolphi committed Sep 4, 2024
1 parent 580478d commit eb3e257
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 4 deletions.
4 changes: 4 additions & 0 deletions Reqnroll/CucumberMesssages/CucumberMessageBroker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,17 @@ namespace Reqnroll.CucumberMesssages

public interface ICucumberMessageBroker
{
bool Enabled { get; }
void Complete(string cucumberMessageSource);
void Publish(ReqnrollCucumberMessage message);
}

public class CucumberMessageBroker : ICucumberMessageBroker
{
private IObjectContainer _objectContainer;

public bool Enabled => _objectContainer.ResolveAll<ICucumberMessageSink>().ToList().Count > 0;

//private ITraceListener _traceListener;

public CucumberMessageBroker(IObjectContainer objectContainer)
Expand Down
60 changes: 56 additions & 4 deletions Reqnroll/CucumberMesssages/CucumberMessagePublisher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public class CucumberMessagePublisher : ICucumberMessagePublisher, IRuntimePlugi
private ICucumberMessageBroker broker;
private IObjectContainer objectContainer;
private ConcurrentDictionary<string, FeatureEventProcessor> featureProcessorsByFeatureName = new();
bool Enabled = false;

public CucumberMessagePublisher(ICucumberMessageBroker CucumberMessageBroker, IObjectContainer objectContainer)
{
Expand Down Expand Up @@ -57,14 +58,23 @@ public void HookIntoTestThreadExecutionEventPublisher(ITestThreadExecutionEventP
private void FeatureStartedEventHandler(FeatureStartedEvent featureStartedEvent)
{
var featureName = featureStartedEvent.FeatureContext.FeatureInfo.Title;
var enabled = featureStartedEvent.FeatureContext.FeatureInfo.FeatureCucumberMessages.Source == null ? false : true;

// This checks to confirm that the Feature was successfully serialized into the required GherkinDocument and Pickles;
// if not, then this is disabled for this feature
// if true, then it checks with the broker to confirm that a listener/sink has been registered
Enabled = broker.Enabled;
if (!Enabled)
return;

var featureEnabled = featureStartedEvent.FeatureContext.FeatureInfo.FeatureCucumberMessages.Source == null ? false : true;

var featureProc = new FeatureEventProcessor
{
Name = featureName,
Enabled = enabled
Enabled = featureEnabled
};

// todo: need a lock around this
if (!featureProcessorsByFeatureName.TryAdd(featureName, featureProc))
{
// This feature has already been started by another thread (executing a different scenario)
Expand All @@ -78,14 +88,18 @@ private void FeatureStartedEventHandler(FeatureStartedEvent featureStartedEvent)
var traceListener = objectContainer.Resolve<ITraceListener>();
traceListener.WriteTestOutput($"Cucumber Message Publisher: FeatureStartedEventHandler: {featureName}");

if (!enabled)
if (!featureEnabled)
return;

ProcessEvent(featureStartedEvent, featureName);
}

private void FeatureFinishedEventHandler(FeatureFinishedEvent featureFinishedEvent)
{
if (!Enabled)
return;


var featureName = featureFinishedEvent.FeatureContext.FeatureInfo.Title;
var featureProcessor = featureProcessorsByFeatureName[featureName];

Expand Down Expand Up @@ -119,55 +133,93 @@ private void FeatureFinishedEventHandler(FeatureFinishedEvent featureFinishedEve

private void ScenarioStartedEventHandler(ScenarioStartedEvent scenarioStartedEvent)
{
if (!Enabled)
return;


var featureName = scenarioStartedEvent.FeatureContext.FeatureInfo.Title;
ProcessEvent(scenarioStartedEvent, featureName);
}

private void ScenarioFinishedEventHandler(ScenarioFinishedEvent scenarioFinishedEvent)
{
if (!Enabled)
return;


var featureName = scenarioFinishedEvent.FeatureContext.FeatureInfo.Title;
ProcessEvent(scenarioFinishedEvent, featureName);
}

private void StepStartedEventHandler(StepStartedEvent stepStartedEvent)
{
if (!Enabled)
return;


var featureName = stepStartedEvent.FeatureContext.FeatureInfo.Title;
ProcessEvent(stepStartedEvent, featureName);
}

private void StepFinishedEventHandler(StepFinishedEvent stepFinishedEvent)
{
if (!Enabled)
return;


var featureName = stepFinishedEvent.FeatureContext.FeatureInfo.Title;
ProcessEvent(stepFinishedEvent, featureName);
}

private void HookBindingStartedEventHandler(HookBindingStartedEvent hookBindingStartedEvent)
{
if (!Enabled)
return;


var featureName = hookBindingStartedEvent.ContextManager?.FeatureContext?.FeatureInfo?.Title;
ProcessEvent(hookBindingStartedEvent, featureName);
}

private void HookBindingFinishedEventHandler(HookBindingFinishedEvent hookBindingEvent)
{
if (!Enabled)
return;


var featureName = hookBindingEvent.ContextManager?.FeatureContext?.FeatureInfo?.Title;
ProcessEvent(hookBindingEvent, featureName);
}

private void AttachmentAddedEventHandler(AttachmentAddedEvent attachmentAddedEvent)
{
if (!Enabled)
return;


var featureName = attachmentAddedEvent.FeatureName;
ProcessEvent(attachmentAddedEvent, featureName);
}

private void OutputAddedEventHandler(OutputAddedEvent outputAddedEvent)
{
ProcessEvent(outputAddedEvent, outputAddedEvent.FeatureName);
if (!Enabled)
return;


ProcessEvent(outputAddedEvent, outputAddedEvent.FeatureName);
}


private void ProcessEvent(ExecutionEvent anEvent, string featureName)
{
if (!Enabled)
return;


var featureProcessor = featureProcessorsByFeatureName[featureName];
if (!featureProcessor.Enabled)
return;

featureProcessor.ProcessEvent(anEvent);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Cucumber.Messages;
using Io.Cucumber.Messages.Types;
using Microsoft.VisualStudio.TestPlatform.PlatformAbstractions;
using Newtonsoft.Json.Bson;
using Reqnroll;
using System.Reflection;

Expand All @@ -10,6 +11,23 @@ namespace CucumberMessages.CompatibilityTests
[TestClass]
public class CucumberCompatibilityTests : CucumberCompatibilityTestBase
{
[TestMethod]
public void NullTest()
{
// The purpose of this test is to confirm that when Cucumber Messages are turned off, the Cucumber Messages ecosystem does not cause any interference anywhere else

AddFeatureFile("""
Feature: Cucumber Messages Null Test
Scenario: Eating Cukes
When I eat 5 cukes
""");

AddPassingStepBinding("When");

ExecuteTests();

ShouldAllScenariosPass();
}
[TestMethod]
public void SmokeTest()
{
Expand Down

0 comments on commit eb3e257

Please sign in to comment.