Skip to content

Commit e1d420f

Browse files
committed
Step ID and last step start time cleanup in ReportPortalHook class
1 parent 2f9d7b1 commit e1d420f

File tree

3 files changed

+64
-49
lines changed

3 files changed

+64
-49
lines changed

src/main/java/com/epam/reportportal/karate/ReportPortalHook.java

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444
import org.slf4j.LoggerFactory;
4545

4646
import java.time.Instant;
47-
import java.time.temporal.ChronoUnit;
4847
import java.util.*;
4948
import java.util.concurrent.ConcurrentHashMap;
5049
import java.util.function.Supplier;
@@ -66,7 +65,7 @@ public class ReportPortalHook implements RuntimeHook {
6665
private final Map<String, Maybe<String>> backgroundIdMap = new ConcurrentHashMap<>();
6766
private final Map<String, ItemStatus> backgroundStatusMap = new ConcurrentHashMap<>();
6867
private final Map<String, Maybe<String>> stepIdMap = new ConcurrentHashMap<>();
69-
private final Map<Maybe<String>, Instant> stepStartTimeMap = new ConcurrentHashMap<>();
68+
private final Map<String, Instant> stepStartTimeMap = new ConcurrentHashMap<>();
7069
private final Set<Maybe<String>> innerFeatures = Collections.newSetFromMap(new ConcurrentHashMap<>());
7170
private volatile Thread shutDownHook;
7271

@@ -343,6 +342,7 @@ protected void embedAttachments(@Nonnull Maybe<String> itemId, @Nullable List<Em
343342
@Override
344343
public void afterScenario(ScenarioRuntime sr) {
345344
Maybe<String> scenarioId = scenarioIdMap.get(sr.scenario.getUniqueId());
345+
stepStartTimeMap.remove(sr.scenario.getUniqueId());
346346
finishBackground(null, sr);
347347

348348
if (scenarioId == null) {
@@ -370,20 +370,12 @@ public void afterScenario(ScenarioRuntime sr) {
370370
* Get step start time. To keep the steps order in case previous step startTime == current step startTime or
371371
* previous step startTime > current step startTime.
372372
*
373-
* @param stepId step ID.
373+
* @param scenarioUniqueId Karate's Scenario Unique ID
374374
* @return step new startTime in Instant format.
375375
*/
376376
@Nonnull
377-
private Instant getStepStartTime(@Nullable Maybe<String> stepId) {
378-
Instant currentStepStartTime = Instant.now();
379-
if (stepId == null || stepStartTimeMap.isEmpty()) {
380-
return currentStepStartTime;
381-
}
382-
Instant lastStepStartTime = stepStartTimeMap.get(stepId);
383-
if (lastStepStartTime.compareTo(currentStepStartTime) >= 0) {
384-
currentStepStartTime = lastStepStartTime.plus(1, ChronoUnit.MICROS);
385-
}
386-
return currentStepStartTime;
377+
private Instant getStepStartTime(@Nullable String scenarioUniqueId) {
378+
return ReportPortalUtils.getStepStartTime(scenarioUniqueId, stepStartTimeMap, launch.get().useMicroseconds());
387379
}
388380

389381
/**
@@ -396,8 +388,7 @@ private Instant getStepStartTime(@Nullable Maybe<String> stepId) {
396388
@Nonnull
397389
protected StartTestItemRQ buildStartStepRq(@Nonnull Step step, @Nonnull ScenarioRuntime sr) {
398390
StartTestItemRQ rq = ReportPortalUtils.buildStartStepRq(step, sr.scenario);
399-
Maybe<String> stepId = stepIdMap.get(sr.scenario.getUniqueId());
400-
Instant startTime = getStepStartTime(stepId);
391+
Instant startTime = getStepStartTime(sr.scenario.getUniqueId());
401392
rq.setStartTime(startTime);
402393
return rq;
403394
}
@@ -424,7 +415,6 @@ public boolean beforeStep(Step step, ScenarioRuntime sr) {
424415

425416
String scenarioId = sr.scenario.getUniqueId();
426417
Maybe<String> stepId = launch.get().startTestItem(background ? backgroundId : scenarioIdMap.get(scenarioId), stepRq);
427-
stepStartTimeMap.put(stepId, (Instant) stepRq.getStartTime());
428418
stepIdMap.put(scenarioId, stepId);
429419
ofNullable(stepRq.getParameters()).filter(params -> !params.isEmpty())
430420
.ifPresent(params -> sendLog(stepId, String.format(PARAMETERS_PATTERN, formatParametersAsTable(params)), LogLevel.INFO));
@@ -490,7 +480,7 @@ public void afterStep(StepResult stepResult, ScenarioRuntime sr) {
490480
}
491481

492482
sendStepResults(stepResult, sr);
493-
Maybe<String> stepId = stepIdMap.get(sr.scenario.getUniqueId());
483+
Maybe<String> stepId = stepIdMap.remove(sr.scenario.getUniqueId());
494484
if (stepId == null) {
495485
LOGGER.error("ERROR: Trying to finish unspecified step.");
496486
return;

src/main/java/com/epam/reportportal/karate/ReportPortalPublisher.java

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
import org.slf4j.LoggerFactory;
3636

3737
import java.time.Instant;
38-
import java.time.temporal.ChronoUnit;
3938
import java.util.HashMap;
4039
import java.util.List;
4140
import java.util.Map;
@@ -55,7 +54,7 @@ public class ReportPortalPublisher {
5554
protected final MemoizingSupplier<Launch> launch;
5655
private final Map<String, Maybe<String>> featureIdMap = new HashMap<>();
5756
private final Map<String, Maybe<String>> scenarioIdMap = new HashMap<>();
58-
private final Map<Maybe<String>, Instant> stepStartTimeMap = new HashMap<>();
57+
private final Map<String, Instant> stepStartTimeMap = new HashMap<>();
5958
private Maybe<String> backgroundId;
6059
private ItemStatus backgroundStatus;
6160
private Maybe<String> stepId;
@@ -164,7 +163,6 @@ public void finishFeature(FeatureResult featureResult) {
164163
finishStep(stepResult, scenarioResult);
165164
}
166165

167-
stepStartTimeMap.clear();
168166
finishScenario(scenarioResult);
169167
}
170168

@@ -220,6 +218,7 @@ public void finishScenario(ScenarioResult scenarioResult) {
220218

221219
FinishTestItemRQ rq = buildFinishScenarioRq(scenarioResult);
222220
Maybe<String> removedScenarioId = scenarioIdMap.remove(scenarioResult.getScenario().getName());
221+
stepStartTimeMap.remove(scenarioResult.getScenario().getUniqueId());
223222
//noinspection ReactiveStreamsUnusedPublisher
224223
launch.get().finishTestItem(removedScenarioId, rq);
225224
finishBackground(null, scenarioResult);
@@ -285,20 +284,12 @@ public void finishBackground(@Nullable StepResult stepResult, @Nonnull ScenarioR
285284
* Get step start time. To keep the steps order in case previous step startTime == current step startTime or
286285
* previous step startTime > current step startTime.
287286
*
288-
* @param stepId step ID.
287+
* @param scenarioUniqueId Karate's Scenario Unique ID
289288
* @return step new startTime in Instant format.
290289
*/
291-
private Instant getStepStartTime(@Nonnull Maybe<String> stepId) {
292-
Instant currentStepStartTime = Instant.now();
293-
294-
if (!stepStartTimeMap.isEmpty()) {
295-
Instant lastStepStartTime = stepStartTimeMap.get(stepId);
296-
297-
if (lastStepStartTime.compareTo(currentStepStartTime) >= 0) {
298-
currentStepStartTime = lastStepStartTime.plus(1, ChronoUnit.MICROS);
299-
}
300-
}
301-
return currentStepStartTime;
290+
@Nonnull
291+
private Instant getStepStartTime(@Nullable String scenarioUniqueId) {
292+
return ReportPortalUtils.getStepStartTime(scenarioUniqueId, stepStartTimeMap, launch.get().useMicroseconds());
302293
}
303294

304295
/**
@@ -311,7 +302,7 @@ private Instant getStepStartTime(@Nonnull Maybe<String> stepId) {
311302
@Nonnull
312303
protected StartTestItemRQ buildStartStepRq(@Nonnull StepResult stepResult, @Nonnull ScenarioResult scenarioResult) {
313304
StartTestItemRQ rq = ReportPortalUtils.buildStartStepRq(stepResult.getStep(), scenarioResult.getScenario());
314-
Instant startTime = getStepStartTime(stepId);
305+
Instant startTime = getStepStartTime(scenarioResult.getScenario().getUniqueId());
315306
rq.setStartTime(startTime);
316307
return rq;
317308
}
@@ -335,7 +326,6 @@ public void startStep(StepResult stepResult, ScenarioResult scenarioResult) {
335326
background && backgroundId != null ? backgroundId : scenarioIdMap.get(scenarioResult.getScenario().getName()),
336327
stepRq
337328
);
338-
stepStartTimeMap.put(stepId, (Instant) stepRq.getStartTime());
339329
ofNullable(stepRq.getParameters()).filter(params -> !params.isEmpty())
340330
.ifPresent(params -> sendLog(stepId, String.format(PARAMETERS_PATTERN, formatParametersAsTable(params)), LogLevel.INFO));
341331
ofNullable(step.getTable()).ifPresent(table -> sendLog(stepId, "Table:\n\n" + formatDataTable(table.getRows()), LogLevel.INFO));

src/main/java/com/epam/reportportal/karate/ReportPortalUtils.java

Lines changed: 50 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import org.slf4j.LoggerFactory;
4444

4545
import java.time.Instant;
46+
import java.time.temporal.ChronoUnit;
4647
import java.util.*;
4748
import java.util.regex.Pattern;
4849
import java.util.stream.Collectors;
@@ -472,21 +473,23 @@ public static void sendLog(Maybe<String> itemId, String message, LogLevel level)
472473
* @param embed Karate's Embed object
473474
*/
474475
public static void embedAttachment(@Nonnull Maybe<String> itemId, @Nonnull Embed embed) {
475-
ReportPortal.emitLog(itemId, id -> {
476-
SaveLogRQ rq = new SaveLogRQ();
477-
rq.setItemUuid(id);
478-
rq.setLevel(LogLevel.INFO.name());
479-
rq.setLogTime(Instant.now());
480-
rq.setMessage("Attachment: " + embed.getResourceType().contentType);
481-
482-
SaveLogRQ.File file = new SaveLogRQ.File();
483-
file.setName(embed.getFile().getName());
484-
file.setContent(embed.getBytes());
485-
file.setContentType(embed.getResourceType().contentType);
486-
rq.setFile(file);
487-
488-
return rq;
489-
});
476+
ReportPortal.emitLog(
477+
itemId, id -> {
478+
SaveLogRQ rq = new SaveLogRQ();
479+
rq.setItemUuid(id);
480+
rq.setLevel(LogLevel.INFO.name());
481+
rq.setLogTime(Instant.now());
482+
rq.setMessage("Attachment: " + embed.getResourceType().contentType);
483+
484+
SaveLogRQ.File file = new SaveLogRQ.File();
485+
file.setName(embed.getFile().getName());
486+
file.setContent(embed.getBytes());
487+
file.setContentType(embed.getResourceType().contentType);
488+
rq.setFile(file);
489+
490+
return rq;
491+
}
492+
);
490493
}
491494

492495
/**
@@ -539,4 +542,36 @@ public static String getInnerScenarioName(String name) {
539542
public static String getInnerFeatureName(String name) {
540543
return FEATURE_TAG + name;
541544
}
545+
546+
/**
547+
* Get step start time. To keep the steps order in case previous step startTime == current step startTime or
548+
* previous step startTime > current step startTime.
549+
*
550+
* @param scenarioUniqueId Karate's Scenario Unique ID, a key for stepStartTimeMap
551+
* @param stepStartTimeMap a holder for start times for every particular scenario
552+
* @param useMicroseconds if server supports microseconds
553+
* @return step new startTime in Instant format.
554+
*/
555+
public static Instant getStepStartTime(@Nullable String scenarioUniqueId, Map<String, Instant> stepStartTimeMap,
556+
boolean useMicroseconds) {
557+
Instant currentStepStartTime = Instant.now();
558+
if (scenarioUniqueId == null || stepStartTimeMap.isEmpty()) {
559+
stepStartTimeMap.put(scenarioUniqueId, currentStepStartTime);
560+
return currentStepStartTime;
561+
}
562+
Instant lastStepStartTime = stepStartTimeMap.get(scenarioUniqueId);
563+
if (lastStepStartTime == null) {
564+
stepStartTimeMap.put(scenarioUniqueId, currentStepStartTime);
565+
return currentStepStartTime;
566+
}
567+
if (lastStepStartTime.compareTo(currentStepStartTime) >= 0) {
568+
if (useMicroseconds) {
569+
currentStepStartTime = lastStepStartTime.plus(1, ChronoUnit.MICROS);
570+
} else {
571+
currentStepStartTime = lastStepStartTime.plus(1, ChronoUnit.MILLIS);
572+
}
573+
}
574+
stepStartTimeMap.put(scenarioUniqueId, currentStepStartTime);
575+
return currentStepStartTime;
576+
}
542577
}

0 commit comments

Comments
 (0)