|
84 | 84 | import java.util.concurrent.atomic.AtomicReference;
|
85 | 85 |
|
86 | 86 | import static java.util.Collections.singletonMap;
|
| 87 | +import static org.hamcrest.Matchers.containsString; |
87 | 88 | import static org.hamcrest.Matchers.empty;
|
88 | 89 | import static org.hamcrest.Matchers.equalTo;
|
| 90 | +import static org.hamcrest.Matchers.greaterThanOrEqualTo; |
89 | 91 | import static org.hamcrest.Matchers.hasSize;
|
90 | 92 | import static org.hamcrest.Matchers.instanceOf;
|
91 | 93 | import static org.hamcrest.Matchers.lessThan;
|
@@ -547,23 +549,45 @@ public void testUpdate() throws IOException {
|
547 | 549 | IndexResponse indexResponse = highLevelClient().index(indexRequest, RequestOptions.DEFAULT);
|
548 | 550 | assertEquals(RestStatus.CREATED, indexResponse.status());
|
549 | 551 |
|
550 |
| - UpdateRequest updateRequest = new UpdateRequest("index", "type", "id"); |
551 |
| - updateRequest.doc(singletonMap("field", "updated"), randomFrom(XContentType.values())); |
552 |
| - |
553 |
| - UpdateResponse updateResponse = execute(updateRequest, highLevelClient()::update, highLevelClient()::updateAsync); |
554 |
| - assertEquals(RestStatus.OK, updateResponse.status()); |
555 |
| - assertEquals(indexResponse.getVersion() + 1, updateResponse.getVersion()); |
556 |
| - |
557 |
| - UpdateRequest updateRequestConflict = new UpdateRequest("index", "type", "id"); |
558 |
| - updateRequestConflict.doc(singletonMap("field", "with_version_conflict"), randomFrom(XContentType.values())); |
559 |
| - updateRequestConflict.version(indexResponse.getVersion()); |
| 552 | + long lastUpdateSeqNo; |
| 553 | + long lastUpdatePrimaryTerm; |
| 554 | + { |
| 555 | + UpdateRequest updateRequest = new UpdateRequest("index", "type", "id"); |
| 556 | + updateRequest.doc(singletonMap("field", "updated"), randomFrom(XContentType.values())); |
| 557 | + final UpdateResponse updateResponse = execute(updateRequest, highLevelClient()::update, highLevelClient()::updateAsync); |
| 558 | + assertEquals(RestStatus.OK, updateResponse.status()); |
| 559 | + assertEquals(indexResponse.getVersion() + 1, updateResponse.getVersion()); |
| 560 | + lastUpdateSeqNo = updateResponse.getSeqNo(); |
| 561 | + lastUpdatePrimaryTerm = updateResponse.getPrimaryTerm(); |
| 562 | + assertThat(lastUpdateSeqNo, greaterThanOrEqualTo(0L)); |
| 563 | + assertThat(lastUpdatePrimaryTerm, greaterThanOrEqualTo(1L)); |
| 564 | + } |
560 | 565 |
|
561 |
| - ElasticsearchStatusException exception = expectThrows(ElasticsearchStatusException.class, () -> |
562 |
| - execute(updateRequestConflict, highLevelClient()::update, highLevelClient()::updateAsync, |
563 |
| - highLevelClient()::update, highLevelClient()::updateAsync)); |
564 |
| - assertEquals(RestStatus.CONFLICT, exception.status()); |
565 |
| - assertEquals("Elasticsearch exception [type=version_conflict_engine_exception, reason=[type][id]: version conflict, " + |
566 |
| - "current version [2] is different than the one provided [1]]", exception.getMessage()); |
| 566 | + { |
| 567 | + final UpdateRequest updateRequest = new UpdateRequest("index", "type", "id"); |
| 568 | + updateRequest.doc(singletonMap("field", "with_seq_no_conflict"), randomFrom(XContentType.values())); |
| 569 | + if (randomBoolean()) { |
| 570 | + updateRequest.setIfSeqNo(lastUpdateSeqNo + 1); |
| 571 | + updateRequest.setIfPrimaryTerm(lastUpdatePrimaryTerm); |
| 572 | + } else { |
| 573 | + updateRequest.setIfSeqNo(lastUpdateSeqNo + (randomBoolean() ? 0 : 1)); |
| 574 | + updateRequest.setIfPrimaryTerm(lastUpdatePrimaryTerm + 1); |
| 575 | + } |
| 576 | + ElasticsearchStatusException exception = expectThrows(ElasticsearchStatusException.class, () -> |
| 577 | + execute(updateRequest, highLevelClient()::update, highLevelClient()::updateAsync)); |
| 578 | + assertEquals(exception.toString(),RestStatus.CONFLICT, exception.status()); |
| 579 | + assertThat(exception.getMessage(), containsString("Elasticsearch exception [type=version_conflict_engine_exception")); |
| 580 | + } |
| 581 | + { |
| 582 | + final UpdateRequest updateRequest = new UpdateRequest("index", "type", "id"); |
| 583 | + updateRequest.doc(singletonMap("field", "with_seq_no"), randomFrom(XContentType.values())); |
| 584 | + updateRequest.setIfSeqNo(lastUpdateSeqNo); |
| 585 | + updateRequest.setIfPrimaryTerm(lastUpdatePrimaryTerm); |
| 586 | + final UpdateResponse updateResponse = execute(updateRequest, highLevelClient()::update, highLevelClient()::updateAsync); |
| 587 | + assertEquals(RestStatus.OK, updateResponse.status()); |
| 588 | + assertEquals(lastUpdateSeqNo + 1, updateResponse.getSeqNo()); |
| 589 | + assertEquals(lastUpdatePrimaryTerm, updateResponse.getPrimaryTerm()); |
| 590 | + } |
567 | 591 | }
|
568 | 592 | {
|
569 | 593 | ElasticsearchStatusException exception = expectThrows(ElasticsearchStatusException.class, () -> {
|
|
0 commit comments