package com.orientechnologies.orient.core.db.document;

import com.orientechnologies.orient.core.db.ODatabaseRecordThreadLocal;
import com.orientechnologies.orient.core.db.OPartitionedDatabasePool;
import com.orientechnologies.orient.core.exception.ODatabaseException;
import org.testng.annotations.Test;

import static org.testng.AssertJUnit.assertNull;

public class ODatabaseDocumentPoolOpenCloseTest {

  @Test
  public void openCloseClearThreadLocal() {
    String url = "memory:" + ODatabaseDocumentPoolOpenCloseTest.class.getSimpleName();
    ODatabaseDocument dbo = new ODatabaseDocumentTx(url).create();
    OPartitionedDatabasePool pool = new OPartitionedDatabasePool(url, "admin", "admin");
    try {
      ODatabaseDocument db = pool.acquire();
      db.close();
      assertNull(ODatabaseRecordThreadLocal.INSTANCE.getIfDefined());
    } finally {
      pool.close();

      dbo.activateOnCurrentThread();
      dbo.drop();
    }
  }

  @Test(expectedExceptions = ODatabaseException.class)
  public void failureOpenPoolDatabase() {

    String url = "memory:" + ODatabaseDocumentPoolOpenCloseTest.class.getSimpleName();
    ODatabaseDocument dbo = new ODatabaseDocumentTx(url).create();
    OPartitionedDatabasePool pool = new OPartitionedDatabasePool(url, "admin", "admin");
    try {
      ODatabaseDocument db = pool.acquire();
      db.open("admin", "admin");
    } finally {
      pool.close();

      dbo.activateOnCurrentThread();
      dbo.drop();
    }

  }

}

@@ -273,37 +273,186 @@
 ort 
-org.testng.annotations.Test;%0A
+com.orientechnologies.orient.core.metadata.schema.OClass;%0Aimport org.testng.annotations.Test;%0A%0Aimport java.util.concurrent.*;%0A%0Aimport static org.testng.AssertJUnit.assertNotNull;
 %0Aimp
@@ -1623,10 +1623,1064 @@
 %7D%0A%0A  %7D%0A%0A
+%0A  @Test%0A  public void checkSchemaRefresh() throws ExecutionException, InterruptedException %7B%0A    String url = %22memory:%22 + ODatabaseDocumentPoolOpenCloseTest.class.getSimpleName();%0A    ODatabaseDocument dbo = new ODatabaseDocumentTx(url).create();%0A    final OPartitionedDatabasePool pool = new OPartitionedDatabasePool(url, %22admin%22, %22admin%22);%0A    try %7B%0A      ODatabaseDocument db = pool.acquire();%0A      ExecutorService exec = Executors.newSingleThreadExecutor();%0A      Future f = exec.submit(new Callable%3CObject%3E() %7B%0A%0A        @Override%0A        public Object call() throws Exception %7B%0A          ODatabaseDocument db1 = pool.acquire();%0A          db1.getMetadata().getSchema().createClass(%22Test%22);%0A          db1.close();%0A          return null;%0A        %7D%0A      %7D);%0A      f.get();%0A%0A      exec.shutdown();%0A%0A      db.activateOnCurrentThread();%0A      OClass clazz = db.getMetadata().getSchema().getClass(%22Test%22);%0A      assertNotNull(clazz);%0A      db.close();%0A    %7D finally %7B%0A      pool.close();%0A      dbo.activateOnCurrentThread();%0A      dbo.drop();%0A    %7D%0A  %7D%0A%0A
 %7D%0A

add test on the schema update cross pool instances, issue #5279