Skip to content

Commit

Permalink
SerenityReporterParallel: improve integration of custom listeners (#3599
Browse files Browse the repository at this point in the history
)
  • Loading branch information
cliviu authored Jan 17, 2025
1 parent 8423087 commit c174081
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
package net.serenitybdd.core.reports;

import net.thucydides.core.steps.session.PlaybackSession;
import net.thucydides.model.domain.ReportData;
import net.thucydides.model.domain.TestOutcome;
import net.thucydides.model.domain.TestResult;
import net.thucydides.model.domain.TestStep;
import net.thucydides.core.steps.StepEventBus;
import net.thucydides.core.steps.session.TestSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.util.Optional;

import static net.thucydides.model.ThucydidesSystemProperty.SERENITY_REPORT_ENCODING;

public class ReportDataSaver implements WithTitle, AndContent, FromFile {

private final StepEventBus eventBus;
private static final Logger LOGGER = LoggerFactory.getLogger(ReportDataSaver.class);
private StepEventBus eventBus;
private String title;
private boolean fileIsDownloadable = false;
private boolean isEvidence = false;
Expand Down Expand Up @@ -46,6 +49,11 @@ private void addReportContent(ReportData reportData) {
}

public void doAddContents(ReportData reportData) {
if (PlaybackSession.isSessionStarted()) {
StepEventBus playBackStepEventBus = PlaybackSession.getTestSessionContext().getStepEventBus();
LOGGER.debug("SRP:replace event bus {} with playback event bus {}", eventBus , playBackStepEventBus );
eventBus = playBackStepEventBus;
}
eventBus.getBaseStepListener().latestTestOutcome().ifPresent(
outcome -> currentStepOrBackgroundIn(outcome)
.withReportData(reportData)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package net.thucydides.core.steps.session;

import net.thucydides.core.steps.StepEventBus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;



public class PlaybackSession {

private static final Logger LOGGER = LoggerFactory.getLogger(PlaybackSession.class);

private static final ThreadLocal<PlaybackSessionContext> sessionContext = ThreadLocal.withInitial(PlaybackSessionContext::new);

public static void startSession(String sessionId, StepEventBus stepEventBus) {
sessionContext.get().getSessionStarted().set(true);
sessionContext.get().setSessionId(sessionId);
sessionContext.get().setStepEventBus(stepEventBus);
LOGGER.debug("SRP:PlaybackSessionStart: id: {} " , sessionId);
}

public static void closeSession() {
sessionContext.get().getSessionStarted().set(false);
LOGGER.debug("SRP:PlaybackSessionEnd: id: {} ", sessionContext.get().getSessionId());
}

public static PlaybackSessionContext getTestSessionContext() {
return sessionContext.get();
}


public static boolean isSessionStarted() {
return sessionContext.get().getSessionStarted().get();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package net.thucydides.core.steps.session;

import net.thucydides.core.steps.StepEventBus;
import java.util.concurrent.atomic.AtomicBoolean;

public class PlaybackSessionContext {

private String sessionId;

private StepEventBus stepEventBus;

private final AtomicBoolean sessionStarted = new AtomicBoolean(false);

public AtomicBoolean getSessionStarted() {
return sessionStarted;
}

public void setSessionId(String sessionId) {
this.sessionId = sessionId;
}

public String getSessionId() {
return sessionId;
}

public StepEventBus getStepEventBus() {
return stepEventBus;
}

public void setStepEventBus(StepEventBus stepEventBus) {
this.stepEventBus = stepEventBus;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import io.cucumber.messages.types.Tag;
import io.cucumber.plugin.event.TestCase;
import io.cucumber.plugin.event.TestStep;
import net.thucydides.core.steps.session.PlaybackSession;
import net.thucydides.model.domain.DataTable;
import net.thucydides.model.domain.DataTableRow;
import net.thucydides.model.domain.TestTag;
Expand Down Expand Up @@ -373,15 +374,20 @@ public void storeAllStepEventBusEventsForLine(int line, TestCase testCase) {
*/
public synchronized void playAllTestEvents() {
LOGGER.debug("SRP:playAllTestEvents for URI " + scenarioContextURI + "--" + allTestEventsByLine);
for (var entry : allTestEventsByLine.entrySet()) {
try {
replayAllTestCaseEventsForLine(entry.getKey(), entry.getValue());
} catch (Throwable exception) {
LOGGER.error("An unrecoverable error occurred during test execution: " + exception.getMessage(), exception);
exception.printStackTrace();
recordUnexpectedFailure(new SerenityManagedException("An unrecoverable error occurred during test execution: " + exception.getMessage(),
exception));
PlaybackSession.startSession("ScenarioContextParallelPlayback_" + scenarioContextURI ,stepEventBus());
try {
for (var entry : allTestEventsByLine.entrySet()) {
try {
replayAllTestCaseEventsForLine(entry.getKey(), entry.getValue());
} catch (Throwable exception) {
LOGGER.error("An unrecoverable error occurred during test execution: " + exception.getMessage(), exception);
exception.printStackTrace();
recordUnexpectedFailure(new SerenityManagedException("An unrecoverable error occurred during test execution: " + exception.getMessage(),
exception));
}
}
} finally {
PlaybackSession.closeSession();
}
clearEventBus();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public class SerenityReporterParallel implements Plugin, ConcurrentEventListener
private LineFilters lineFilters;


private static final Logger LOGGER = LoggerFactory.getLogger(SerenityReporter.class);
private static final Logger LOGGER = LoggerFactory.getLogger(SerenityReporterParallel.class);

private final Set<URI> contextURISet = new CopyOnWriteArraySet<>();

Expand Down

0 comments on commit c174081

Please sign in to comment.