|
18 | 18 | */ |
19 | 19 | package co.elastic.apm.agent.impl; |
20 | 20 |
|
| 21 | +import co.elastic.apm.agent.AbstractInstrumentationTest; |
21 | 22 | import co.elastic.apm.agent.MockReporter; |
22 | 23 | import co.elastic.apm.agent.configuration.CoreConfiguration; |
23 | 24 | import co.elastic.apm.agent.configuration.SpyConfiguration; |
24 | 25 | import co.elastic.apm.agent.impl.metadata.MetaData; |
25 | 26 | import co.elastic.apm.agent.objectpool.TestObjectPoolFactory; |
26 | 27 | import co.elastic.apm.agent.report.ApmServerClient; |
| 28 | +import org.junit.jupiter.api.AfterAll; |
27 | 29 | import org.junit.jupiter.api.AfterEach; |
| 30 | +import org.junit.jupiter.api.BeforeAll; |
28 | 31 | import org.junit.jupiter.api.Test; |
29 | | -import org.stagemonitor.configuration.ConfigurationOption; |
30 | 32 | import org.stagemonitor.configuration.ConfigurationRegistry; |
31 | 33 |
|
32 | 34 |
|
| 35 | +import java.io.IOException; |
| 36 | + |
33 | 37 | import static org.assertj.core.api.Assertions.assertThat; |
34 | 38 | import static org.mockito.Mockito.mock; |
35 | 39 | import static org.mockito.Mockito.when; |
36 | 40 |
|
37 | | -class DropUnsampledTransactionsTest { |
| 41 | +class DropUnsampledTransactionsTest extends AbstractInstrumentationTest { |
38 | 42 |
|
39 | 43 | private static ApmServerClient apmServerClient = mock(ApmServerClient.class); |
40 | 44 |
|
41 | | - private MockReporter reporter = new MockReporter(); |
| 45 | + private static MockReporter reporter = new MockReporter(); |
42 | 46 |
|
43 | | - private ElasticApmTracer tracer; |
| 47 | + private static ElasticApmTracer tracer; |
44 | 48 |
|
45 | | - private void startTracer(double sampleRate) { |
| 49 | + @BeforeAll |
| 50 | + static void startTracer() { |
46 | 51 | ConfigurationRegistry configurationRegistry = SpyConfiguration.createSpyConfig(); |
47 | | - when(configurationRegistry.getConfig(CoreConfiguration.class).getSampleRate()).thenReturn(ConfigurationOption.doubleOption().buildWithDefault(sampleRate)); |
48 | 52 | tracer = new ElasticApmTracer(configurationRegistry, reporter, new TestObjectPoolFactory(), apmServerClient, "ephemeralId", MetaData.create(configurationRegistry, "ephemeralId")); |
49 | 53 | tracer.start(false); |
50 | 54 | } |
51 | 55 |
|
52 | | - @AfterEach |
53 | | - void stopTracer() { |
| 56 | + @AfterAll |
| 57 | + static void stopTracer() { |
54 | 58 | tracer.stop(); |
55 | 59 | } |
56 | 60 |
|
| 61 | + @AfterEach |
| 62 | + void resetReporter() { |
| 63 | + reporter.reset(); |
| 64 | + } |
| 65 | + |
57 | 66 | @Test |
58 | | - void whenTheAPMServerSupportsKeepingUnsampledTransactionsUnsampledTransactionsShouldBeReported() { |
| 67 | + void whenTheAPMServerSupportsKeepingUnsampledTransactionsUnsampledTransactionsShouldBeReported() throws IOException { |
59 | 68 | when(apmServerClient.supportsKeepingUnsampledTransaction()).thenReturn(true); |
60 | | - startTracer(0.0); |
| 69 | + tracer.getConfig(CoreConfiguration.class).getSampleRate().update(0.0, SpyConfiguration.CONFIG_SOURCE_NAME); |
61 | 70 |
|
62 | 71 | tracer.startRootTransaction(null).end(); |
63 | 72 |
|
64 | 73 | assertThat(reporter.getTransactions().size()).isEqualTo(1); |
65 | 74 | } |
66 | 75 |
|
67 | 76 | @Test |
68 | | - void whenTheAPMServerSupportsKeepingUnsampledTransactionsSampledTransactionsShouldBeReported() { |
| 77 | + void whenTheAPMServerSupportsKeepingUnsampledTransactionsSampledTransactionsShouldBeReported() throws IOException { |
69 | 78 | when(apmServerClient.supportsKeepingUnsampledTransaction()).thenReturn(true); |
70 | | - startTracer(1.0); |
| 79 | + tracer.getConfig(CoreConfiguration.class).getSampleRate().update(1.0, SpyConfiguration.CONFIG_SOURCE_NAME); |
71 | 80 |
|
72 | 81 | tracer.startRootTransaction(null).end(); |
73 | 82 |
|
74 | 83 | assertThat(reporter.getTransactions().size()).isEqualTo(1); |
75 | 84 | } |
76 | 85 |
|
77 | 86 | @Test |
78 | | - void whenTheAPMServerDoesNotSupportsKeepingUnsampledTransactionsUnsampledTransactionsShouldNotBeReported() { |
| 87 | + void whenTheAPMServerDoesNotSupportsKeepingUnsampledTransactionsUnsampledTransactionsShouldNotBeReported() throws IOException { |
79 | 88 | when(apmServerClient.supportsKeepingUnsampledTransaction()).thenReturn(false); |
80 | | - startTracer(0.0); |
| 89 | + tracer.getConfig(CoreConfiguration.class).getSampleRate().update(0.0, SpyConfiguration.CONFIG_SOURCE_NAME); |
81 | 90 |
|
82 | 91 | tracer.startRootTransaction(null).end(); |
83 | 92 |
|
84 | 93 | assertThat(reporter.getTransactions().size()).isEqualTo(0); |
85 | 94 | } |
86 | 95 |
|
87 | 96 | @Test |
88 | | - void whenTheAPMServerDoesNotSupportsKeepingUnsampledTransactionsSampledTransactionsShouldBeReported() { |
| 97 | + void whenTheAPMServerDoesNotSupportsKeepingUnsampledTransactionsSampledTransactionsShouldBeReported() throws IOException { |
89 | 98 | when(apmServerClient.supportsKeepingUnsampledTransaction()).thenReturn(false); |
90 | | - startTracer(1.0); |
| 99 | + tracer.getConfig(CoreConfiguration.class).getSampleRate().update(1.0, SpyConfiguration.CONFIG_SOURCE_NAME); |
91 | 100 |
|
92 | 101 | tracer.startRootTransaction(null).end(); |
93 | 102 |
|
|
0 commit comments