### Question:
ZipkinSocketContextEmitter implements ContextEmitter<Map<String, String>, C> { @Override public void emit(ClientCallContext ctx, @Nonnull Map<String, String> additionalData, @Nullable C ignore) { RequestUUID requestUUID = ctx.getRequestUUID(); if (requestUUID instanceof ZipkinRequestUUID) { ZipkinRequestUUID zipkinRequestUUID = (ZipkinRequestUUID) requestUUID; if (zipkinRequestUUID.isZipkinTracingEnabled()) { ZipkinData zipkinData = zipkinRequestUUID.getZipkinData(); appendZipkinHeaders(additionalData, zipkinData); } else { additionalData.put(ZipkinKeys.SAMPLED, ZipkinKeys.DO_NOT_SAMPLE_VALUE); } } } ZipkinSocketContextEmitter(CompoundContextEmitter<Map<String, String>, C> compoundContextEmitter); @Override void emit(ClientCallContext ctx, @Nonnull Map<String, String> additionalData, @Nullable C ignore);  }### Answer:
@Test public void emit_WhenZipkinTracingIsNotEnabled_ShouldDisableSamplingForTheEntireRequestChain() { Map<String, String> additionalData = Maps.newHashMap(); Map.Entry<String, String> expectedHeader = new AbstractMap.SimpleEntry<>(ZipkinKeys.SAMPLED, ZipkinKeys.DO_NOT_SAMPLE_VALUE); victim = new ZipkinSocketContextEmitter(compoundContextEmitter); when(ctx.traceLoggingEnabled()).thenReturn(false); when(zipkinRequestUUID.isZipkinTracingEnabled()).thenReturn(false); victim.emit(ctx, additionalData, null); assertEquals(1, additionalData.size()); assertTrue(additionalData.containsKey(expectedHeader.getKey())); assertEquals(additionalData.get(expectedHeader.getKey()), expectedHeader.getValue()); }
@Test public void emit_WhenZipkinTracingIsEnabledOnNonFlaggedOriginalRequest_ShouldAppendZipkinHeaders() { Map<String, String> additionalData = Maps.newHashMap(); Map<String, String> expectedZipkinHeaders = createZipkinHeaders(traceId, spanId, null, null); victim = new ZipkinSocketContextEmitter(compoundContextEmitter); when(ctx.traceLoggingEnabled()).thenReturn(false); when(zipkinRequestUUID.isZipkinTracingEnabled()).thenReturn(true); when(zipkinRequestUUID.getZipkinData()).thenReturn(zipkinData); when(zipkinData.getTraceId()).thenReturn(traceId); when(zipkinData.getSpanId()).thenReturn(spanId); when(zipkinData.getParentSpanId()).thenReturn(null); when(zipkinData.getFlags()).thenReturn(null); victim.emit(ctx, additionalData, null); assertEquals(3, additionalData.size()); assertTrue(additionalData.entrySet().containsAll(expectedZipkinHeaders.entrySet())); }
@Test public void emit_WhenZipkinTracingIsEnabledOnChildRequest_ShouldAppendZipkinHeaders() { Map<String, String> additionalData = Maps.newHashMap(); Map<String, String> expectedZipkinHeaders = createZipkinHeaders(traceId, spanId, parentSpanId, flags); victim = new ZipkinSocketContextEmitter(compoundContextEmitter); when(ctx.traceLoggingEnabled()).thenReturn(false); when(zipkinRequestUUID.isZipkinTracingEnabled()).thenReturn(true); when(zipkinRequestUUID.getZipkinData()).thenReturn(zipkinData); when(zipkinData.getTraceId()).thenReturn(traceId); when(zipkinData.getSpanId()).thenReturn(spanId); when(zipkinData.getParentSpanId()).thenReturn(parentSpanId); when(zipkinData.getFlags()).thenReturn(flags); victim.emit(ctx, additionalData, null); assertEquals(5, additionalData.size()); assertTrue(additionalData.entrySet().containsAll(expectedZipkinHeaders.entrySet())); }