PageRenderTime 29ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

/modules/elasticsearch/src/test/java/org/elasticsearch/transport/AbstractSimpleTransportTests.java

https://github.com/avar/elasticsearch
Java | 437 lines | 327 code | 79 blank | 31 comment | 1 complexity | 65957d0627da24f841c59f141ab31bda MD5 | raw file
  1. /*
  2. * Licensed to Elastic Search and Shay Banon under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. Elastic Search licenses this
  6. * file to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. package org.elasticsearch.transport;
  20. import org.elasticsearch.cluster.node.DiscoveryNode;
  21. import org.elasticsearch.common.io.stream.StreamInput;
  22. import org.elasticsearch.common.io.stream.StreamOutput;
  23. import org.elasticsearch.common.io.stream.Streamable;
  24. import org.elasticsearch.common.io.stream.VoidStreamable;
  25. import org.elasticsearch.common.unit.TimeValue;
  26. import org.elasticsearch.threadpool.ThreadPool;
  27. import org.testng.annotations.AfterMethod;
  28. import org.testng.annotations.BeforeMethod;
  29. import org.testng.annotations.Test;
  30. import java.io.IOException;
  31. import java.util.concurrent.CountDownLatch;
  32. import java.util.concurrent.TimeUnit;
  33. import static org.elasticsearch.transport.TransportRequestOptions.*;
  34. import static org.hamcrest.MatcherAssert.*;
  35. import static org.hamcrest.Matchers.*;
  36. /**
  37. * @author kimchy (shay.banon)
  38. */
  39. public abstract class AbstractSimpleTransportTests {
  40. protected ThreadPool threadPool;
  41. protected TransportService serviceA;
  42. protected TransportService serviceB;
  43. protected DiscoveryNode serviceANode;
  44. protected DiscoveryNode serviceBNode;
  45. @BeforeMethod public void setUp() {
  46. threadPool = new ThreadPool();
  47. build();
  48. serviceA.connectToNode(serviceBNode);
  49. serviceB.connectToNode(serviceANode);
  50. }
  51. @AfterMethod public void tearDown() {
  52. serviceA.close();
  53. serviceB.close();
  54. threadPool.shutdown();
  55. }
  56. protected abstract void build();
  57. @Test public void testHelloWorld() {
  58. serviceA.registerHandler("sayHello", new BaseTransportRequestHandler<StringMessage>() {
  59. @Override public StringMessage newInstance() {
  60. return new StringMessage();
  61. }
  62. @Override public String executor() {
  63. return ThreadPool.Names.CACHED;
  64. }
  65. @Override public void messageReceived(StringMessage request, TransportChannel channel) {
  66. assertThat("moshe", equalTo(request.message));
  67. try {
  68. channel.sendResponse(new StringMessage("hello " + request.message));
  69. } catch (IOException e) {
  70. e.printStackTrace();
  71. assertThat(e.getMessage(), false, equalTo(true));
  72. }
  73. }
  74. });
  75. TransportFuture<StringMessage> res = serviceB.submitRequest(serviceANode, "sayHello",
  76. new StringMessage("moshe"), new BaseTransportResponseHandler<StringMessage>() {
  77. @Override public StringMessage newInstance() {
  78. return new StringMessage();
  79. }
  80. @Override public String executor() {
  81. return ThreadPool.Names.CACHED;
  82. }
  83. @Override public void handleResponse(StringMessage response) {
  84. assertThat("hello moshe", equalTo(response.message));
  85. }
  86. @Override public void handleException(TransportException exp) {
  87. exp.printStackTrace();
  88. assertThat("got exception instead of a response: " + exp.getMessage(), false, equalTo(true));
  89. }
  90. });
  91. try {
  92. StringMessage message = res.get();
  93. assertThat("hello moshe", equalTo(message.message));
  94. } catch (Exception e) {
  95. assertThat(e.getMessage(), false, equalTo(true));
  96. }
  97. serviceA.removeHandler("sayHello");
  98. }
  99. @Test public void testVoidMessageCompressed() {
  100. serviceA.registerHandler("sayHello", new BaseTransportRequestHandler<VoidStreamable>() {
  101. @Override public VoidStreamable newInstance() {
  102. return VoidStreamable.INSTANCE;
  103. }
  104. @Override public String executor() {
  105. return ThreadPool.Names.CACHED;
  106. }
  107. @Override public void messageReceived(VoidStreamable request, TransportChannel channel) {
  108. try {
  109. channel.sendResponse(VoidStreamable.INSTANCE, TransportResponseOptions.options().withCompress(true));
  110. } catch (IOException e) {
  111. e.printStackTrace();
  112. assertThat(e.getMessage(), false, equalTo(true));
  113. }
  114. }
  115. });
  116. TransportFuture<VoidStreamable> res = serviceB.submitRequest(serviceANode, "sayHello",
  117. VoidStreamable.INSTANCE, TransportRequestOptions.options().withCompress(true), new BaseTransportResponseHandler<VoidStreamable>() {
  118. @Override public VoidStreamable newInstance() {
  119. return VoidStreamable.INSTANCE;
  120. }
  121. @Override public String executor() {
  122. return ThreadPool.Names.CACHED;
  123. }
  124. @Override public void handleResponse(VoidStreamable response) {
  125. }
  126. @Override public void handleException(TransportException exp) {
  127. exp.printStackTrace();
  128. assertThat("got exception instead of a response: " + exp.getMessage(), false, equalTo(true));
  129. }
  130. });
  131. try {
  132. VoidStreamable message = res.get();
  133. assertThat(message, notNullValue());
  134. } catch (Exception e) {
  135. assertThat(e.getMessage(), false, equalTo(true));
  136. }
  137. serviceA.removeHandler("sayHello");
  138. }
  139. @Test public void testHelloWorldCompressed() {
  140. serviceA.registerHandler("sayHello", new BaseTransportRequestHandler<StringMessage>() {
  141. @Override public StringMessage newInstance() {
  142. return new StringMessage();
  143. }
  144. @Override public String executor() {
  145. return ThreadPool.Names.CACHED;
  146. }
  147. @Override public void messageReceived(StringMessage request, TransportChannel channel) {
  148. assertThat("moshe", equalTo(request.message));
  149. try {
  150. channel.sendResponse(new StringMessage("hello " + request.message), TransportResponseOptions.options().withCompress(true));
  151. } catch (IOException e) {
  152. e.printStackTrace();
  153. assertThat(e.getMessage(), false, equalTo(true));
  154. }
  155. }
  156. });
  157. TransportFuture<StringMessage> res = serviceB.submitRequest(serviceANode, "sayHello",
  158. new StringMessage("moshe"), TransportRequestOptions.options().withCompress(true), new BaseTransportResponseHandler<StringMessage>() {
  159. @Override public StringMessage newInstance() {
  160. return new StringMessage();
  161. }
  162. @Override public String executor() {
  163. return ThreadPool.Names.CACHED;
  164. }
  165. @Override public void handleResponse(StringMessage response) {
  166. assertThat("hello moshe", equalTo(response.message));
  167. }
  168. @Override public void handleException(TransportException exp) {
  169. exp.printStackTrace();
  170. assertThat("got exception instead of a response: " + exp.getMessage(), false, equalTo(true));
  171. }
  172. });
  173. try {
  174. StringMessage message = res.get();
  175. assertThat("hello moshe", equalTo(message.message));
  176. } catch (Exception e) {
  177. assertThat(e.getMessage(), false, equalTo(true));
  178. }
  179. serviceA.removeHandler("sayHello");
  180. }
  181. @Test public void testErrorMessage() {
  182. serviceA.registerHandler("sayHelloException", new BaseTransportRequestHandler<StringMessage>() {
  183. @Override public StringMessage newInstance() {
  184. return new StringMessage();
  185. }
  186. @Override public String executor() {
  187. return ThreadPool.Names.CACHED;
  188. }
  189. @Override public void messageReceived(StringMessage request, TransportChannel channel) throws Exception {
  190. assertThat("moshe", equalTo(request.message));
  191. throw new RuntimeException("bad message !!!");
  192. }
  193. });
  194. TransportFuture<StringMessage> res = serviceB.submitRequest(serviceANode, "sayHelloException",
  195. new StringMessage("moshe"), new BaseTransportResponseHandler<StringMessage>() {
  196. @Override public StringMessage newInstance() {
  197. return new StringMessage();
  198. }
  199. @Override public String executor() {
  200. return ThreadPool.Names.CACHED;
  201. }
  202. @Override public void handleResponse(StringMessage response) {
  203. assertThat("got response instead of exception", false, equalTo(true));
  204. }
  205. @Override public void handleException(TransportException exp) {
  206. assertThat("bad message !!!", equalTo(exp.getCause().getMessage()));
  207. }
  208. });
  209. try {
  210. res.txGet();
  211. assertThat("exception should be thrown", false, equalTo(true));
  212. } catch (Exception e) {
  213. assertThat("bad message !!!", equalTo(e.getCause().getMessage()));
  214. }
  215. serviceA.removeHandler("sayHelloException");
  216. }
  217. @Test
  218. public void testDisconnectListener() throws Exception {
  219. final CountDownLatch latch = new CountDownLatch(1);
  220. TransportConnectionListener disconnectListener = new TransportConnectionListener() {
  221. @Override public void onNodeConnected(DiscoveryNode node) {
  222. throw new RuntimeException("Should not be called");
  223. }
  224. @Override public void onNodeDisconnected(DiscoveryNode node) {
  225. latch.countDown();
  226. }
  227. };
  228. serviceA.addConnectionListener(disconnectListener);
  229. serviceB.close();
  230. assertThat(latch.await(5, TimeUnit.SECONDS), equalTo(true));
  231. }
  232. @Test public void testTimeoutSendExceptionWithNeverSendingBackResponse() throws Exception {
  233. serviceA.registerHandler("sayHelloTimeoutNoResponse", new BaseTransportRequestHandler<StringMessage>() {
  234. @Override public StringMessage newInstance() {
  235. return new StringMessage();
  236. }
  237. @Override public String executor() {
  238. return ThreadPool.Names.CACHED;
  239. }
  240. @Override public void messageReceived(StringMessage request, TransportChannel channel) {
  241. assertThat("moshe", equalTo(request.message));
  242. // don't send back a response
  243. // try {
  244. // channel.sendResponse(new StringMessage("hello " + request.message));
  245. // } catch (IOException e) {
  246. // e.printStackTrace();
  247. // assertThat(e.getMessage(), false, equalTo(true));
  248. // }
  249. }
  250. });
  251. TransportFuture<StringMessage> res = serviceB.submitRequest(serviceANode, "sayHelloTimeoutNoResponse",
  252. new StringMessage("moshe"), options().withTimeout(100), new BaseTransportResponseHandler<StringMessage>() {
  253. @Override public StringMessage newInstance() {
  254. return new StringMessage();
  255. }
  256. @Override public String executor() {
  257. return ThreadPool.Names.CACHED;
  258. }
  259. @Override public void handleResponse(StringMessage response) {
  260. assertThat("got response instead of exception", false, equalTo(true));
  261. }
  262. @Override public void handleException(TransportException exp) {
  263. assertThat(exp, instanceOf(ReceiveTimeoutTransportException.class));
  264. }
  265. });
  266. try {
  267. StringMessage message = res.txGet();
  268. assertThat("exception should be thrown", false, equalTo(true));
  269. } catch (Exception e) {
  270. assertThat(e, instanceOf(ReceiveTimeoutTransportException.class));
  271. }
  272. serviceA.removeHandler("sayHelloTimeoutNoResponse");
  273. }
  274. @Test public void testTimeoutSendExceptionWithDelayedResponse() throws Exception {
  275. serviceA.registerHandler("sayHelloTimeoutDelayedResponse", new BaseTransportRequestHandler<StringMessage>() {
  276. @Override public StringMessage newInstance() {
  277. return new StringMessage();
  278. }
  279. @Override public String executor() {
  280. return ThreadPool.Names.CACHED;
  281. }
  282. @Override public void messageReceived(StringMessage request, TransportChannel channel) {
  283. TimeValue sleep = TimeValue.parseTimeValue(request.message, null);
  284. try {
  285. Thread.sleep(sleep.millis());
  286. } catch (InterruptedException e) {
  287. // ignore
  288. }
  289. try {
  290. channel.sendResponse(new StringMessage("hello " + request.message));
  291. } catch (IOException e) {
  292. e.printStackTrace();
  293. assertThat(e.getMessage(), false, equalTo(true));
  294. }
  295. }
  296. });
  297. TransportFuture<StringMessage> res = serviceB.submitRequest(serviceANode, "sayHelloTimeoutDelayedResponse",
  298. new StringMessage("300ms"), options().withTimeout(100), new BaseTransportResponseHandler<StringMessage>() {
  299. @Override public StringMessage newInstance() {
  300. return new StringMessage();
  301. }
  302. @Override public String executor() {
  303. return ThreadPool.Names.CACHED;
  304. }
  305. @Override public void handleResponse(StringMessage response) {
  306. assertThat("got response instead of exception", false, equalTo(true));
  307. }
  308. @Override public void handleException(TransportException exp) {
  309. assertThat(exp, instanceOf(ReceiveTimeoutTransportException.class));
  310. }
  311. });
  312. try {
  313. StringMessage message = res.txGet();
  314. assertThat("exception should be thrown", false, equalTo(true));
  315. } catch (Exception e) {
  316. assertThat(e, instanceOf(ReceiveTimeoutTransportException.class));
  317. }
  318. // sleep for 400 millis to make sure we get back the response
  319. Thread.sleep(400);
  320. for (int i = 0; i < 10; i++) {
  321. final int counter = i;
  322. // now, try and send another request, this times, with a short timeout
  323. res = serviceB.submitRequest(serviceANode, "sayHelloTimeoutDelayedResponse",
  324. new StringMessage(counter + "ms"), options().withTimeout(100), new BaseTransportResponseHandler<StringMessage>() {
  325. @Override public StringMessage newInstance() {
  326. return new StringMessage();
  327. }
  328. @Override public String executor() {
  329. return ThreadPool.Names.CACHED;
  330. }
  331. @Override public void handleResponse(StringMessage response) {
  332. assertThat("hello " + counter + "ms", equalTo(response.message));
  333. }
  334. @Override public void handleException(TransportException exp) {
  335. exp.printStackTrace();
  336. assertThat("got exception instead of a response for " + counter + ": " + exp.getDetailedMessage(), false, equalTo(true));
  337. }
  338. });
  339. StringMessage message = res.txGet();
  340. assertThat(message.message, equalTo("hello " + counter + "ms"));
  341. }
  342. serviceA.removeHandler("sayHelloTimeoutDelayedResponse");
  343. }
  344. private class StringMessage implements Streamable {
  345. private String message;
  346. private StringMessage(String message) {
  347. this.message = message;
  348. }
  349. private StringMessage() {
  350. }
  351. @Override public void readFrom(StreamInput in) throws IOException {
  352. message = in.readUTF();
  353. }
  354. @Override public void writeTo(StreamOutput out) throws IOException {
  355. out.writeUTF(message);
  356. }
  357. }
  358. }