PageRenderTime 60ms CodeModel.GetById 10ms app.highlight 42ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://bitbucket.org/gabral6_gmailcom/hazelcast
Java | 271 lines | 241 code | 15 blank | 15 comment | 0 complexity | 38b418533f461e9fb10b4ac6c675ca09 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.core.*;
 20import com.hazelcast.core.LifecycleEvent.LifecycleState;
 21import com.hazelcast.security.Credentials;
 22import com.hazelcast.security.UsernamePasswordCredentials;
 23import org.junit.Ignore;
 24import org.junit.Test;
 25import org.junit.runner.RunWith;
 26
 27import java.net.InetSocketAddress;
 28import java.util.ArrayList;
 29import java.util.HashSet;
 30import java.util.List;
 31import java.util.Set;
 32import java.util.concurrent.CountDownLatch;
 33import java.util.concurrent.TimeUnit;
 34
 35import static org.junit.Assert.*;
 36import static org.mockito.Mockito.*;
 37
 38@RunWith(com.hazelcast.util.RandomBlockJUnit4ClassRunner.class)
 39public class ConnectionManagerTest {
 40    final Credentials credentials = new UsernamePasswordCredentials();
 41
 42    private LifecycleServiceClientImpl createLifecycleServiceClientImpl(HazelcastClient hazelcastClient, final List<LifecycleState> lifecycleEvents) {
 43        final LifecycleServiceClientImpl lifecycleService = new LifecycleServiceClientImpl(hazelcastClient);
 44        lifecycleService.addLifecycleListener(new LifecycleListener() {
 45
 46            public void stateChanged(LifecycleEvent event) {
 47                lifecycleEvents.add(event.getState());
 48            }
 49        });
 50        return lifecycleService;
 51    }
 52
 53    @Test
 54    public void testGetConnection() throws Exception {
 55        HazelcastClient client = mock(HazelcastClient.class);
 56        InetSocketAddress inetSocketAddress = new InetSocketAddress("localhost", 5701);
 57        final Connection connection = mock(Connection.class);
 58        final CountDownLatch latch = new CountDownLatch(2);
 59        final List<LifecycleState> lifecycleEvents = new ArrayList<LifecycleState>();
 60        final LifecycleServiceClientImpl lifecycleService = createLifecycleServiceClientImpl(client, lifecycleEvents);
 61        ClientConfig clientConfig = new ClientConfig();
 62        clientConfig.setCredentials(credentials).addInetSocketAddress(inetSocketAddress).setConnectionTimeout(60000);
 63        ConnectionManager connectionManager = new ConnectionManager(client, clientConfig, lifecycleService) {
 64            protected Connection getNextConnection() {
 65                latch.countDown();
 66                return connection;
 67            }
 68        };
 69        ClientBinder binder = mock(ClientBinder.class);
 70        connectionManager.setBinder(binder);
 71        connectionManager.getConnection();
 72        assertEquals(connection, connectionManager.getConnection());
 73        verify(binder).bind(connection, credentials);
 74        assertEquals(connection, connectionManager.getConnection());
 75        assertEquals(1, latch.getCount());
 76        Thread.sleep(100); // wait a little events to be fired
 77        assertArrayEquals(new Object[]{LifecycleState.CLIENT_CONNECTION_OPENING}, lifecycleEvents.toArray());
 78    }
 79
 80    @Test
 81    public void testGetConnectionWhenThereIsNoConnection() throws Exception {
 82        HazelcastClient client = mock(HazelcastClient.class);
 83        InetSocketAddress inetSocketAddress = new InetSocketAddress("localhost", 5701);
 84        final List<LifecycleState> lifecycleEvents = new ArrayList<LifecycleState>();
 85        final LifecycleServiceClientImpl lifecycleService = createLifecycleServiceClientImpl(client, lifecycleEvents);
 86        ClientConfig clientConfig = new ClientConfig();
 87        clientConfig.setCredentials(credentials).addInetSocketAddress(inetSocketAddress).setConnectionTimeout(60000);
 88        ConnectionManager connectionManager = new ConnectionManager(client, clientConfig, lifecycleService) {
 89            protected Connection getNextConnection() {
 90                return null;
 91            }
 92        };
 93        ClientBinder binder = mock(ClientBinder.class);
 94        connectionManager.setBinder(binder);
 95        connectionManager.getConnection();
 96        assertEquals(null, connectionManager.getConnection());
 97        assertEquals(null, connectionManager.getConnection());
 98        assertArrayEquals(new Object[0], lifecycleEvents.toArray());
 99    }
100
101    @Test
102    public void testDestroyConnection() throws Exception {
103        HazelcastClient client = mock(HazelcastClient.class);
104        InetSocketAddress inetSocketAddress = new InetSocketAddress("localhost", 5701);
105        final Connection connection = mock(Connection.class);
106        final CountDownLatch latch = new CountDownLatch(2);
107        final List<LifecycleState> lifecycleEvents = new ArrayList<LifecycleState>();
108        final LifecycleServiceClientImpl lifecycleService = createLifecycleServiceClientImpl(client, lifecycleEvents);
109        ClientConfig clientConfig = new ClientConfig();
110        clientConfig.setCredentials(credentials).addInetSocketAddress(inetSocketAddress).setConnectionTimeout(60000);
111        ConnectionManager connectionManager = new ConnectionManager(client, clientConfig, lifecycleService) {
112            protected Connection getNextConnection() {
113                latch.countDown();
114                return connection;
115            }
116        };
117        ClientBinder binder = mock(ClientBinder.class);
118        connectionManager.setBinder(binder);
119        assertEquals(connection, connectionManager.getConnection());
120        connectionManager.destroyConnection(connection);
121        connectionManager.getConnection();
122        assertTrue(latch.await(1, TimeUnit.SECONDS));
123        Thread.sleep(100); // wait a little events to be fired
124        assertArrayEquals(new Object[]{LifecycleState.CLIENT_CONNECTION_OPENING,
125                LifecycleState.CLIENT_CONNECTION_LOST,
126                LifecycleState.CLIENT_CONNECTION_OPENING},
127                lifecycleEvents.toArray());
128    }
129
130    @Test
131    @Ignore
132    public void testSameMemberAdded() throws Exception {
133        HazelcastClient client = mock(HazelcastClient.class);
134        InetSocketAddress inetSocketAddress = new InetSocketAddress("localhost", 5701);
135        final Connection connection = mock(Connection.class);
136        final CountDownLatch latch = new CountDownLatch(2);
137        final List<LifecycleState> lifecycleEvents = new ArrayList<LifecycleState>();
138        final LifecycleServiceClientImpl lifecycleService = createLifecycleServiceClientImpl(client, lifecycleEvents);
139        ClientConfig clientConfig = new ClientConfig();
140        clientConfig.setCredentials(credentials).addInetSocketAddress(inetSocketAddress).setConnectionTimeout(60000);
141        ConnectionManager connectionManager = new ConnectionManager(client, clientConfig, lifecycleService) {
142            protected Connection getNextConnection() {
143                latch.countDown();
144                return connection;
145            }
146        };
147        ClientBinder binder = mock(ClientBinder.class);
148        connectionManager.setBinder(binder);
149        Cluster cluster = mock(Cluster.class);
150        Member member = mock(Member.class);
151        when(member.getInetSocketAddress()).thenReturn(inetSocketAddress);
152        MembershipEvent membershipEvent = new MembershipEvent(cluster, member, MembershipEvent.MEMBER_ADDED);
153        connectionManager.memberAdded(membershipEvent);
154        connectionManager.getClusterMembers().contains(inetSocketAddress);
155        assertEquals(1, connectionManager.getClusterMembers().size());
156        assertArrayEquals(new Object[0], lifecycleEvents.toArray());
157    }
158
159    @Test
160    @Ignore
161    public void testDifferentMemberAdded() throws Exception {
162        HazelcastClient client = mock(HazelcastClient.class);
163        InetSocketAddress inetSocketAddress = new InetSocketAddress("localhost", 5701);
164        final Connection connection = mock(Connection.class);
165        final List<LifecycleState> lifecycleEvents = new ArrayList<LifecycleState>();
166        final LifecycleServiceClientImpl lifecycleService = createLifecycleServiceClientImpl(client, lifecycleEvents);
167        ClientConfig clientConfig = new ClientConfig();
168        clientConfig.setCredentials(credentials).addInetSocketAddress(inetSocketAddress).setConnectionTimeout(60000);
169        ConnectionManager connectionManager = new ConnectionManager(client, clientConfig, lifecycleService) {
170            protected Connection getNextConnection() {
171                return connection;
172            }
173        };
174        ClientBinder binder = mock(ClientBinder.class);
175        connectionManager.setBinder(binder);
176        Cluster cluster = mock(Cluster.class);
177        InetSocketAddress inetSocketAddress2 = new InetSocketAddress("hostname", 5702);
178        Member member = mock(Member.class);
179        when(member.getInetSocketAddress()).thenReturn(inetSocketAddress2);
180        MembershipEvent membershipEvent = new MembershipEvent(cluster, member, MembershipEvent.MEMBER_ADDED);
181        connectionManager.memberAdded(membershipEvent);
182        connectionManager.getClusterMembers().contains(inetSocketAddress2);
183        assertEquals(2, connectionManager.getClusterMembers().size());
184        assertArrayEquals(new Object[0], lifecycleEvents.toArray());
185    }
186
187    @Test
188    public void testMemberRemoved() throws Exception {
189        HazelcastClient client = mock(HazelcastClient.class);
190        InetSocketAddress inetSocketAddress = new InetSocketAddress("localhost", 5701);
191        final Connection connection = mock(Connection.class);
192        final List<LifecycleState> lifecycleEvents = new ArrayList<LifecycleState>();
193        final LifecycleServiceClientImpl lifecycleService = createLifecycleServiceClientImpl(client, lifecycleEvents);
194        ClientConfig clientConfig = new ClientConfig();
195        clientConfig.setCredentials(credentials).addInetSocketAddress(inetSocketAddress).setConnectionTimeout(60000);
196        ConnectionManager connectionManager = new ConnectionManager(client, clientConfig, lifecycleService) {
197            protected Connection getNextConnection() {
198                return connection;
199            }
200        };
201        ClientBinder binder = mock(ClientBinder.class);
202        connectionManager.setBinder(binder);
203        Cluster cluster = mock(Cluster.class);
204        Member member = mock(Member.class);
205        when(member.getInetSocketAddress()).thenReturn(inetSocketAddress);
206        MembershipEvent membershipEvent = new MembershipEvent(cluster, member, MembershipEvent.MEMBER_REMOVED);
207        connectionManager.memberRemoved(membershipEvent);
208        assertEquals(0, connectionManager.getClusterMembers().size());
209        assertArrayEquals(new Object[0], lifecycleEvents.toArray());
210    }
211
212    @Test
213    public void testUpdateMembers() throws Exception {
214        HazelcastClient client = mock(HazelcastClient.class);
215        Cluster cluster = mock(Cluster.class);
216        when(client.getCluster()).thenReturn(cluster);
217        Set<Member> members = new HashSet<Member>();
218        Member member1 = mock(Member.class);
219        Member member2 = mock(Member.class);
220        Member member3 = mock(Member.class);
221        InetSocketAddress inetSocketAddress1 = new InetSocketAddress("localhost", 9701);
222        InetSocketAddress inetSocketAddress2 = new InetSocketAddress("localhost", 9702);
223        InetSocketAddress inetSocketAddress3 = new InetSocketAddress("localhost", 9703);
224        when(member1.getInetSocketAddress()).thenReturn(inetSocketAddress1);
225        when(member2.getInetSocketAddress()).thenReturn(inetSocketAddress2);
226        when(member3.getInetSocketAddress()).thenReturn(inetSocketAddress3);
227        members.add(member1);
228        members.add(member2);
229        members.add(member3);
230        when(cluster.getMembers()).thenReturn(members);
231        InetSocketAddress inetSocketAddress = new InetSocketAddress("localhost", 5701);
232        final Connection connection = mock(Connection.class);
233        final List<LifecycleState> lifecycleEvents = new ArrayList<LifecycleState>();
234        final LifecycleServiceClientImpl lifecycleService = createLifecycleServiceClientImpl(client, lifecycleEvents);
235        ClientConfig clientConfig = new ClientConfig();
236        clientConfig.setCredentials(credentials).addInetSocketAddress(inetSocketAddress).setConnectionTimeout(60000);
237        ConnectionManager connectionManager = new ConnectionManager(client, clientConfig, lifecycleService) {
238            protected Connection getNextConnection() {
239                return connection;
240            }
241        };
242        ClientBinder binder = mock(ClientBinder.class);
243        connectionManager.setBinder(binder);
244        connectionManager.updateMembers();
245        assertTrue(connectionManager.getClusterMembers().contains(inetSocketAddress1));
246        assertTrue(connectionManager.getClusterMembers().contains(inetSocketAddress2));
247        assertTrue(connectionManager.getClusterMembers().contains(inetSocketAddress3));
248        assertFalse(connectionManager.getClusterMembers().contains(inetSocketAddress));
249        assertEquals(3, connectionManager.getClusterMembers().size());
250        assertArrayEquals(new Object[0], lifecycleEvents.toArray());
251    }
252
253    @Test
254    public void testShouldExecuteOnDisconnect() throws Exception {
255        HazelcastClient client = mock(HazelcastClient.class);
256        InetSocketAddress inetSocketAddress = new InetSocketAddress("localhost", 5701);
257        final Connection connection = mock(Connection.class);
258        final List<LifecycleState> lifecycleEvents = new ArrayList<LifecycleState>();
259        final LifecycleServiceClientImpl lifecycleService = createLifecycleServiceClientImpl(client, lifecycleEvents);
260        ClientConfig clientConfig = new ClientConfig();
261        clientConfig.setCredentials(credentials).addInetSocketAddress(inetSocketAddress).setConnectionTimeout(60000);
262        ConnectionManager connectionManager = new ConnectionManager(client, clientConfig, lifecycleService) {
263            protected Connection getNextConnection() {
264                return connection;
265            }
266        };
267        assertTrue(connectionManager.shouldExecuteOnDisconnect(connection));
268        assertFalse(connectionManager.shouldExecuteOnDisconnect(connection));
269        assertArrayEquals(new Object[0], lifecycleEvents.toArray());
270    }
271}