|
| 1 | +package datadog.trace.common.writer; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 5 | +import static org.mockito.Mockito.verify; |
| 6 | + |
| 7 | +import datadog.trace.api.config.OtlpConfig; |
| 8 | +import datadog.trace.core.otlp.common.OtlpSender; |
| 9 | +import org.junit.jupiter.api.Test; |
| 10 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 11 | +import org.mockito.Mock; |
| 12 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 13 | + |
| 14 | +@ExtendWith(MockitoExtension.class) |
| 15 | +class OtlpWriterTest { |
| 16 | + |
| 17 | + @Mock OtlpSender sender; |
| 18 | + |
| 19 | + @Test |
| 20 | + void closeShutsDownSender() { |
| 21 | + OtlpWriter writer = OtlpWriter.builder().sender(sender).build(); |
| 22 | + writer.start(); |
| 23 | + |
| 24 | + writer.close(); |
| 25 | + |
| 26 | + verify(sender).shutdown(); |
| 27 | + } |
| 28 | + |
| 29 | + @Test |
| 30 | + void getApisIsEmpty() { |
| 31 | + OtlpWriter writer = OtlpWriter.builder().sender(sender).build(); |
| 32 | + writer.start(); |
| 33 | + try { |
| 34 | + assertTrue(writer.getApis().isEmpty()); |
| 35 | + } finally { |
| 36 | + writer.close(); |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + void builderRespectsInjectedSenderAcrossProtocols() { |
| 42 | + for (OtlpConfig.Protocol protocol : OtlpConfig.Protocol.values()) { |
| 43 | + OtlpWriter writer = OtlpWriter.builder().protocol(protocol).sender(sender).build(); |
| 44 | + writer.start(); |
| 45 | + writer.close(); |
| 46 | + } |
| 47 | + // One shutdown per writer we built. |
| 48 | + verify(sender, org.mockito.Mockito.times(OtlpConfig.Protocol.values().length)).shutdown(); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + void buildWithDefaultsDoesNotThrow() { |
| 53 | + // Exercises the default-path sender construction (real OtlpHttpSender) without |
| 54 | + // actually sending anything — start/close only. Guards against a default config |
| 55 | + // that would otherwise fail at construction time. |
| 56 | + assertDoesNotThrow( |
| 57 | + () -> { |
| 58 | + OtlpWriter writer = OtlpWriter.builder().build(); |
| 59 | + writer.start(); |
| 60 | + writer.close(); |
| 61 | + }); |
| 62 | + } |
| 63 | +} |
0 commit comments