-
Notifications
You must be signed in to change notification settings - Fork 881
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
convert redisscala tests from groovy to java #12553
Changes from 7 commits
5a143b8
e644f59
568ce4a
ff4303c
b95cb65
1d5d240
b9c424c
5263e9a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,177 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.rediscala; | ||
|
||
import static io.opentelemetry.api.trace.SpanKind.CLIENT; | ||
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.awaitility.Awaitility.await; | ||
|
||
import io.opentelemetry.api.common.AttributeKey; | ||
import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension; | ||
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension; | ||
import io.opentelemetry.instrumentation.testing.junit.db.SemconvStabilityUtil; | ||
import io.opentelemetry.semconv.incubating.DbIncubatingAttributes; | ||
import org.junit.jupiter.api.AfterAll; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
import org.testcontainers.containers.GenericContainer; | ||
import org.testcontainers.shaded.org.apache.commons.lang3.tuple.Pair; | ||
import redis.ByteStringDeserializer; | ||
import redis.ByteStringDeserializer$; | ||
import redis.ByteStringSerializer; | ||
import redis.ByteStringSerializer$; | ||
import redis.RedisClient; | ||
import redis.RedisDispatcher; | ||
import scala.Option; | ||
import scala.concurrent.Future; | ||
|
||
class RediscalaClientTest { | ||
|
||
@RegisterExtension | ||
static final InstrumentationExtension testing = AgentInstrumentationExtension.create(); | ||
|
||
private static GenericContainer<?> redisServer; | ||
|
||
@SuppressWarnings("deprecation") // DB_OPERATION is deprecated | ||
private static final AttributeKey<String> DB_OPERATION = | ||
SemconvStabilityUtil.maybeStable(DbIncubatingAttributes.DB_OPERATION); | ||
|
||
private static Object system; | ||
private static RedisClient redisClient; | ||
|
||
@BeforeAll | ||
static void setUp() throws Exception { | ||
redisServer = new GenericContainer<>("redis:6.2.3-alpine").withExposedPorts(6379); | ||
redisServer.start(); | ||
|
||
String host = redisServer.getHost(); | ||
Integer port = redisServer.getMappedPort(6379); | ||
|
||
try { | ||
Class<?> clazz = Class.forName("akka.actor.ActorSystem"); | ||
system = clazz.getMethod("create").invoke(null); | ||
} catch (ClassNotFoundException exception) { | ||
Class<?> clazz = Class.forName("org.apache.pekko.actor.ActorSystem"); | ||
system = clazz.getMethod("create").invoke(null); | ||
} | ||
|
||
try { | ||
RedisClient.class.getMethod("username"); | ||
redisClient = | ||
(RedisClient) | ||
RedisClient.class.getConstructors()[0].newInstance( | ||
host, | ||
port, | ||
Option.apply(null), | ||
Option.apply(null), | ||
Option.apply(null), | ||
"RedisClient", | ||
Option.apply(null), | ||
system, | ||
new RedisDispatcher("rediscala.rediscala-client-worker-dispatcher")); | ||
} catch (Exception e) { | ||
redisClient = | ||
(RedisClient) | ||
RedisClient.class.getConstructors()[0].newInstance( | ||
host, | ||
port, | ||
Option.apply(null), | ||
Option.apply(null), | ||
"RedisClient", | ||
Option.apply(null), | ||
system, | ||
new RedisDispatcher("rediscala.rediscala-client-worker-dispatcher")); | ||
} | ||
} | ||
|
||
@AfterAll | ||
static void tearDown() throws Exception { | ||
redisServer.stop(); | ||
if (system != null) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. groovy version also calls |
||
system.getClass().getMethod("terminate").invoke(system); | ||
} | ||
} | ||
|
||
@Test | ||
void testGetCommand() { | ||
Pair<Future<Object>, Future<Option<String>>> result = | ||
testing.runWithSpan( | ||
"parent", | ||
() -> { | ||
ByteStringSerializer<String> serializer = ByteStringSerializer$.MODULE$.String(); | ||
ByteStringDeserializer<String> deserializer = | ||
ByteStringDeserializer$.MODULE$.String(); | ||
Future<Object> writeFuture = | ||
redisClient.set( | ||
"bar", | ||
"baz", | ||
Option.apply(null), | ||
Option.apply(null), | ||
false, | ||
false, | ||
serializer); | ||
Future<Option<String>> valueFuture = redisClient.get("bar", deserializer); | ||
return Pair.of(writeFuture, valueFuture); | ||
}); | ||
|
||
await().atMost(java.time.Duration.ofSeconds(3)).until(() -> result.getLeft().isCompleted()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could import There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
await().atMost(java.time.Duration.ofSeconds(3)).until(() -> result.getRight().isCompleted()); | ||
assertThat(result.getLeft().value().get().get()).isEqualTo(true); | ||
assertThat(result.getRight().value().get().get().get()).isEqualTo("baz"); | ||
|
||
testing.waitAndAssertTraces( | ||
trace -> | ||
trace.hasSpansSatisfyingExactly( | ||
span -> span.hasName("parent").hasNoParent(), | ||
span -> | ||
span.hasName("SET") | ||
.hasKind(CLIENT) | ||
.hasParent(trace.getSpan(0)) | ||
.hasAttributesSatisfyingExactly( | ||
equalTo(DbIncubatingAttributes.DB_SYSTEM, "redis"), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could you add a static import for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. also could use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
equalTo(DB_OPERATION, "SET")), | ||
span -> | ||
span.hasName("GET") | ||
.hasKind(CLIENT) | ||
.hasParent(trace.getSpan(0)) | ||
.hasAttributesSatisfyingExactly( | ||
equalTo(DbIncubatingAttributes.DB_SYSTEM, "redis"), | ||
equalTo(DB_OPERATION, "GET")))); | ||
} | ||
|
||
@Test | ||
public void testSetCommand() { | ||
ByteStringSerializer<String> serializer = ByteStringSerializer$.MODULE$.String(); | ||
|
||
Future<Object> value = | ||
testing.runWithSpan( | ||
"parent", | ||
() -> | ||
redisClient.set( | ||
"foo", | ||
"bar", | ||
Option.apply(null), | ||
Option.apply(null), | ||
false, | ||
false, | ||
serializer)); | ||
await().atMost(java.time.Duration.ofSeconds(3)).until(value::isCompleted); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. just wondering whether it would be better to use scala await class like the groovy test does or maybe even consider converting the whole test to scala There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i think java class is enough to cover the instrument case |
||
assertThat(value.value().get().get()).isEqualTo(true); | ||
testing.waitAndAssertTraces( | ||
trace -> | ||
trace.hasSpansSatisfyingExactly( | ||
span -> span.hasName("parent").hasNoParent(), | ||
span -> | ||
span.hasName("SET") | ||
.hasKind(CLIENT) | ||
.hasParent(trace.getSpan(0)) | ||
.hasAttributesSatisfying( | ||
equalTo(DbIncubatingAttributes.DB_SYSTEM, "redis"), | ||
equalTo(DB_OPERATION, "SET")))); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we avoid using classes shaded into other libraries
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done