|
19 | 19 |
|
20 | 20 | package org.elasticsearch.action.support;
|
21 | 21 |
|
| 22 | +import org.apache.lucene.index.CorruptIndexException; |
| 23 | +import org.apache.lucene.index.IndexFormatTooNewException; |
| 24 | +import org.apache.lucene.index.IndexFormatTooOldException; |
| 25 | +import org.apache.lucene.store.AlreadyClosedException; |
| 26 | +import org.apache.lucene.store.LockObtainFailedException; |
22 | 27 | import org.elasticsearch.ElasticsearchException;
|
23 | 28 | import org.elasticsearch.action.support.broadcast.BroadcastShardOperationFailedException;
|
24 | 29 | import org.elasticsearch.common.Strings;
|
25 | 30 | import org.elasticsearch.common.bytes.BytesReference;
|
| 31 | +import org.elasticsearch.common.io.stream.BytesStreamOutput; |
| 32 | +import org.elasticsearch.common.io.stream.StreamInput; |
| 33 | +import org.elasticsearch.common.util.concurrent.EsRejectedExecutionException; |
26 | 34 | import org.elasticsearch.common.xcontent.XContent;
|
27 | 35 | import org.elasticsearch.common.xcontent.XContentBuilder;
|
28 | 36 | import org.elasticsearch.common.xcontent.XContentParser;
|
|
32 | 40 | import org.elasticsearch.rest.RestStatus;
|
33 | 41 | import org.elasticsearch.test.ESTestCase;
|
34 | 42 |
|
| 43 | +import java.io.EOFException; |
| 44 | +import java.io.FileNotFoundException; |
35 | 45 | import java.io.IOException;
|
| 46 | +import java.util.function.Supplier; |
| 47 | + |
| 48 | +import static org.hamcrest.Matchers.equalTo; |
36 | 49 |
|
37 | 50 | public class DefaultShardOperationFailedExceptionTests extends ESTestCase {
|
38 | 51 |
|
@@ -110,4 +123,60 @@ public void testFromXContent() throws IOException {
|
110 | 123 | assertEquals(parsed.status(), RestStatus.INTERNAL_SERVER_ERROR);
|
111 | 124 | assertEquals(parsed.getCause().getMessage(), "Elasticsearch exception [type=exception, reason=foo]");
|
112 | 125 | }
|
| 126 | + |
| 127 | + public void testSerialization() throws Exception { |
| 128 | + final DefaultShardOperationFailedException exception = randomInstance(); |
| 129 | + try (BytesStreamOutput out = new BytesStreamOutput()) { |
| 130 | + exception.writeTo(out); |
| 131 | + |
| 132 | + try (StreamInput in = out.bytes().streamInput()) { |
| 133 | + final DefaultShardOperationFailedException deserializedException = new DefaultShardOperationFailedException(in); |
| 134 | + assertNotSame(exception, deserializedException); |
| 135 | + assertThat(deserializedException.index(), equalTo(exception.index())); |
| 136 | + assertThat(deserializedException.shardId(), equalTo(exception.shardId())); |
| 137 | + assertThat(deserializedException.reason(), equalTo(exception.reason())); |
| 138 | + assertThat(deserializedException.getCause().getMessage(), equalTo(exception.getCause().getMessage())); |
| 139 | + assertThat(deserializedException.getCause().getClass(), equalTo(exception.getCause().getClass())); |
| 140 | + assertArrayEquals(deserializedException.getCause().getStackTrace(), exception.getCause().getStackTrace()); |
| 141 | + } |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + private static DefaultShardOperationFailedException randomInstance() { |
| 146 | + final Exception cause = randomException(); |
| 147 | + if (cause instanceof ElasticsearchException) { |
| 148 | + return new DefaultShardOperationFailedException((ElasticsearchException) cause); |
| 149 | + } else { |
| 150 | + return new DefaultShardOperationFailedException(randomAlphaOfLengthBetween(1, 5), randomIntBetween(0, 10), cause); |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + @SuppressWarnings("unchecked") |
| 155 | + private static Exception randomException() { |
| 156 | + Supplier<Exception> supplier = randomFrom( |
| 157 | + () -> new CorruptIndexException(randomAlphaOfLengthBetween(1, 5), randomAlphaOfLengthBetween(1, 5), randomExceptionOrNull()), |
| 158 | + () -> new NullPointerException(randomAlphaOfLengthBetween(1, 5)), |
| 159 | + () -> new NumberFormatException(randomAlphaOfLengthBetween(1, 5)), |
| 160 | + () -> new IllegalArgumentException(randomAlphaOfLengthBetween(1, 5), randomExceptionOrNull()), |
| 161 | + () -> new AlreadyClosedException(randomAlphaOfLengthBetween(1, 5), randomExceptionOrNull()), |
| 162 | + () -> new EOFException(randomAlphaOfLengthBetween(1, 5)), |
| 163 | + () -> new SecurityException(randomAlphaOfLengthBetween(1, 5), randomExceptionOrNull()), |
| 164 | + () -> new StringIndexOutOfBoundsException(randomAlphaOfLengthBetween(1, 5)), |
| 165 | + () -> new ArrayIndexOutOfBoundsException(randomAlphaOfLengthBetween(1, 5)), |
| 166 | + () -> new StringIndexOutOfBoundsException(randomAlphaOfLengthBetween(1, 5)), |
| 167 | + () -> new FileNotFoundException(randomAlphaOfLengthBetween(1, 5)), |
| 168 | + () -> new IllegalStateException(randomAlphaOfLengthBetween(1, 5), randomExceptionOrNull()), |
| 169 | + () -> new LockObtainFailedException(randomAlphaOfLengthBetween(1, 5), randomExceptionOrNull()), |
| 170 | + () -> new InterruptedException(randomAlphaOfLengthBetween(1, 5)), |
| 171 | + () -> new IOException(randomAlphaOfLengthBetween(1, 5), randomExceptionOrNull()), |
| 172 | + () -> new EsRejectedExecutionException(randomAlphaOfLengthBetween(1, 5), randomBoolean()), |
| 173 | + () -> new IndexFormatTooNewException(randomAlphaOfLengthBetween(1, 10), randomInt(), randomInt(), randomInt()), |
| 174 | + () -> new IndexFormatTooOldException(randomAlphaOfLengthBetween(1, 5), randomAlphaOfLengthBetween(1, 5)) |
| 175 | + ); |
| 176 | + return supplier.get(); |
| 177 | + } |
| 178 | + |
| 179 | + private static Exception randomExceptionOrNull() { |
| 180 | + return randomBoolean() ? randomException() : null; |
| 181 | + } |
113 | 182 | }
|
0 commit comments