package com.nokia.dempsy;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.junit.Ignore;

import com.nokia.dempsy.config.ClusterId;
import com.nokia.dempsy.mpcluster.MpCluster;
import com.nokia.dempsy.mpcluster.MpClusterException;
import com.nokia.dempsy.mpcluster.MpClusterSession;
import com.nokia.dempsy.mpcluster.MpClusterSlot;
import com.nokia.dempsy.router.ClusterInformation;
import com.nokia.dempsy.router.DefaultRoutingStrategy;
import com.nokia.dempsy.router.SlotInformation;

@Ignore
public class TestUtils
{
   public static interface Condition<T>
   {
      public boolean conditionMet(T o);
   }

   public static <T> boolean poll(long timeoutMillis, T userObject, Condition<T> condition) throws InterruptedException
   {
      for (long endTime = System.currentTimeMillis() + timeoutMillis;
            endTime > System.currentTimeMillis() && !condition.conditionMet(userObject);)
         Thread.sleep(1);
      return condition.conditionMet(userObject);
   }
   
   public static boolean waitForClustersToBeInitialized(long timeoutMillis, 
         final int numSlotsPerCluster, Dempsy dempsy) throws InterruptedException
   {
      try
      {
         final List<ClusterId> clusters = new ArrayList<ClusterId>();
         
         // find out all of the ClusterIds
         for (Dempsy.Application app : dempsy.applications)
         {
            for (Dempsy.Application.Cluster cluster : app.appClusters)
               if (!cluster.clusterDefinition.isRouteAdaptorType())
                  clusters.add(new ClusterId(cluster.clusterDefinition.getClusterId()));
         }
         
         return poll(timeoutMillis, dempsy.clusterSessionFactory.createSession(), new Condition<MpClusterSession<ClusterInformation, SlotInformation>>()
         {
            @Override
            public boolean conditionMet(MpClusterSession<ClusterInformation, SlotInformation> session)
            {
               try
               {
                  for (ClusterId c : clusters)
                  {
                     MpCluster<ClusterInformation, SlotInformation> cluster = session.getCluster(c);
                     Collection<MpClusterSlot<SlotInformation>> slots = cluster.getActiveSlots();
                     if (slots == null || slots.size() != numSlotsPerCluster)
                        return false;
                  }
               }
               catch(MpClusterException e)
               {
                  return false;
               }

               return DefaultRoutingStrategy.allOutboundsInitialized();
            }
         });
      }
      catch (MpClusterException e)
      {
         return false;
      }
   }
}

@@ -1655,79 +1655,168 @@
     
-return poll(timeoutMillis, dempsy.clusterSessionFactory.createSession()
+MpClusterSession%3CClusterInformation, SlotInformation%3E session = dempsy.clusterSessionFactory.createSession();%0A         boolean ret = poll(timeoutMillis, session
 , ne
@@ -2698,16 +2698,72 @@
     %7D);%0A
+         %0A         session.stop();%0A         return ret;%0A
       %7D%0A

Make sure MpClusterSessions that are opened for testing purposes are cleaned up.