/hazelcast-client/src/test/java/com/hazelcast/client/SocketInterceptorTest.java

https://bitbucket.org/gabral6_gmailcom/hazelcast · Java · 232 lines · 197 code · 20 blank · 15 comment · 12 complexity · 365267a3557e6709e932d39ceaf15b42 MD5 · raw file

  1. /*
  2. * Copyright (c) 2008-2013, Hazelcast, Inc. All Rights Reserved.
  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. * http://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 com.hazelcast.client;
  17. import com.hazelcast.config.Config;
  18. import com.hazelcast.config.GroupConfig;
  19. import com.hazelcast.config.SocketInterceptorConfig;
  20. import com.hazelcast.core.Hazelcast;
  21. import com.hazelcast.core.HazelcastInstance;
  22. import com.hazelcast.impl.GroupProperties;
  23. import com.hazelcast.nio.MemberSocketInterceptor;
  24. import org.junit.After;
  25. import org.junit.Before;
  26. import org.junit.Test;
  27. import org.junit.runner.RunWith;
  28. import java.io.IOException;
  29. import java.io.InputStream;
  30. import java.io.OutputStream;
  31. import java.net.Socket;
  32. import java.util.Random;
  33. import java.util.concurrent.atomic.AtomicInteger;
  34. import static org.junit.Assert.assertEquals;
  35. import static org.junit.Assert.assertTrue;
  36. @RunWith(com.hazelcast.util.RandomBlockJUnit4ClassRunner.class)
  37. public class SocketInterceptorTest {
  38. @After
  39. @Before
  40. public void cleanup() throws Exception {
  41. Hazelcast.shutdownAll();
  42. HazelcastClient.shutdownAll();
  43. }
  44. @Test(timeout = 120000)
  45. public void testSuccessfulSocketInterceptor() {
  46. Config config = new Config();
  47. SocketInterceptorConfig sic = new SocketInterceptorConfig();
  48. MySocketInterceptor mySocketInterceptor = new MySocketInterceptor(true);
  49. sic.setImplementation(mySocketInterceptor).setEnabled(true);
  50. config.getNetworkConfig().setSocketInterceptorConfig(sic);
  51. HazelcastInstance h1 = Hazelcast.newHazelcastInstance(config);
  52. HazelcastInstance h2 = Hazelcast.newHazelcastInstance(config);
  53. HazelcastInstance h3 = Hazelcast.newHazelcastInstance(config);
  54. HazelcastInstance h4 = Hazelcast.newHazelcastInstance(config);
  55. int count = 1000;
  56. for (int i = 0; i < count; i++) {
  57. h1.getMap("default").put(i, "value" + i);
  58. h2.getMap("default").put(i, "value" + i);
  59. h3.getMap("default").put(i, "value" + i);
  60. h4.getMap("default").put(i, "value" + i);
  61. }
  62. assertEquals(4, h4.getCluster().getMembers().size());
  63. assertTrue(mySocketInterceptor.getAcceptCallCount() >= 6);
  64. assertTrue(mySocketInterceptor.getConnectCallCount() >= 6);
  65. assertEquals(4, mySocketInterceptor.getInitCallCount());
  66. assertEquals(0, mySocketInterceptor.getAcceptFailureCount());
  67. assertEquals(0, mySocketInterceptor.getConnectFailureCount());
  68. ClientConfig clientConfig = new ClientConfig();
  69. clientConfig.setGroupConfig(new GroupConfig("dev", "dev-pass")).addAddress("localhost");
  70. MySocketInterceptor myClientSocketInterceptor = new MySocketInterceptor(true);
  71. clientConfig.setSocketInterceptor(myClientSocketInterceptor);
  72. HazelcastInstance client = HazelcastClient.newHazelcastClient(clientConfig);
  73. for (int i = 0; i < count; i++) {
  74. client.getMap("default").put(i, "value" + i);
  75. }
  76. assertTrue(mySocketInterceptor.getAcceptCallCount() >= 7);
  77. assertTrue(mySocketInterceptor.getConnectCallCount() >= 6);
  78. assertEquals(1, myClientSocketInterceptor.getConnectCallCount());
  79. assertEquals(0, myClientSocketInterceptor.getAcceptCallCount());
  80. assertEquals(0, mySocketInterceptor.getAcceptFailureCount());
  81. assertEquals(0, mySocketInterceptor.getConnectFailureCount());
  82. assertEquals(0, myClientSocketInterceptor.getAcceptFailureCount());
  83. assertEquals(0, myClientSocketInterceptor.getConnectFailureCount());
  84. }
  85. @Test(expected = RuntimeException.class, timeout = 120000)
  86. public void testFailingSocketInterceptor() {
  87. Config config = new Config();
  88. config.setProperty(GroupProperties.PROP_MAX_JOIN_SECONDS, "3");
  89. SocketInterceptorConfig sic = new SocketInterceptorConfig();
  90. MySocketInterceptor mySocketInterceptor = new MySocketInterceptor(false);
  91. sic.setImplementation(mySocketInterceptor).setEnabled(true);
  92. config.getNetworkConfig().setSocketInterceptorConfig(sic);
  93. HazelcastInstance h1 = Hazelcast.newHazelcastInstance(config);
  94. HazelcastInstance h2 = Hazelcast.newHazelcastInstance(config);
  95. }
  96. @Test(expected = RuntimeException.class, timeout = 120000)
  97. public void testFailingClientSocketInterceptor() {
  98. Config config = new Config();
  99. SocketInterceptorConfig sic = new SocketInterceptorConfig();
  100. MySocketInterceptor mySocketInterceptor = new MySocketInterceptor(true);
  101. sic.setImplementation(mySocketInterceptor).setEnabled(true);
  102. config.getNetworkConfig().setSocketInterceptorConfig(sic);
  103. HazelcastInstance h1 = Hazelcast.newHazelcastInstance(config);
  104. HazelcastInstance h2 = Hazelcast.newHazelcastInstance(config);
  105. int count = 1000;
  106. for (int i = 0; i < count; i++) {
  107. h1.getMap("default").put(i, "value" + i);
  108. h2.getMap("default").put(i, "value" + i);
  109. }
  110. assertEquals(2, h2.getCluster().getMembers().size());
  111. assertTrue(mySocketInterceptor.getAcceptCallCount() >= 1);
  112. assertTrue(mySocketInterceptor.getConnectCallCount() >= 1);
  113. assertEquals(2, mySocketInterceptor.getInitCallCount());
  114. assertEquals(0, mySocketInterceptor.getAcceptFailureCount());
  115. assertEquals(0, mySocketInterceptor.getConnectFailureCount());
  116. ClientConfig clientConfig = new ClientConfig();
  117. clientConfig.setGroupConfig(new GroupConfig("dev", "dev-pass")).addAddress("localhost");
  118. MySocketInterceptor myClientSocketInterceptor = new MySocketInterceptor(false);
  119. clientConfig.setSocketInterceptor(myClientSocketInterceptor);
  120. HazelcastInstance client = HazelcastClient.newHazelcastClient(clientConfig);
  121. for (int i = 0; i < count; i++) {
  122. client.getMap("default").put(i, "value" + i);
  123. }
  124. assertTrue(mySocketInterceptor.getAcceptCallCount() >= 2);
  125. assertTrue(mySocketInterceptor.getConnectCallCount() >= 1);
  126. assertEquals(1, myClientSocketInterceptor.getConnectCallCount());
  127. assertEquals(0, myClientSocketInterceptor.getAcceptCallCount());
  128. assertEquals(1, mySocketInterceptor.getAcceptFailureCount());
  129. assertEquals(0, myClientSocketInterceptor.getAcceptFailureCount());
  130. assertEquals(1, myClientSocketInterceptor.getConnectFailureCount());
  131. }
  132. public static class MySocketInterceptor implements MemberSocketInterceptor {
  133. final AtomicInteger initCallCount = new AtomicInteger();
  134. final AtomicInteger acceptCallCount = new AtomicInteger();
  135. final AtomicInteger connectCallCount = new AtomicInteger();
  136. final AtomicInteger acceptFailureCount = new AtomicInteger();
  137. final AtomicInteger connectFailureCount = new AtomicInteger();
  138. final boolean successful;
  139. public MySocketInterceptor(boolean successful) {
  140. this.successful = successful;
  141. }
  142. public void init(SocketInterceptorConfig socketInterceptorConfig) {
  143. initCallCount.incrementAndGet();
  144. }
  145. public void onAccept(Socket acceptedSocket) throws IOException {
  146. acceptCallCount.incrementAndGet();
  147. try {
  148. OutputStream out = acceptedSocket.getOutputStream();
  149. InputStream in = acceptedSocket.getInputStream();
  150. int loop = new Random().nextInt(2) + 1;
  151. int secretValue = 1;
  152. int expected = (int) Math.pow(2, loop);
  153. for (int i = 0; i < loop; i++) {
  154. out.write(secretValue);
  155. int read = in.read();
  156. if (read != 2 * secretValue) {
  157. throw new IOException("Authentication Failed");
  158. }
  159. secretValue = read;
  160. }
  161. if (secretValue != expected) {
  162. throw new IOException("Authentication Failed");
  163. }
  164. out.write(0);
  165. } catch (IOException e) {
  166. acceptFailureCount.incrementAndGet();
  167. throw e;
  168. }
  169. }
  170. public void onConnect(Socket connectedSocket) throws IOException {
  171. connectCallCount.incrementAndGet();
  172. try {
  173. OutputStream out = connectedSocket.getOutputStream();
  174. InputStream in = connectedSocket.getInputStream();
  175. int multiplyBy = (successful) ? 2 : 1;
  176. while (true) {
  177. int read = in.read();
  178. if (read == 0) return;
  179. out.write(read * multiplyBy);
  180. }
  181. } catch (IOException e) {
  182. connectFailureCount.incrementAndGet();
  183. throw e;
  184. }
  185. }
  186. public int getInitCallCount() {
  187. return initCallCount.get();
  188. }
  189. public int getAcceptCallCount() {
  190. return acceptCallCount.get();
  191. }
  192. public int getConnectCallCount() {
  193. return connectCallCount.get();
  194. }
  195. public int getAcceptFailureCount() {
  196. return acceptFailureCount.get();
  197. }
  198. public int getConnectFailureCount() {
  199. return connectFailureCount.get();
  200. }
  201. @Override
  202. public String toString() {
  203. return "MySocketInterceptor{" +
  204. "initCallCount=" + initCallCount +
  205. ", acceptCallCount=" + acceptCallCount +
  206. ", connectCallCount=" + connectCallCount +
  207. ", acceptFailureCount=" + acceptFailureCount +
  208. ", connectFailureCount=" + connectFailureCount +
  209. '}';
  210. }
  211. }
  212. }