PageRenderTime 24ms CodeModel.GetById 8ms app.highlight 13ms RepoModel.GetById 1ms app.codeStats 0ms

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