Migrate dd-trace-core groovy files to java part 8#11437
Conversation
we migrate 8 tests: - CheckpointerTest - DataStreamsTransactionExtractorsTest - DataStreamsWritingTest - DefaultDataStreamsMonitoringTest - DefaultPathwayContextTest - SchemaBuilderTest - SchemaSamplerTest - TransactionContainerTest
|
Hi! 👋 Thanks for your pull request! 🎉 To help us review it, please make sure to:
If you need help, please check our contributing guidelines. |
This comment has been minimized.
This comment has been minimized.
AlexeyKuznetsov-DD
left a comment
There was a problem hiding this comment.
LGTM, left several nit comments.
|
|
||
| @FunctionalInterface | ||
| public interface RequestHandler { | ||
| void handle(HandlerApi api) throws Exception; | ||
| } | ||
|
|
||
| public static JavaTestHttpServer httpServer(Consumer<JavaTestHttpServer> spec) { | ||
| JavaTestHttpServer server = new JavaTestHttpServer(); | ||
| spec.accept(server); | ||
| server.start(); | ||
| return server; | ||
| } | ||
|
|
||
| private final Server internalServer; | ||
| private HandlersSpec handlers; | ||
| private Consumer<Server> customizer = s -> {}; | ||
|
|
||
| public String keystorePath; | ||
| private URI address; | ||
| private URI secureAddress; | ||
| private final AtomicReference<HandlerApi.RequestApi> last = new AtomicReference<>(); | ||
|
|
||
| public final SSLContext sslContext; | ||
|
|
||
| private final X509TrustManager trustManager = |
There was a problem hiding this comment.
nit: a little bit unusual to see methods declared before variables... I think small re-ordering would not harm.
| for (Map.Entry<String, Object> entry : carrier.entries()) { | ||
| if ("dd-pathway-ctx-base64".equals(entry.getKey())) { | ||
| hasPathwayCtxBase64 = true; | ||
| break; | ||
| } | ||
| } |
There was a problem hiding this comment.
nit: Just curious if this can be refactored to java streams API?
something like:
boolean hasPathwayCtxBase64 =
carrier.entries().stream()
.anyMatch(entry -> "dd-pathway-ctx-base64".equals(entry.getKey()));
| DataStreamsTransactionExtractor.Type.HTTP_OUT_HEADERS, extractors.get(0).getType()); | ||
| assertEquals("transaction_id", extractors.get(0).getValue()); | ||
| assertEquals("second_extractor", extractors.get(1).getName()); | ||
| assertEquals(DataStreamsTransactionExtractor.Type.HTTP_IN_HEADERS, extractors.get(1).getType()); |
There was a problem hiding this comment.
nit: DataStreamsTransactionExtractor.Type.XXX can be static imports for readability.
| dataStreams.add( | ||
| new StatsPoint( | ||
| DataStreamsTags.create(null, null), | ||
| 9, | ||
| 0, | ||
| 10, | ||
| timeSource.getCurrentTimeNanos(), | ||
| 0, | ||
| 0, | ||
| 0, | ||
| null)); | ||
| dataStreams.add( | ||
| new StatsPoint( | ||
| DataStreamsTags.create( | ||
| "testType", DataStreamsTags.Direction.INBOUND, "testTopic", "testGroup", null), | ||
| 1, | ||
| 2, | ||
| 5, |
There was a problem hiding this comment.
nit: probably with helpers functions can be more human-readable?
here and similar places.
| pointConsumer = new CapturingPointConsumer(); | ||
| } | ||
|
|
||
| private static void verifyFirstPoint(StatsPoint point) { |
There was a problem hiding this comment.
nit: would assertFirstPoint be a better name ?
| boolean canSample1 = sampler.canSample(currentTimeMillis); | ||
| int weight1 = sampler.trySample(currentTimeMillis); | ||
| boolean canSample2 = sampler.canSample(currentTimeMillis + 1000); | ||
| boolean canSample3 = sampler.canSample(currentTimeMillis + 2000); | ||
| boolean canSample4 = sampler.canSample(currentTimeMillis + 30000); | ||
| int weight4 = sampler.trySample(currentTimeMillis + 30000); | ||
| boolean canSample5 = sampler.canSample(currentTimeMillis + 30001); | ||
|
|
||
| assertTrue(canSample1); | ||
| assertEquals(1, weight1); | ||
| assertFalse(canSample2); | ||
| assertFalse(canSample3); | ||
| assertTrue(canSample4); | ||
| assertEquals(3, weight4); | ||
| assertFalse(canSample5); |
There was a problem hiding this comment.
nit: why not just inline into asserts?
| StatsGroup group = bucket.getGroups().iterator().next(); | ||
| assertEquals("type:testType", group.getTags().getType()); | ||
| assertEquals("group:testGroup", group.getTags().getGroup()); | ||
| assertEquals("topic:testTopic", group.getTags().getTopic()); | ||
| assertEquals(3, group.getTags().nonNullSize()); | ||
| assertEquals(1L, group.getHash()); | ||
| assertEquals(2L, group.getParentHash()); |
There was a problem hiding this comment.
nit: probably some assertGroup helper can make code more readable here and similar places.
This phase is for migrating Groovy to Java, It is not for refactoring the tests. Most of the comments are for refactoring them, but the issue is already there in Groovy. |
fix forbidden apis
| boolean hasPathwayCtxBase64 = false; | ||
| for (Map.Entry<String, Object> entry : carrier.entries()) { | ||
| if ("dd-pathway-ctx-base64".equals(entry.getKey())) { | ||
| hasPathwayCtxBase64 = true; | ||
| break; | ||
| } | ||
| } |
There was a problem hiding this comment.
suggestion: My nitpick suggestion is not spotless compliant.
| boolean hasPathwayCtxBase64 = false; | |
| for (Map.Entry<String, Object> entry : carrier.entries()) { | |
| if ("dd-pathway-ctx-base64".equals(entry.getKey())) { | |
| hasPathwayCtxBase64 = true; | |
| break; | |
| } | |
| } | |
| boolean hasPathwayCtxBase64 = carrier.entries().stream() | |
| .anyMatch(entry -> PROPAGATION_KEY_BASE64.equals(entry.getKey())); |
| // cleanup | ||
| dataStreams.close(); |
There was a problem hiding this comment.
question: This was in the spock cleanup: block. If it makes sense to have it in an @After teardown. So we can differentiate test setup/teardown failures from the actual test.
There was a problem hiding this comment.
the thing is it's not in all test of the class.
but maybe those tests needs more love and going to a refactor phase later
| long deadline = System.currentTimeMillis() + 1000; | ||
| while (System.currentTimeMillis() < deadline) { |
There was a problem hiding this comment.
nitpick: Might be better with a nano time.
There was a problem hiding this comment.
considering the precision required, currentTimeMillis is fine here
| for (Entry<StatsBucket.SchemaKey, Long> entry : usages) { | ||
| StatsBucket.SchemaKey key = entry.getKey(); | ||
| if (key.getSchemaId() == schemaId | ||
| && operation.equals(key.getOperation()) | ||
| && key.isKey() == isKey) { | ||
| return entry; | ||
| } | ||
| } | ||
| return null; |
There was a problem hiding this comment.
nitpick: I was looking at using stream API, but it doesn't gain much lines
return usages.stream()
.filter(
entry -> {
StatsBucket.SchemaKey key = entry.getKey();
return key.getSchemaId() == schemaId
&& operation.equals(key.getOperation())
&& key.isKey() == isKey;
})
.findFirst()
.orElse(null);
|
Looks good! As follow up ideas : for the test server, I'm pondering if this is something that could be made a JUnit extension, so the server lifecyle is properly managed. Then the next question is where should it be remains in the |
sleep in synchronized block
|
/merge |
|
View all feedbacks in Devflow UI.
The expected merge time in
|
What Does This Do
we migrate 8 tests:
Motivation
this is part of the effort to migrate groovy tests to Java/JUnit
part1: #11053
part2: #11062
part3: #11085
part4: #11146
part5: #11217
part6: #11362
part7: #11374
Additional Notes
I have create a Java version of TestHttpServer as this is used by one of the test.
We keep the groovy and the Java version until the migration is completed
Contributor Checklist
type:and (comp:orinst:) labels in addition to any other useful labelsclose,fix, or any linking keywords when referencing an issueUse
solvesinstead, and assign the PR milestone to the issueJira ticket: [PROJ-IDENT]
Note: Once your PR is ready to merge, add it to the merge queue by commenting
/merge./merge -ccancels the queue request./merge -f --reason "reason"skips all merge queue checks; please use this judiciously, as some checks do not run at the PR-level. For more information, see this doc.