/*
 * Copyright 2014 Red Hat, Inc.
 *
 *  All rights reserved. This program and the accompanying materials
 *  are made available under the terms of the Eclipse Public License v1.0
 *  and Apache License v2.0 which accompanies this distribution.
 *
 *  The Eclipse Public License is available at
 *  http://www.eclipse.org/legal/epl-v10.html
 *
 *  The Apache License v2.0 is available at
 *  http://www.opensource.org/licenses/apache2.0.php
 *
 *  You may elect to redistribute this code under either of these licenses.
 */

package io.vertx.ext.asyncsql;

import io.vertx.core.json.JsonObject;
import io.vertx.core.shareddata.LocalMap;
import io.vertx.test.core.VertxTestBase;
import org.junit.Test;

/**
 * @author <a href="http://tfox.org">Tim Fox</a>
 */
public class RefCountTest extends VertxTestBase {

  private LocalMap<String, Object> getLocalMap() {
    return vertx.sharedData().getLocalMap("__vertx.MySQLPostgreSQL.pools.MySQL");
  }

  @Test
  public void testNonShared() {
    LocalMap<String, Object> map = getLocalMap();
    JsonObject config = new JsonObject();
    AsyncSQLClient client1 = MySQLClient.createNonShared(vertx, config);
    assertEquals(1, map.size());
    AsyncSQLClient client2 = MySQLClient.createNonShared(vertx, config);
    assertEquals(2, map.size());
    AsyncSQLClient client3 = MySQLClient.createNonShared(vertx, config);
    assertEquals(3, map.size());
    client1.close();
    assertEquals(2, map.size());
    client2.close();
    assertEquals(1, map.size());
    client3.close();
    assertEquals(0, map.size());
    waitUntil(() -> getLocalMap().size() == 0);
    waitUntil(() -> map != getLocalMap()); // Map has been closed
  }

  @Test
  public void testSharedDefault() throws Exception {
    LocalMap<String, Object> map = getLocalMap();
    JsonObject config = new JsonObject();
    AsyncSQLClient client1 = MySQLClient.createShared(vertx, config);
    assertEquals(1, map.size());
    AsyncSQLClient client2 = MySQLClient.createShared(vertx, config);
    assertEquals(1, map.size());
    AsyncSQLClient client3 = MySQLClient.createShared(vertx, config);
    assertEquals(1, map.size());
    client1.close();
    Thread.sleep(200);
    assertEquals(1, map.size());
    client2.close();
    assertEquals(1, map.size());
    client3.close();
    waitUntil(() -> map.size() == 0);
    waitUntil(() -> map != getLocalMap()); // Map has been closed
  }

  @Test
  public void testSharedNamed() throws Exception {
    LocalMap<String, Object> map = getLocalMap();
    JsonObject config = new JsonObject();
    AsyncSQLClient client1 = MySQLClient.createShared(vertx, config, "ds1");
    assertEquals(1, map.size());
    AsyncSQLClient client2 = MySQLClient.createShared(vertx, config, "ds1");
    assertEquals(1, map.size());
    AsyncSQLClient client3 = MySQLClient.createShared(vertx, config, "ds1");
    assertEquals(1, map.size());

    AsyncSQLClient client4 = MySQLClient.createShared(vertx, config, "ds2");
    assertEquals(2, map.size());
    AsyncSQLClient client5 = MySQLClient.createShared(vertx, config, "ds2");
    assertEquals(2, map.size());
    AsyncSQLClient client6 = MySQLClient.createShared(vertx, config, "ds2");
    assertEquals(2, map.size());

    client1.close();
    Thread.sleep(200);
    assertEquals(2, map.size());
    client2.close();
    assertEquals(2, map.size());
    client3.close();
    waitUntil(() -> map.size() == 1);

    client4.close();
    Thread.sleep(200);
    assertEquals(1, map.size());
    client5.close();
    assertEquals(1, map.size());
    client6.close();
    waitUntil(() -> map.size() == 0);
    waitUntil(() -> map != getLocalMap()); // Map has been closed
  }
}

@@ -2156,39 +2156,16 @@
 lose();%0A
-    Thread.sleep(200);%0A
     asse
@@ -3207,39 +3207,16 @@
 lose();%0A
-    Thread.sleep(200);%0A
     asse
@@ -3379,31 +3379,8 @@
 ();%0A
-    Thread.sleep(200);%0A
     

Remove unnecessary Thread.sleep