package com.datastax.driver.core;

import org.testng.annotations.Test;

import java.util.Collections;

import static org.testng.Assert.assertEquals;


public class SessionLeakTest {


    @Test(groups = "short")
    public void connectionLeakTest() throws Exception {
        // Checking for JAVA-342
        CCMBridge ccmBridge;
        ccmBridge = CCMBridge.create("test", 1);

        // give the driver time to close other sessions in this class
        //Thread.sleep(10);

        // create a new cluster object and ensure 0 sessions and connections
        Cluster cluster = Cluster.builder().addContactPoints(CCMBridge.IP_PREFIX + '1').build();

        int corePoolSize = cluster.getConfiguration()
                .getPoolingOptions()
                .getCoreConnectionsPerHost(HostDistance.LOCAL);

        assertEquals(cluster.manager.sessions.size(), 0);
        assertEquals((int) cluster.getMetrics().getOpenConnections().getValue(), 0);

        // ensure sessions.size() returns with 1 control connection + core pool size.
        Session session = cluster.connect();
        assertEquals(cluster.manager.sessions.size(), 1);
        assertEquals((int) cluster.getMetrics().getOpenConnections().getValue(), 1 + corePoolSize);

        // ensure sessions.size() returns to 0 with only 1 active connection (the control connection)
        session.close();
        assertEquals(cluster.manager.sessions.size(), 0);
        assertEquals((int) cluster.getMetrics().getOpenConnections().getValue(), 1);

        try {
            Session thisSession;

            // ensure bootstrapping a node does not create additional connections


            ccmBridge.bootstrapNode(2);
            assertEquals(cluster.manager.sessions.size(), 0);
            assertEquals((int) cluster.getMetrics().getOpenConnections().getValue(), 1);

            // ensure a new session gets registered and core connections are established
            // there should be corePoolSize more connections to accommodate for the new host.
            thisSession = cluster.connect();
            assertEquals(cluster.manager.sessions.size(), 1);

            assertEquals((int) cluster.getMetrics().getOpenConnections().getValue(), 1 + (corePoolSize * 2));

            // ensure bootstrapping a node does not create additional connections that won't get cleaned up
            thisSession.close();

            assertEquals(cluster.manager.sessions.size(), 0);

            assertEquals((int) cluster.getMetrics().getOpenConnections().getValue(), 1);


        } finally {
            // ensure we decommission node2 for the rest of the tests

            assertEquals(cluster.manager.sessions.size(), 0);
            assertEquals((int) cluster.getMetrics().getOpenConnections().getValue(), 1);

            if(cluster != null){
                cluster.close();

            }

            if (ccmBridge != null) {
                ccmBridge.remove();
            }


        }
    }
}

@@ -372,25 +372,24 @@
 t%22, 1);%0A
-%0A
         
 // give 
@@ -384,99 +384,50 @@
     
-// give the driver time to close other sessions in this class%0A        //Thread.sleep(10);%0A%0A
+Cluster cluster = null;%0A        try %7B%0A    
     
@@ -499,39 +499,35 @@
 ections%0A        
-Cluster
+   
  cluster = Clust
@@ -588,25 +588,57 @@
 ').build();%0A
-%0A
+            cluster.init();%0A%0A    
         int 
@@ -691,24 +691,28 @@
             
+  
+  
 .getPoolingO
@@ -720,16 +720,20 @@
 tions()%0A
+    
         
@@ -789,32 +789,36 @@
 OCAL);%0A%0A        
+   
+ 
 assertEquals(clu
@@ -843,32 +843,104 @@
 ons.size(), 0);%0A
+            // Should be 1 control connection after initialization.%0A    
         assertEq
@@ -1000,29 +1000,33 @@
 getValue(), 
-0
+1
 );%0A%0A
+    
         // e
@@ -1099,32 +1099,36 @@
 l size.%0A        
+   
+ 
 Session session 
@@ -1140,32 +1140,36 @@
 ster.connect();%0A
+    
         assertEq
@@ -1210,32 +1210,36 @@
 (), 1);%0A        
+    
 assertEquals((in
@@ -1315,32 +1315,36 @@
 Size);%0A%0A        
+ 
+   
 // ensure sessio
@@ -1429,16 +1429,20 @@
         
+    
 session.
@@ -1450,32 +1450,36 @@
 lose();%0A        
+  
+  
 assertEquals(clu
@@ -1504,32 +1504,36 @@
 ons.size(), 0);%0A
+    
         assertEq
@@ -1606,56 +1606,8 @@
 );%0A%0A
-        try %7B%0A            Session thisSession;%0A%0A
     
@@ -1684,18 +1684,16 @@
 ections%0A
-%0A%0A
         
@@ -2063,32 +2063,40 @@
 st.%0A            
+Session 
 thisSession = cl
@@ -2490,17 +2490,16 @@
 (), 0);%0A
-%0A
         
@@ -2579,18 +2579,16 @@
 (), 1);%0A
-%0A%0A
         
@@ -2603,231 +2603,8 @@
 y %7B%0A
-            // ensure we decommission node2 for the rest of the tests%0A%0A            assertEquals(cluster.manager.sessions.size(), 0);%0A            assertEquals((int) cluster.getMetrics().getOpenConnections().getValue(), 1);%0A%0A
     
@@ -2657,33 +2657,32 @@
 luster.close();%0A
-%0A
             %7D%0A%0A 
@@ -2679,17 +2679,16 @@
       %7D%0A
-%0A
         
@@ -2766,18 +2766,16 @@
       %7D%0A
-%0A%0A
         

Update SessionLeakTest to init Cluster first, be more resilient.