Skip to content

Commit a3a46b2

Browse files
DATAES-510 - Add tests for resource clean up.
Original Pull Request: spring-projects#231
1 parent b50e60f commit a3a46b2

File tree

6 files changed

+226
-0
lines changed

6 files changed

+226
-0
lines changed

src/test/java/org/springframework/data/elasticsearch/client/reactive/ReactiveElasticsearchClientUnitTests.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@
2020
import static org.mockito.Mockito.*;
2121
import static org.springframework.data.elasticsearch.client.reactive.ReactiveMockClientTestsUtils.MockWebClientProvider.Receive.*;
2222

23+
import reactor.core.publisher.Mono;
2324
import reactor.test.StepVerifier;
2425

26+
import java.io.IOException;
2527
import java.net.URI;
2628
import java.util.Collections;
2729

@@ -38,12 +40,14 @@
3840
import org.elasticsearch.index.VersionType;
3941
import org.junit.Before;
4042
import org.junit.Test;
43+
import org.mockito.Mockito;
4144
import org.reactivestreams.Publisher;
4245
import org.springframework.data.elasticsearch.client.reactive.ReactiveMockClientTestsUtils.MockDelegatingElasticsearchHostProvider;
4346
import org.springframework.data.elasticsearch.client.reactive.ReactiveMockClientTestsUtils.MockWebClientProvider.Receive;
4447
import org.springframework.http.HttpMethod;
4548
import org.springframework.http.HttpStatus;
4649
import org.springframework.http.MediaType;
50+
import org.springframework.util.StreamUtils;
4751

4852
/**
4953
* @author Christoph Strobl
@@ -572,4 +576,51 @@ public void searchShouldReturnEmptyFluxIfNothingFound() {
572576
.verifyComplete();
573577
}
574578

579+
// --> SCROLL
580+
581+
@Test // DATAES-510
582+
public void scrollShouldReadAll() throws IOException {
583+
584+
byte[] start = StreamUtils.copyToByteArray(Receive.fromPath("search-ok-scroll").getInputStream());
585+
byte[] next = StreamUtils.copyToByteArray(Receive.fromPath("scroll_ok").getInputStream());
586+
byte[] end = StreamUtils.copyToByteArray(Receive.fromPath("scroll_no_more_results").getInputStream());
587+
byte[] cleanup = StreamUtils.copyToByteArray(Receive.fromPath("scroll_clean").getInputStream());
588+
589+
hostProvider.when(HOST) //
590+
.receive(Receive::json) //
591+
.receive(response -> Mockito.when(response.body(any())).thenReturn(Mono.just(start), Mono.just(next),
592+
Mono.just(end), Mono.just(cleanup)));
593+
594+
client.scroll(new SearchRequest("twitter")) //
595+
.as(StepVerifier::create) //
596+
.expectNextCount(4) //
597+
.verifyComplete();
598+
599+
hostProvider.when(HOST).receive(response -> {
600+
verify(response, times(4)).body(any());
601+
});
602+
}
603+
604+
@Test // DATAES-510
605+
public void scrollShouldCleanUpResourcesOnError() throws IOException {
606+
607+
byte[] start = StreamUtils.copyToByteArray(Receive.fromPath("search-ok-scroll").getInputStream());
608+
byte[] error = StreamUtils.copyToByteArray(Receive.fromPath("scroll_error").getInputStream());
609+
byte[] cleanup = StreamUtils.copyToByteArray(Receive.fromPath("scroll_clean").getInputStream());
610+
611+
hostProvider.when(HOST) //
612+
.receive(Receive::json) //
613+
.receive(response -> Mockito.when(response.body(any())).thenReturn(Mono.just(start), Mono.just(error),
614+
Mono.just(cleanup)));
615+
616+
client.scroll(new SearchRequest("twitter")) //
617+
.as(StepVerifier::create) //
618+
.expectNextCount(2) //
619+
.verifyError();
620+
621+
hostProvider.when(HOST).receive(response -> {
622+
verify(response, times(3)).body(any());
623+
});
624+
}
625+
575626
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"num_freed": 1,
3+
"succeeded": true
4+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
{
2+
"error": {
3+
"caused_by": {
4+
"reason": "No search context found for id [1]",
5+
"type": "search_context_missing_exception"
6+
},
7+
"failed_shards": [
8+
{
9+
"index": null,
10+
"reason": {
11+
"reason": "No search context found for id [1]",
12+
"type": "search_context_missing_exception"
13+
},
14+
"shard": -1
15+
},
16+
{
17+
"index": null,
18+
"reason": {
19+
"reason": "No search context found for id [2]",
20+
"type": "search_context_missing_exception"
21+
},
22+
"shard": -1
23+
},
24+
{
25+
"index": null,
26+
"reason": {
27+
"reason": "No search context found for id [3]",
28+
"type": "search_context_missing_exception"
29+
},
30+
"shard": -1
31+
},
32+
{
33+
"index": null,
34+
"reason": {
35+
"reason": "No search context found for id [4]",
36+
"type": "search_context_missing_exception"
37+
},
38+
"shard": -1
39+
},
40+
{
41+
"index": null,
42+
"reason": {
43+
"reason": "No search context found for id [5]",
44+
"type": "search_context_missing_exception"
45+
},
46+
"shard": -1
47+
}
48+
],
49+
"grouped": true,
50+
"phase": "query",
51+
"reason": "all shards failed",
52+
"root_cause": [
53+
{
54+
"reason": "No search context found for id [1]",
55+
"type": "search_context_missing_exception"
56+
},
57+
{
58+
"reason": "No search context found for id [2]",
59+
"type": "search_context_missing_exception"
60+
},
61+
{
62+
"reason": "No search context found for id [3]",
63+
"type": "search_context_missing_exception"
64+
},
65+
{
66+
"reason": "No search context found for id [4]",
67+
"type": "search_context_missing_exception"
68+
},
69+
{
70+
"reason": "No search context found for id [5]",
71+
"type": "search_context_missing_exception"
72+
}
73+
],
74+
"type": "search_phase_execution_exception"
75+
},
76+
"status": 404
77+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"_shards": {
3+
"failed": 0,
4+
"skipped": 0,
5+
"successful": 5,
6+
"total": 5
7+
},
8+
"hits": {
9+
"hits": [],
10+
"max_score": 1.0,
11+
"total": 100
12+
},
13+
"terminated_early": true,
14+
"timed_out": false,
15+
"took": 1
16+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
{
2+
"_shards": {
3+
"failed": 0,
4+
"skipped": 0,
5+
"successful": 5,
6+
"total": 5
7+
},
8+
"hits": {
9+
"hits": [
10+
{
11+
"_index": "twitter",
12+
"_type": "doc",
13+
"_id": "2",
14+
"_score": 0.2876821,
15+
"_source": {
16+
"user": "kimchy",
17+
"post_date": "2009-11-15T14:12:12",
18+
"message": "Another tweet, will it be indexed?"
19+
}
20+
},
21+
{
22+
"_index": "twitter",
23+
"_type": "doc",
24+
"_id": "1",
25+
"_score": 0.2876821,
26+
"_source": {
27+
"user": "kimchy",
28+
"post_date": "2009-11-15T13:12:00",
29+
"message": "Trying out Elasticsearch, so far so good?"
30+
}
31+
}
32+
],
33+
"max_score": 1.0,
34+
"total": 100
35+
},
36+
"terminated_early": true,
37+
"timed_out": false,
38+
"took": 1
39+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
{
2+
"took": 52,
3+
"timed_out": false,
4+
"_scroll_id": "DnF1ZXJ5VGhlbkZldGNoBQAAAAAAAAAHFndhSE1uNUlLUXhXb1ZvQTNqOHNrMWcAAAAAAAAABhZ3YUhNbjVJS1F4V29Wb0EzajhzazFnAAAAAAAAAAgWd2FITW41SUtReFdvVm9BM2o4c2sxZwAAAAAAAAAJFndhSE1uNUlLUXhXb1ZvQTNqOHNrMWcAAAAAAAAAChZ3YUhNbjVJS1F4V29Wb0EzajhzazFn",
5+
"_shards": {
6+
"total": 5,
7+
"successful": 5,
8+
"skipped": 0,
9+
"failed": 0
10+
},
11+
"hits": {
12+
"total": 100,
13+
"max_score": 0.2876821,
14+
"hits": [
15+
{
16+
"_index": "twitter",
17+
"_type": "doc",
18+
"_id": "2",
19+
"_score": 0.2876821,
20+
"_source": {
21+
"user": "kimchy",
22+
"post_date": "2009-11-15T14:12:12",
23+
"message": "Another tweet, will it be indexed?"
24+
}
25+
},
26+
{
27+
"_index": "twitter",
28+
"_type": "doc",
29+
"_id": "1",
30+
"_score": 0.2876821,
31+
"_source": {
32+
"user": "kimchy",
33+
"post_date": "2009-11-15T13:12:00",
34+
"message": "Trying out Elasticsearch, so far so good?"
35+
}
36+
}
37+
]
38+
}
39+
}

0 commit comments

Comments
 (0)