Search in sources :

Example 1 with SocketInterceptorConfig

use of com.hazelcast.config.SocketInterceptorConfig in project hazelcast by hazelcast.

the class ConnectionTest method testDanglingSocketsOnTerminate.

private void testDanglingSocketsOnTerminate(boolean withSocketInterceptor) throws Exception {
    final int port = 5701;
    Config config = new Config();
    config.getNetworkConfig().setPort(port).setPortAutoIncrement(false);
    if (withSocketInterceptor) {
        config.getNetworkConfig().setSocketInterceptorConfig(new SocketInterceptorConfig().setEnabled(true).setImplementation(new MemberSocketInterceptor() {

            public void init(Properties properties) {
            }

            public void onAccept(Socket acceptedSocket) throws IOException {
            }

            public void onConnect(Socket connectedSocket) throws IOException {
            }
        }));
    }
    final HazelcastInstance hz = Hazelcast.newHazelcastInstance(config);
    final int count = 100;
    final CountDownLatch latch = new CountDownLatch(count);
    final CountDownLatch ll = new CountDownLatch(1);
    final AtomicInteger cc = new AtomicInteger();
    new Thread() {

        public void run() {
            try {
                ll.await(1, TimeUnit.MINUTES);
                hz.getLifecycleService().terminate();
            } catch (InterruptedException ignored) {
            }
        }
    }.start();
    final Collection<Socket> sockets = Collections.newSetFromMap(new ConcurrentHashMap<Socket, Boolean>());
    final AtomicInteger k0 = new AtomicInteger();
    final AtomicInteger k1 = new AtomicInteger();
    for (int i = 0; i < count; i++) {
        Runnable task = new Runnable() {

            public void run() {
                try {
                    if (cc.incrementAndGet() == count / 10) {
                        ll.countDown();
                    }
                    Socket socket = new Socket();
                    sockets.add(socket);
                    try {
                        socket.connect(new InetSocketAddress(port));
                        k0.incrementAndGet();
                    } catch (IOException e) {
                        k1.incrementAndGet();
                    }
                    OutputStream out = socket.getOutputStream();
                    out.write(Protocols.CLUSTER.getBytes());
                    out.flush();
                    socket.getInputStream().read();
                } catch (IOException ignored) {
                } finally {
                    latch.countDown();
                }
            }
        };
        Thread t = new Thread(task, "socket-thread-" + i);
        t.setDaemon(true);
        t.start();
    }
    try {
        assertTrue(latch.await(1, TimeUnit.MINUTES));
    } catch (AssertionError e) {
        System.err.println(ThreadDumpGenerator.dumpAllThreads());
        throw e;
    } finally {
        for (Socket socket : sockets) {
            try {
                socket.close();
            } catch (Exception e) {
                ignore(e);
            }
        }
    }
}
Also used : SocketInterceptorConfig(com.hazelcast.config.SocketInterceptorConfig) SocketInterceptorConfig(com.hazelcast.config.SocketInterceptorConfig) Config(com.hazelcast.config.Config) InetSocketAddress(java.net.InetSocketAddress) OutputStream(java.io.OutputStream) IOException(java.io.IOException) Properties(java.util.Properties) CountDownLatch(java.util.concurrent.CountDownLatch) IOException(java.io.IOException) HazelcastInstance(com.hazelcast.core.HazelcastInstance) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket)

Example 2 with SocketInterceptorConfig

use of com.hazelcast.config.SocketInterceptorConfig in project hazelcast by hazelcast.

the class TestClientNetworkConfig method smokeSocketInterceptor.

@Test
public void smokeSocketInterceptor() {
    ClientConfig config = client.getClientConfig();
    SocketInterceptorConfig socketInterceptorConfig = config.getNetworkConfig().getSocketInterceptorConfig();
    assertEquals(false, socketInterceptorConfig.isEnabled());
    assertEquals(DummySocketInterceptor.class.getName(), socketInterceptorConfig.getClassName());
}
Also used : SocketInterceptorConfig(com.hazelcast.config.SocketInterceptorConfig) ClientConfig(com.hazelcast.client.config.ClientConfig) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 3 with SocketInterceptorConfig

use of com.hazelcast.config.SocketInterceptorConfig in project hazelcast by hazelcast.

the class TestFullApplicationContext method testSocketInterceptorConfig.

@Test
public void testSocketInterceptorConfig() {
    SocketInterceptorConfig socketInterceptorConfig = config.getNetworkConfig().getSocketInterceptorConfig();
    assertNotNull(socketInterceptorConfig);
    assertFalse(socketInterceptorConfig.isEnabled());
    assertEquals(DummySocketInterceptor.class.getName(), socketInterceptorConfig.getClassName());
    assertEquals(socketInterceptor, socketInterceptorConfig.getImplementation());
}
Also used : SocketInterceptorConfig(com.hazelcast.config.SocketInterceptorConfig) Test(org.junit.Test) QuickTest(com.hazelcast.test.annotation.QuickTest)

Example 4 with SocketInterceptorConfig

use of com.hazelcast.config.SocketInterceptorConfig in project hazelcast by hazelcast.

the class XmlClientConfigBuilder method handleSocketInterceptorConfig.

private void handleSocketInterceptorConfig(Node node, ClientNetworkConfig clientNetworkConfig) {
    SocketInterceptorConfig socketInterceptorConfig = parseSocketInterceptorConfig(node);
    clientNetworkConfig.setSocketInterceptorConfig(socketInterceptorConfig);
}
Also used : SocketInterceptorConfig(com.hazelcast.config.SocketInterceptorConfig)

Example 5 with SocketInterceptorConfig

use of com.hazelcast.config.SocketInterceptorConfig in project hazelcast by hazelcast.

the class XmlClientConfigBuilderTest method testNetworkConfig.

@Test
public void testNetworkConfig() {
    final ClientNetworkConfig networkConfig = clientConfig.getNetworkConfig();
    assertEquals(2, networkConfig.getConnectionAttemptLimit());
    assertEquals(2, networkConfig.getAddresses().size());
    assertContains(networkConfig.getAddresses(), "127.0.0.1");
    assertContains(networkConfig.getAddresses(), "127.0.0.2");
    assertTrue(networkConfig.isSmartRouting());
    assertTrue(networkConfig.isRedoOperation());
    final SocketInterceptorConfig socketInterceptorConfig = networkConfig.getSocketInterceptorConfig();
    assertTrue(socketInterceptorConfig.isEnabled());
    assertEquals("com.hazelcast.examples.MySocketInterceptor", socketInterceptorConfig.getClassName());
    assertEquals("bar", socketInterceptorConfig.getProperty("foo"));
    final ClientAwsConfig awsConfig = networkConfig.getAwsConfig();
    assertTrue(awsConfig.isEnabled());
    assertTrue(awsConfig.isInsideAws());
    assertEquals("TEST_ACCESS_KEY", awsConfig.getAccessKey());
    assertEquals("TEST_ACCESS_KEY", awsConfig.getAccessKey());
    assertEquals("TEST_SECRET_KEY", awsConfig.getSecretKey());
    assertEquals("us-east-1", awsConfig.getRegion());
    assertEquals("ec2.amazonaws.com", awsConfig.getHostHeader());
    assertEquals("type", awsConfig.getTagKey());
    assertEquals("hz-nodes", awsConfig.getTagValue());
    assertEquals(11, awsConfig.getConnectionTimeoutSeconds());
}
Also used : SocketInterceptorConfig(com.hazelcast.config.SocketInterceptorConfig) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) XMLConfigBuilderTest(com.hazelcast.config.XMLConfigBuilderTest)

Aggregations

SocketInterceptorConfig (com.hazelcast.config.SocketInterceptorConfig)5 QuickTest (com.hazelcast.test.annotation.QuickTest)3 Test (org.junit.Test)3 ClientConfig (com.hazelcast.client.config.ClientConfig)1 Config (com.hazelcast.config.Config)1 XMLConfigBuilderTest (com.hazelcast.config.XMLConfigBuilderTest)1 HazelcastInstance (com.hazelcast.core.HazelcastInstance)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 InetSocketAddress (java.net.InetSocketAddress)1 ServerSocket (java.net.ServerSocket)1 Socket (java.net.Socket)1 Properties (java.util.Properties)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1