Skip to content

Commit e8f73b7

Browse files
authored
Add Blockhound to test setup.
Original Pull Request #1823 Closes #1822
1 parent 5ed655e commit e8f73b7

File tree

5 files changed

+196
-21
lines changed

5 files changed

+196
-21
lines changed

Diff for: pom.xml

+45-21
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
<netty>4.1.52.Final</netty>
2525
<springdata.commons>2.6.0-SNAPSHOT</springdata.commons>
2626
<testcontainers>1.15.1</testcontainers>
27+
<blockhound-junit>1.0.6.RELEASE</blockhound-junit>
2728
<java-module-name>spring.data.elasticsearch</java-module-name>
2829
</properties>
2930

@@ -256,6 +257,14 @@
256257
<scope>test</scope>
257258
</dependency>
258259

260+
<dependency>
261+
<groupId>io.projectreactor.tools</groupId>
262+
<artifactId>blockhound-junit-platform</artifactId>
263+
<version>${blockhound-junit}</version>
264+
<scope>test</scope>
265+
</dependency>
266+
267+
259268
<!--
260269
we don't use lombok in Spring Data Elasticsearch anymore. But the dependency is set in the parent project, and so the
261270
lombok compiler stuff is executed regardless of the fact that we don't need it.
@@ -272,24 +281,24 @@
272281
<scope>test</scope>
273282
</dependency>
274283

275-
<!--
276-
<dependency>
277-
<groupId>org.apache.openwebbeans.test</groupId>
278-
<artifactId>cditest-owb</artifactId>
279-
<version>1.2.8</version>
280-
<scope>test</scope>
281-
<exclusions>
282-
<exclusion>
283-
<groupId>org.apache.geronimo.specs</groupId>
284-
<artifactId>geronimo-jcdi_1.0_spec</artifactId>
285-
</exclusion>
286-
<exclusion>
287-
<groupId>org.apache.geronimo.specs</groupId>
288-
<artifactId>geronimo-atinject_1.0_spec</artifactId>
289-
</exclusion>
290-
</exclusions>
291-
</dependency>
292-
-->
284+
<!--
285+
<dependency>
286+
<groupId>org.apache.openwebbeans.test</groupId>
287+
<artifactId>cditest-owb</artifactId>
288+
<version>1.2.8</version>
289+
<scope>test</scope>
290+
<exclusions>
291+
<exclusion>
292+
<groupId>org.apache.geronimo.specs</groupId>
293+
<artifactId>geronimo-jcdi_1.0_spec</artifactId>
294+
</exclusion>
295+
<exclusion>
296+
<groupId>org.apache.geronimo.specs</groupId>
297+
<artifactId>geronimo-atinject_1.0_spec</artifactId>
298+
</exclusion>
299+
</exclusions>
300+
</dependency>
301+
-->
293302

294303
<dependency>
295304
<groupId>org.skyscreamer</groupId>
@@ -448,9 +457,7 @@
448457

449458
<profiles>
450459
<profile>
451-
452460
<id>ci</id>
453-
454461
<build>
455462
<plugins>
456463
<plugin>
@@ -473,9 +480,26 @@
473480
</plugin>
474481
</plugins>
475482
</build>
476-
477483
</profile>
478484

485+
<profile>
486+
<id>jdk13+</id>
487+
<!-- on jDK13+, Blockhound needs this JVM flag set -->
488+
<activation>
489+
<jdk>[13,)</jdk>
490+
</activation>
491+
<build>
492+
<plugins>
493+
<plugin>
494+
<groupId>org.apache.maven.plugins</groupId>
495+
<artifactId>maven-surefire-plugin</artifactId>
496+
<configuration>
497+
<argLine>-XX:+AllowRedefinitionToAddDeleteMethods</argLine>
498+
</configuration>
499+
</plugin>
500+
</plugins>
501+
</build>
502+
</profile>
479503
</profiles>
480504

481505
<repositories>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.elasticsearch.blockhound;
17+
18+
import reactor.blockhound.BlockHound;
19+
import reactor.blockhound.integration.BlockHoundIntegration;
20+
21+
import org.elasticsearch.Build;
22+
import org.elasticsearch.common.xcontent.XContentBuilder;
23+
import org.springframework.data.elasticsearch.support.VersionInfo;
24+
25+
/**
26+
* @author Peter-Josef Meisch
27+
*/
28+
public class BlockHoundIntegrationCustomizer implements BlockHoundIntegration {
29+
@Override
30+
public void applyTo(BlockHound.Builder builder) {
31+
// Elasticsearch classes reading from the classpath on initialization, needed for parsing Elasticsearch responses
32+
builder.allowBlockingCallsInside(XContentBuilder.class.getName(), "<clinit>")
33+
.allowBlockingCallsInside(Build.class.getName(), "<clinit>");
34+
35+
// Spring Data Elasticsearch classes reading from the classpath
36+
builder.allowBlockingCallsInside(VersionInfo.class.getName(), "logVersions");
37+
}
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright 2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.elasticsearch.blockhound;
17+
18+
import static org.assertj.core.api.Assertions.*;
19+
20+
import reactor.blockhound.BlockingOperationError;
21+
import reactor.core.publisher.Mono;
22+
23+
import java.time.Duration;
24+
25+
import org.junit.jupiter.api.DisplayName;
26+
import org.junit.jupiter.api.Test;
27+
28+
/**
29+
* @author Peter-Josef Meisch
30+
*/
31+
public class BlockHoundTests {
32+
33+
@Test // #1822
34+
@DisplayName("should fail if BlockHound is not installed")
35+
void shouldFailIfBlockHoundIsNotInstalled() {
36+
37+
assertThatThrownBy(() -> {
38+
Mono.delay(Duration.ofMillis(1)).doOnNext(it -> {
39+
try {
40+
Thread.sleep(10);
41+
} catch (InterruptedException e) {
42+
throw new RuntimeException(e);
43+
}
44+
}).block(); // should throw an exception about Thread.sleep
45+
}).hasCauseInstanceOf(BlockingOperationError.class);
46+
}
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright 2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.elasticsearch.core.index;
17+
18+
import static org.springframework.data.elasticsearch.annotations.FieldType.*;
19+
20+
import java.time.Instant;
21+
22+
import org.junit.jupiter.api.DisplayName;
23+
import org.junit.jupiter.api.Test;
24+
import org.springframework.beans.factory.annotation.Autowired;
25+
import org.springframework.data.annotation.Id;
26+
import org.springframework.data.elasticsearch.annotations.DateFormat;
27+
import org.springframework.data.elasticsearch.annotations.Document;
28+
import org.springframework.data.elasticsearch.annotations.Field;
29+
import org.springframework.data.elasticsearch.annotations.Mapping;
30+
import org.springframework.data.elasticsearch.core.ReactiveElasticsearchOperations;
31+
import org.springframework.data.elasticsearch.core.ReactiveIndexOperations;
32+
import org.springframework.data.elasticsearch.junit.jupiter.ReactiveElasticsearchRestTemplateConfiguration;
33+
import org.springframework.data.elasticsearch.junit.jupiter.SpringIntegrationTest;
34+
import org.springframework.lang.Nullable;
35+
import org.springframework.test.context.ContextConfiguration;
36+
37+
/**
38+
* @author Peter-Josef Meisch
39+
*/
40+
@SpringIntegrationTest
41+
@ContextConfiguration(classes = { ReactiveElasticsearchRestTemplateConfiguration.class })
42+
public class ReactiveMappingBuilderIntegrationTests {
43+
44+
@Autowired private ReactiveElasticsearchOperations operations;
45+
46+
@Test // #1822
47+
@DisplayName("should write runtime fields")
48+
void shouldWriteRuntimeFields() {
49+
50+
ReactiveIndexOperations indexOps = operations.indexOps(RuntimeFieldEntity.class);
51+
52+
indexOps.create().block();
53+
indexOps.putMapping().block();
54+
indexOps.delete().block();
55+
}
56+
57+
// region entities
58+
@Document(indexName = "runtime-fields")
59+
@Mapping(runtimeFieldsPath = "/mappings/runtime-fields.json")
60+
private static class RuntimeFieldEntity {
61+
@Id @Nullable private String id;
62+
@Field(type = Date, format = DateFormat.epoch_millis, name = "@timestamp") @Nullable private Instant timestamp;
63+
}
64+
// endregion
65+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
org.springframework.data.elasticsearch.blockhound.BlockHoundIntegrationCustomizer

0 commit comments

Comments
 (0)