|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
1 | 6 | package io.opentelemetry.javaagent.instrumentation.rediscala; |
2 | 7 |
|
3 | | -class RediscalaClientTest { |
| 8 | +import static io.opentelemetry.api.trace.SpanKind.CLIENT; |
| 9 | +import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo; |
| 10 | +import static org.assertj.core.api.Assertions.assertThat; |
| 11 | +import static org.awaitility.Awaitility.await; |
| 12 | + |
| 13 | +import io.opentelemetry.instrumentation.testing.internal.AutoCleanupExtension; |
| 14 | +import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension; |
| 15 | +import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension; |
| 16 | +import io.opentelemetry.semconv.incubating.DbIncubatingAttributes; |
| 17 | +import org.junit.jupiter.api.AfterAll; |
| 18 | +import org.junit.jupiter.api.BeforeAll; |
| 19 | +import org.junit.jupiter.api.Test; |
| 20 | +import org.junit.jupiter.api.extension.RegisterExtension; |
| 21 | +import org.testcontainers.containers.GenericContainer; |
| 22 | +import org.testcontainers.shaded.org.apache.commons.lang3.tuple.Pair; |
| 23 | +import redis.ByteStringDeserializer; |
| 24 | +import redis.ByteStringDeserializer$; |
| 25 | +import redis.ByteStringSerializer; |
| 26 | +import redis.ByteStringSerializer$; |
| 27 | +import redis.RedisClient; |
| 28 | +import redis.RedisDispatcher; |
| 29 | +import scala.Option; |
| 30 | +import scala.concurrent.Future; |
| 31 | + |
| 32 | +public class RediscalaClientTest { |
| 33 | + |
| 34 | + @RegisterExtension |
| 35 | + static final InstrumentationExtension testing = AgentInstrumentationExtension.create(); |
| 36 | + |
| 37 | + @RegisterExtension static final AutoCleanupExtension cleanup = AutoCleanupExtension.create(); |
| 38 | + |
| 39 | + private static GenericContainer<?> redisServer; |
| 40 | + |
| 41 | + private static String host; |
| 42 | + private static int port; |
| 43 | + private static Object system; |
| 44 | + private static RedisClient redisClient; |
| 45 | + |
| 46 | + @BeforeAll |
| 47 | + static void setUp() throws Exception { |
| 48 | + redisServer = new GenericContainer<>("redis:6.2.3-alpine").withExposedPorts(6379); |
| 49 | + redisServer.start(); |
| 50 | + cleanup.deferCleanup(redisServer::stop); |
| 51 | + |
| 52 | + host = redisServer.getHost(); |
| 53 | + port = redisServer.getMappedPort(6379); |
| 54 | + |
| 55 | + try { |
| 56 | + Class<?> clazz = Class.forName("akka.actor.ActorSystem"); |
| 57 | + system = clazz.getMethod("create").invoke(null); |
| 58 | + } catch (ClassNotFoundException exception) { |
| 59 | + Class<?> clazz = Class.forName("org.apache.pekko.actor.ActorSystem"); |
| 60 | + system = clazz.getMethod("create").invoke(null); |
| 61 | + } |
| 62 | + |
| 63 | + try { |
| 64 | + RedisClient.class.getMethod("username"); |
| 65 | + redisClient = |
| 66 | + (RedisClient) |
| 67 | + RedisClient.class.getConstructors()[0].newInstance( |
| 68 | + host, |
| 69 | + port, |
| 70 | + Option.apply(null), |
| 71 | + Option.apply(null), |
| 72 | + Option.apply(null), |
| 73 | + "RedisClient", |
| 74 | + Option.apply(null), |
| 75 | + system, |
| 76 | + new RedisDispatcher("rediscala.rediscala-client-worker-dispatcher")); |
| 77 | + } catch (Exception e) { |
| 78 | + redisClient = |
| 79 | + (RedisClient) |
| 80 | + RedisClient.class.getConstructors()[0].newInstance( |
| 81 | + host, |
| 82 | + port, |
| 83 | + Option.apply(null), |
| 84 | + Option.apply(null), |
| 85 | + "RedisClient", |
| 86 | + Option.apply(null), |
| 87 | + system, |
| 88 | + new RedisDispatcher("rediscala.rediscala-client-worker-dispatcher")); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + @AfterAll |
| 93 | + static void tearDown() throws Exception { |
| 94 | + if (system != null) { |
| 95 | + system.getClass().getMethod("terminate").invoke(system); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + void testGetCommand() throws Exception { |
| 101 | + Pair<Future<Object>, Future<Option<String>>> result = |
| 102 | + testing.runWithSpan( |
| 103 | + "parent", |
| 104 | + () -> { |
| 105 | + ByteStringSerializer<String> serializer = ByteStringSerializer$.MODULE$.String(); |
| 106 | + ByteStringDeserializer<String> deserializer = |
| 107 | + ByteStringDeserializer$.MODULE$.String(); |
| 108 | + Future<Object> writeFuture = |
| 109 | + redisClient.set( |
| 110 | + "bar", |
| 111 | + "baz", |
| 112 | + Option.apply(null), |
| 113 | + Option.apply(null), |
| 114 | + false, |
| 115 | + false, |
| 116 | + serializer); |
| 117 | + Future<Option<String>> valueFuture = redisClient.get("bar", deserializer); |
| 118 | + return Pair.of(writeFuture, valueFuture); |
| 119 | + }); |
| 120 | + |
| 121 | + await().atMost(java.time.Duration.ofSeconds(3)).until(() -> result.getLeft().isCompleted()); |
| 122 | + await().atMost(java.time.Duration.ofSeconds(3)).until(() -> result.getRight().isCompleted()); |
| 123 | + assertThat(result.getLeft().value().get().get()).isEqualTo(true); |
| 124 | + assertThat(result.getRight().value().get().get().get()).isEqualTo("baz"); |
| 125 | + |
| 126 | + testing.waitAndAssertTraces( |
| 127 | + trace -> |
| 128 | + trace.hasSpansSatisfyingExactly( |
| 129 | + span -> span.hasName("parent").hasNoParent(), |
| 130 | + span -> |
| 131 | + span.hasName("SET") |
| 132 | + .hasKind(CLIENT) |
| 133 | + .hasParent(trace.getSpan(0)) |
| 134 | + .hasAttributesSatisfying( |
| 135 | + equalTo(DbIncubatingAttributes.DB_SYSTEM, "redis"), |
| 136 | + equalTo(DbIncubatingAttributes.DB_OPERATION_NAME, "SET")), |
| 137 | + span -> |
| 138 | + span.hasName("GET") |
| 139 | + .hasKind(CLIENT) |
| 140 | + .hasParent(trace.getSpan(1)) |
| 141 | + .hasAttributesSatisfying( |
| 142 | + equalTo(DbIncubatingAttributes.DB_SYSTEM, "redis"), |
| 143 | + equalTo(DbIncubatingAttributes.DB_OPERATION_NAME, "GET")))); |
| 144 | + } |
| 145 | + |
| 146 | + @Test |
| 147 | + public void testSetCommand() throws Exception { |
| 148 | + ByteStringSerializer<String> serializer = ByteStringSerializer$.MODULE$.String(); |
4 | 149 |
|
| 150 | + Future<Object> value = |
| 151 | + testing.runWithSpan( |
| 152 | + "parent", |
| 153 | + () -> |
| 154 | + redisClient.set( |
| 155 | + "foo", |
| 156 | + "bar", |
| 157 | + Option.apply(null), |
| 158 | + Option.apply(null), |
| 159 | + false, |
| 160 | + false, |
| 161 | + serializer)); |
| 162 | + await().atMost(java.time.Duration.ofSeconds(3)).until(value::isCompleted); |
| 163 | + assertThat(value.value().get().get()).isEqualTo(true); |
| 164 | + testing.waitAndAssertTraces( |
| 165 | + trace -> |
| 166 | + trace.hasSpansSatisfyingExactly( |
| 167 | + span -> span.hasName("parent").hasNoParent(), |
| 168 | + span -> |
| 169 | + span.hasName("SET") |
| 170 | + .hasKind(CLIENT) |
| 171 | + .hasParent(trace.getSpan(0)) |
| 172 | + .hasAttributesSatisfying( |
| 173 | + equalTo(DbIncubatingAttributes.DB_SYSTEM, "redis"), |
| 174 | + equalTo(DbIncubatingAttributes.DB_OPERATION_NAME, "SET")))); |
| 175 | + } |
5 | 176 | } |
0 commit comments