Skip to content

Commit 327c496

Browse files
otelbot[bot]trask
andauthored
Code review sweep (run 24960889682) (#18314)
Co-authored-by: otelbot <197425009+otelbot@users.noreply.github.com> Co-authored-by: Trask Stalnaker <trask.stalnaker@gmail.com>
1 parent 8948946 commit 327c496

11 files changed

Lines changed: 49 additions & 61 deletions

File tree

.github/agents/knowledge/testing-general-patterns.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@
3737
wrapping when the lambda already needs its own error handling for behavioral reasons.
3838
- Do **not** choose `CompletableFuture.runAsync(...)` over the simpler
3939
`executor.submit(runnable).get()` just to avoid the checked exceptions thrown by `Future.get()`.
40+
- Do **not** wrap a call in `assertThatCode(() -> ...).doesNotThrowAnyException()`
41+
solely to narrow a test method's `throws` clause by swallowing checked exceptions
42+
into an `AssertionError`. If the call throws, the test already fails via its
43+
`throws` clause. Prefer calling the method directly and leaving `throws Exception`
44+
(or the narrowest checked type) on the `@Test` method.
4045

4146
## Test Resource Cleanup
4247

instrumentation/elasticsearch/elasticsearch-transport-6.0/testing/src/main/java/io/opentelemetry/javaagent/instrumentation/elasticsearch/transport/v6_0/AbstractElasticsearch6NodeClientTest.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import org.elasticsearch.client.Client;
1616
import org.elasticsearch.common.settings.Settings;
1717
import org.elasticsearch.node.Node;
18-
import org.junit.jupiter.api.AfterAll;
1918
import org.junit.jupiter.api.BeforeAll;
2019
import org.junit.jupiter.api.Test;
2120
import org.junit.jupiter.api.io.TempDir;
@@ -45,6 +44,7 @@ void setUp(@TempDir File esWorkingDir) {
4544
.put("discovery.type", "single-node")
4645
.build();
4746
testNode = getNodeFactory().newNode(settings);
47+
cleanup.deferAfterAll(testNode);
4848
startNode(testNode);
4949

5050
client = testNode.client();
@@ -75,11 +75,6 @@ void setUp(@TempDir File esWorkingDir) {
7575
testing.clearData();
7676
}
7777

78-
@AfterAll
79-
void cleanUp() throws Exception {
80-
testNode.close();
81-
}
82-
8378
protected abstract NodeFactory getNodeFactory();
8479

8580
@Override

instrumentation/elasticsearch/elasticsearch-transport-6.0/testing/src/main/java/io/opentelemetry/javaagent/instrumentation/elasticsearch/transport/v6_0/AbstractElasticsearch6TransportClientTest.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import org.elasticsearch.node.Node;
2020
import org.elasticsearch.transport.TransportService;
2121
import org.elasticsearch.transport.client.PreBuiltTransportClient;
22-
import org.junit.jupiter.api.AfterAll;
2322
import org.junit.jupiter.api.BeforeAll;
2423
import org.junit.jupiter.api.io.TempDir;
2524
import org.slf4j.Logger;
@@ -46,6 +45,7 @@ void setUp(@TempDir File esWorkingDir) {
4645
.put("discovery.type", "single-node")
4746
.build();
4847
testNode = getNodeFactory().newNode(settings);
48+
cleanup.deferAfterAll(testNode);
4949
startNode(testNode);
5050

5151
tcpPublishAddress =
@@ -59,6 +59,7 @@ void setUp(@TempDir File esWorkingDir) {
5959
.put("thread_pool.listener.size", 1)
6060
.put(CLUSTER_NAME_SETTING.getKey(), clusterName)
6161
.build());
62+
cleanup.deferAfterAll(client);
6263
client.addTransportAddress(tcpPublishAddress);
6364
testing.runWithSpan(
6465
"setup",
@@ -88,12 +89,6 @@ void setUp(@TempDir File esWorkingDir) {
8889
testing.clearData();
8990
}
9091

91-
@AfterAll
92-
void cleanUp() throws Exception {
93-
client.close();
94-
testNode.close();
95-
}
96-
9792
protected abstract NodeFactory getNodeFactory();
9893

9994
@Override

instrumentation/executors/bootstrap/src/main/java/io/opentelemetry/javaagent/bootstrap/executors/PropagatedContext.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public final class PropagatedContext {
2222

2323
// Used by AtomicReferenceFieldUpdater
2424
@SuppressWarnings("UnusedVariable")
25+
@Nullable
2526
private volatile Context context;
2627

2728
PropagatedContext() {}

instrumentation/executors/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/executors/CallableInstrumentation.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public ElementMatcher<TypeDescription> typeMatcher() {
3232
public void transform(TypeTransformer transformer) {
3333
transformer.applyAdviceToMethod(
3434
named("call").and(takesArguments(0)).and(isPublic()),
35-
CallableInstrumentation.class.getName() + "$CallableAdvice");
35+
getClass().getName() + "$CallableAdvice");
3636
}
3737

3838
@SuppressWarnings("unused")

instrumentation/executors/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/executors/FutureInstrumentation.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public boolean matches(TypeDescription target) {
9292
public void transform(TypeTransformer transformer) {
9393
transformer.applyAdviceToMethod(
9494
named("cancel").and(returns(boolean.class)),
95-
FutureInstrumentation.class.getName() + "$CanceledFutureAdvice");
95+
getClass().getName() + "$CanceledFutureAdvice");
9696
}
9797

9898
@SuppressWarnings("unused")

instrumentation/executors/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/executors/JavaExecutorInstrumentation.java

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,45 +53,44 @@ public ElementMatcher<TypeDescription> typeMatcher() {
5353
public void transform(TypeTransformer transformer) {
5454
transformer.applyAdviceToMethod(
5555
named("execute").and(takesArgument(0, Runnable.class)).and(takesArguments(1)),
56-
JavaExecutorInstrumentation.class.getName() + "$SetExecuteRunnableStateAdvice");
56+
getClass().getName() + "$SetExecuteRunnableStateAdvice");
5757
// Netty uses addTask as the actual core of their submission; there are non-standard variations
5858
// like execute(Runnable,boolean) that aren't caught by standard instrumentation
5959
transformer.applyAdviceToMethod(
6060
named("addTask").and(takesArgument(0, Runnable.class)).and(takesArguments(1)),
61-
JavaExecutorInstrumentation.class.getName() + "$SetExecuteRunnableStateAdvice");
61+
getClass().getName() + "$SetExecuteRunnableStateAdvice");
6262
transformer.applyAdviceToMethod(
6363
named("execute").and(takesArgument(0, ForkJoinTask.class)),
64-
JavaExecutorInstrumentation.class.getName() + "$SetJavaForkJoinStateAdvice");
64+
getClass().getName() + "$SetJavaForkJoinStateAdvice");
6565
transformer.applyAdviceToMethod(
6666
named("submit")
6767
.and(takesArgument(0, Runnable.class))
6868
.and(returns(hasSuperType(is(Future.class)))),
69-
JavaExecutorInstrumentation.class.getName() + "$SetSubmitRunnableStateAdvice");
69+
getClass().getName() + "$SetSubmitRunnableStateAdvice");
7070
transformer.applyAdviceToMethod(
7171
named("submit")
7272
.and(takesArgument(0, Callable.class))
7373
.and(returns(hasSuperType(is(Future.class)))),
74-
JavaExecutorInstrumentation.class.getName() + "$SetCallableStateAdvice");
74+
getClass().getName() + "$SetCallableStateAdvice");
7575
transformer.applyAdviceToMethod(
7676
named("submit").and(takesArgument(0, ForkJoinTask.class)),
77-
JavaExecutorInstrumentation.class.getName() + "$SetJavaForkJoinStateAdvice");
77+
getClass().getName() + "$SetJavaForkJoinStateAdvice");
7878
transformer.applyAdviceToMethod(
7979
namedOneOf("invokeAny", "invokeAll").and(takesArgument(0, Collection.class)),
80-
JavaExecutorInstrumentation.class.getName()
81-
+ "$SetCallableStateForCallableCollectionAdvice");
80+
getClass().getName() + "$SetCallableStateForCallableCollectionAdvice");
8281
transformer.applyAdviceToMethod(
8382
named("invoke").and(takesArgument(0, ForkJoinTask.class)),
84-
JavaExecutorInstrumentation.class.getName() + "$SetJavaForkJoinStateAdvice");
83+
getClass().getName() + "$SetJavaForkJoinStateAdvice");
8584
transformer.applyAdviceToMethod(
8685
named("schedule")
8786
.and(takesArgument(0, Runnable.class))
8887
.and(returns(hasSuperType(is(Future.class)))),
89-
JavaExecutorInstrumentation.class.getName() + "$SetSubmitRunnableStateAdvice");
88+
getClass().getName() + "$SetSubmitRunnableStateAdvice");
9089
transformer.applyAdviceToMethod(
9190
named("schedule")
9291
.and(takesArgument(0, Callable.class))
9392
.and(returns(hasSuperType(is(Future.class)))),
94-
JavaExecutorInstrumentation.class.getName() + "$SetCallableStateAdvice");
93+
getClass().getName() + "$SetCallableStateAdvice");
9594
}
9695

9796
@SuppressWarnings("unused")

instrumentation/executors/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/executors/JavaForkJoinTaskInstrumentation.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,9 @@ public ElementMatcher<TypeDescription> typeMatcher() {
4747
public void transform(TypeTransformer transformer) {
4848
transformer.applyAdviceToMethod(
4949
named("exec").and(takesArguments(0)).and(not(isAbstract())),
50-
JavaForkJoinTaskInstrumentation.class.getName() + "$ForkJoinTaskAdvice");
50+
getClass().getName() + "$ForkJoinTaskAdvice");
5151
transformer.applyAdviceToMethod(
52-
named("fork").and(takesArguments(0)),
53-
JavaForkJoinTaskInstrumentation.class.getName() + "$ForkAdvice");
52+
named("fork").and(takesArguments(0)), getClass().getName() + "$ForkAdvice");
5453
}
5554

5655
@SuppressWarnings("unused")

instrumentation/executors/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/executors/RunnableInstrumentation.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public ElementMatcher<TypeDescription> typeMatcher() {
3131
public void transform(TypeTransformer transformer) {
3232
transformer.applyAdviceToMethod(
3333
named("run").and(takesArguments(0)).and(isPublic()),
34-
RunnableInstrumentation.class.getName() + "$RunnableAdvice");
34+
getClass().getName() + "$RunnableAdvice");
3535
}
3636

3737
@SuppressWarnings("unused")

instrumentation/executors/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/executors/CompletableFutureTest.java

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -32,35 +32,30 @@ void multipleCallbacks() {
3232
cleanup.deferCleanup(executor::shutdown);
3333
cleanup.deferCleanup(executor2::shutdown);
3434

35-
String result;
36-
try {
37-
result =
38-
testing.runWithSpan(
39-
"parent",
40-
() ->
41-
CompletableFuture.supplyAsync(
42-
() -> {
43-
testing.runWithSpan("supplier", () -> {});
44-
try {
45-
Thread.sleep(1);
46-
} catch (InterruptedException e) {
47-
Thread.currentThread().interrupt();
48-
throw new AssertionError(e);
49-
}
50-
return "a";
51-
},
52-
executor)
53-
.thenCompose(
54-
s -> CompletableFuture.supplyAsync(new AppendingSupplier(s), executor2))
55-
.thenApply(
56-
s -> {
57-
testing.runWithSpan("function", () -> {});
58-
return s + "c";
59-
})
60-
.get());
61-
} catch (Exception e) {
62-
throw new AssertionError(e);
63-
}
35+
String result =
36+
testing.runWithSpan(
37+
"parent",
38+
() ->
39+
CompletableFuture.supplyAsync(
40+
() -> {
41+
testing.runWithSpan("supplier", () -> {});
42+
try {
43+
Thread.sleep(1);
44+
} catch (InterruptedException e) {
45+
Thread.currentThread().interrupt();
46+
throw new AssertionError(e);
47+
}
48+
return "a";
49+
},
50+
executor)
51+
.thenCompose(
52+
s -> CompletableFuture.supplyAsync(new AppendingSupplier(s), executor2))
53+
.thenApply(
54+
s -> {
55+
testing.runWithSpan("function", () -> {});
56+
return s + "c";
57+
})
58+
.join());
6459

6560
assertThat(result).isEqualTo("abc");
6661

0 commit comments

Comments
 (0)