Skip to content

Commit 162e387

Browse files
committed
Try to improve tests
1 parent 94ed653 commit 162e387

File tree

1 file changed

+25
-16
lines changed

1 file changed

+25
-16
lines changed

apm-agent-core/src/test/java/co/elastic/apm/agent/impl/DropUnsampledTransactionsTest.java

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,76 +18,85 @@
1818
*/
1919
package co.elastic.apm.agent.impl;
2020

21+
import co.elastic.apm.agent.AbstractInstrumentationTest;
2122
import co.elastic.apm.agent.MockReporter;
2223
import co.elastic.apm.agent.configuration.CoreConfiguration;
2324
import co.elastic.apm.agent.configuration.SpyConfiguration;
2425
import co.elastic.apm.agent.impl.metadata.MetaData;
2526
import co.elastic.apm.agent.objectpool.TestObjectPoolFactory;
2627
import co.elastic.apm.agent.report.ApmServerClient;
28+
import org.junit.jupiter.api.AfterAll;
2729
import org.junit.jupiter.api.AfterEach;
30+
import org.junit.jupiter.api.BeforeAll;
2831
import org.junit.jupiter.api.Test;
29-
import org.stagemonitor.configuration.ConfigurationOption;
3032
import org.stagemonitor.configuration.ConfigurationRegistry;
3133

3234

35+
import java.io.IOException;
36+
3337
import static org.assertj.core.api.Assertions.assertThat;
3438
import static org.mockito.Mockito.mock;
3539
import static org.mockito.Mockito.when;
3640

37-
class DropUnsampledTransactionsTest {
41+
class DropUnsampledTransactionsTest extends AbstractInstrumentationTest {
3842

3943
private static ApmServerClient apmServerClient = mock(ApmServerClient.class);
4044

41-
private MockReporter reporter = new MockReporter();
45+
private static MockReporter reporter = new MockReporter();
4246

43-
private ElasticApmTracer tracer;
47+
private static ElasticApmTracer tracer;
4448

45-
private void startTracer(double sampleRate) {
49+
@BeforeAll
50+
static void startTracer() {
4651
ConfigurationRegistry configurationRegistry = SpyConfiguration.createSpyConfig();
47-
when(configurationRegistry.getConfig(CoreConfiguration.class).getSampleRate()).thenReturn(ConfigurationOption.doubleOption().buildWithDefault(sampleRate));
4852
tracer = new ElasticApmTracer(configurationRegistry, reporter, new TestObjectPoolFactory(), apmServerClient, "ephemeralId", MetaData.create(configurationRegistry, "ephemeralId"));
4953
tracer.start(false);
5054
}
5155

52-
@AfterEach
53-
void stopTracer() {
56+
@AfterAll
57+
static void stopTracer() {
5458
tracer.stop();
5559
}
5660

61+
@AfterEach
62+
void resetReporter() {
63+
reporter.reset();
64+
}
65+
5766
@Test
58-
void whenTheAPMServerSupportsKeepingUnsampledTransactionsUnsampledTransactionsShouldBeReported() {
67+
void whenTheAPMServerSupportsKeepingUnsampledTransactionsUnsampledTransactionsShouldBeReported() throws IOException {
5968
when(apmServerClient.supportsKeepingUnsampledTransaction()).thenReturn(true);
60-
startTracer(0.0);
69+
tracer.getConfig(CoreConfiguration.class).getSampleRate().update(0.0, SpyConfiguration.CONFIG_SOURCE_NAME);
6170

6271
tracer.startRootTransaction(null).end();
6372

6473
assertThat(reporter.getTransactions().size()).isEqualTo(1);
6574
}
6675

6776
@Test
68-
void whenTheAPMServerSupportsKeepingUnsampledTransactionsSampledTransactionsShouldBeReported() {
77+
void whenTheAPMServerSupportsKeepingUnsampledTransactionsSampledTransactionsShouldBeReported() throws IOException {
6978
when(apmServerClient.supportsKeepingUnsampledTransaction()).thenReturn(true);
70-
startTracer(1.0);
79+
tracer.getConfig(CoreConfiguration.class).getSampleRate().update(1.0, SpyConfiguration.CONFIG_SOURCE_NAME);
7180

7281
tracer.startRootTransaction(null).end();
7382

7483
assertThat(reporter.getTransactions().size()).isEqualTo(1);
7584
}
7685

7786
@Test
78-
void whenTheAPMServerDoesNotSupportsKeepingUnsampledTransactionsUnsampledTransactionsShouldNotBeReported() {
87+
void whenTheAPMServerDoesNotSupportsKeepingUnsampledTransactionsUnsampledTransactionsShouldNotBeReported() throws IOException {
7988
when(apmServerClient.supportsKeepingUnsampledTransaction()).thenReturn(false);
80-
startTracer(0.0);
89+
tracer.getConfig(CoreConfiguration.class).getSampleRate().update(0.0, SpyConfiguration.CONFIG_SOURCE_NAME);
8190

8291
tracer.startRootTransaction(null).end();
8392

8493
assertThat(reporter.getTransactions().size()).isEqualTo(0);
8594
}
8695

8796
@Test
88-
void whenTheAPMServerDoesNotSupportsKeepingUnsampledTransactionsSampledTransactionsShouldBeReported() {
97+
void whenTheAPMServerDoesNotSupportsKeepingUnsampledTransactionsSampledTransactionsShouldBeReported() throws IOException {
8998
when(apmServerClient.supportsKeepingUnsampledTransaction()).thenReturn(false);
90-
startTracer(1.0);
99+
tracer.getConfig(CoreConfiguration.class).getSampleRate().update(1.0, SpyConfiguration.CONFIG_SOURCE_NAME);
91100

92101
tracer.startRootTransaction(null).end();
93102

0 commit comments

Comments
 (0)