Skip to content
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

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

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;
Copy link
Contributor

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

groovy version also calls redisServer.stop()

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());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could import java.time.Duration

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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"),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you add a static import for DB_SYSTEM

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also could use DbIncubatingAttributes .DbIncubatingAttributes.REDIS

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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);
Copy link
Contributor

Choose a reason for hiding this comment

The 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

Copy link
Contributor Author

@shalk shalk Nov 11, 2024

Choose a reason for hiding this comment

The 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"))));
}
}
Loading