KermossTracer { public Span startGtxSpan(String name, String gtx, String traceId, Span span) { if (span == null || !hasGtx(span)) { Span newSpan = spanForId(traceId, span); Span newGlobalSpan = tracer.createSpan(name, newSpan); newGlobalSpan.tag("kermoss.gtx", gtx); if (span != null) { span.tags().forEach((k, v) -> { newGlobalSpan.tag(k, v); }); } publisher.publishEvent(new GtxSpanStarted()); log.info("Open new Span" + newGlobalSpan); return newGlobalSpan; } return span; }  @TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT) void closeSpan(GtxSpanStarted gtxSpanStarted); @TransactionalEventListener(phase = TransactionPhase.AFTER_ROLLBACK) void closeSpanE(GtxSpanStarted gtxSpanStarted); Span startGtxSpan(String name, String gtx, String traceId, Span span); Span startGtxSpan(GlobalTransaction gt, Span span); Span startNestedSpan(String name, String event, Span span); void closeSpan(Span span); Span currentSpan();  }
@Test public void testStartGtxSpanWhenSpanNotNullAndHasNoGtx() { final String name = "name"; final String gtx = "gtx"; final String traceId = "4e30f7340b3fb631"; final Span span = mock(Span.class); final Span newGlobalSpan = mock(Span.class); when(mockTracer.createSpan(anyString(), any(Span.class))).thenReturn(newGlobalSpan); final Span result = kermossTracerUnderTest.startGtxSpan(name, gtx, traceId, span); verify(newGlobalSpan, atLeastOnce()).tag(anyString(), anyString()); verify(mockPublisher).publishEvent(any(GtxSpanStarted.class)); assertEquals(result, newGlobalSpan); }
@Test public void testStartGtxSpanWhenSpanNotNullAndHasGtx() { final String name = "name"; final String gtx = "gtx"; final String traceId = "4e30f7340b3fb631"; final Span span = mock(Span.class, RETURNS_DEEP_STUBS); final Span newGlobalSpan = mock(Span.class); when(span.tags().containsKey("kermoss.gtx")).thenReturn(true); final Span result = kermossTracerUnderTest.startGtxSpan(name, gtx, traceId, span); verify(newGlobalSpan, never()).tag(anyString(), anyString()); verify(mockPublisher, never()).publishEvent(any(GtxSpanStarted.class)); assertEquals(result, span); }
@Test public void testStartGtxSpanWhenSpanIsNull() { final String name = "name"; final String gtx = "gtx"; final String traceId = "4e30f7340b3fb631"; final Span span = null; final Span newGlobalSpan = mock(Span.class); when(mockTracer.createSpan(anyString(), any(Span.class))).thenReturn(newGlobalSpan); final Span result = kermossTracerUnderTest.startGtxSpan(name, gtx, traceId, span); verify(newGlobalSpan, times(1)).tag(anyString(), anyString()); verify(mockPublisher).publishEvent(any(GtxSpanStarted.class)); assertEquals(result, newGlobalSpan); }
@Test public void testStartGtxSpanWhenGlobalTransactionHasParent() { final GlobalTransaction gt = mock(GlobalTransaction.class); final Span span = mock(Span.class); ArgumentCaptor<String> name = ArgumentCaptor.forClass(String.class); doReturn(span).when(kermossTracerUnderTest).startGtxSpan(anyString(), anyString(), anyString(), any(Span.class)); when(gt.getParent()).thenReturn("gtParent"); kermossTracerUnderTest.startGtxSpan(gt, span); verify(kermossTracerUnderTest).startGtxSpan(name.capture(), anyString(), anyString(), any(Span.class)); assertEquals(name.getValue(), "In the child globalTransaction"); }
@Test public void testStartGtxSpanWhenGlobalTransactionHasNoParent() { final GlobalTransaction gt = mock(GlobalTransaction.class); final Span span = mock(Span.class); ArgumentCaptor<String> name = ArgumentCaptor.forClass(String.class); doReturn(span).when(kermossTracerUnderTest).startGtxSpan(anyString(), anyString(), anyString(), any(Span.class)); when(gt.getParent()).thenReturn(null); kermossTracerUnderTest.startGtxSpan(gt, span); verify(kermossTracerUnderTest).startGtxSpan(name.capture(), anyString(), anyString(), any(Span.class)); assertEquals(name.getValue(),"In the parent globalTransaction"); }