4444import org .slf4j .LoggerFactory ;
4545
4646import java .time .Instant ;
47- import java .time .temporal .ChronoUnit ;
4847import java .util .*;
4948import java .util .concurrent .ConcurrentHashMap ;
5049import 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 ;
0 commit comments