Skip to content

Commit

Permalink
fix failing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ericvergnaud committed Oct 12, 2021
1 parent 6970815 commit cd5e1e8
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 24 deletions.
69 changes: 54 additions & 15 deletions MongoStore/src/test/java/prompto/store/mongo/BaseMongoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@

import java.io.IOException;
import java.util.Collections;
import java.util.Timer;
import java.util.TimerTask;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.slf4j.LoggerFactory;

import com.mongodb.client.MongoDatabase;
Expand All @@ -28,20 +32,27 @@

public abstract class BaseMongoTest {

protected int mongoPort;
MongodExecutable mongo;
static protected MongodExecutable mongo = null;
static protected int mongoPort = 0;
static protected Timer mongoStopper = null;
protected MongoStore store;
protected MongoDatabase db;

@Before
public void __before__() throws IOException {

@BeforeClass
public static synchronized void __before_class__() throws IOException {
if(mongoPort==0)
mongoPort = Network.getFreeServerPort();
mongo = startMongo(mongoPort);
TempDirectories.create();
Mode.set(Mode.UNITTEST);
if(mongo==null)
mongo = startMongo(mongoPort);
}


@AfterClass
public static synchronized void __after_class__() throws IOException {

}

public static MongodExecutable startMongo(int mongoPort) throws IOException {
IRuntimeConfig runtimeConfig = new RuntimeConfigBuilder()
.defaultsWithLogger(Command.MongoD, LoggerFactory.getLogger(BaseMongoTest.class.getName()))
Expand All @@ -57,19 +68,47 @@ public static MongodExecutable startMongo(int mongoPort) throws IOException {
return mongo;
}

@After
public void __after__() throws IOException {
if (mongo != null)
stopMongo(mongo);
}

public static void stopMongo(MongodExecutable mongo) {
mongo.stop();
if (mongo != null) {
System.out.println("Stopping shared Mongo instance");
mongo.stop();
}
}

@Before
public synchronized void __before__() throws IOException {
if(mongoStopper!=null) {
mongoStopper.cancel();
mongoStopper = null;
}
TempDirectories.create();
Mode.set(Mode.UNITTEST);
}

@After
public synchronized void __after__() throws IOException {
if(mongoStopper==null) {
mongoStopper = new Timer("Stop Mongo");
mongoStopper.schedule(new TimerTask() {

@Override
public void run() {
stopMongo(mongo);
mongo = null;
mongoPort = 0;
mongoStopper.cancel();
mongoStopper = null;
}

}, 5000);
}
}

protected MongoStore createStore(String name) {
System.out.println("Creating new store from shared Mongo instance");
store = new MongoStore("localhost", mongoPort, name, false);
DataStore.setInstance(store);
DataStore.setGlobal(store);
DataStore.useGlobal();
db = store.db;
return store;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class TestQuery extends BaseMongoTest {

@Before
public void before() throws Exception {
createStore("TestQuery");
createStore("TestQuery_" + System.currentTimeMillis());
context = Context.newGlobalsContext();
registerDbIdAttribute();
registerNameAttribute();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class TestStoreLoginSource_Mongo extends BaseMongoTest {

@Before
public void before() {
createStore("LOGIN");
createStore("LOGIN_" + System.currentTimeMillis());
StoredUserInfoCache cache = new StoredUserInfoCache(store);
source = new StoredPasswordDigestAuthenticationSource(cache);
}
Expand Down
1 change: 1 addition & 0 deletions Server/src/test/java/prompto/server/TestRemoteExec.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public class TestRemoteExec extends BaseUITest {
public void before() throws Exception {
ApplicationContext.reset();
DataStore.setGlobal(new MemStore());
DataStore.useGlobal();
Standalone.synchronizeSchema(ICodeStore.getInstance(), DataStore.getInstance());
}

Expand Down
1 change: 1 addition & 0 deletions Server/src/test/java/prompto/server/TestScheduler.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public class TestScheduler extends BaseUITest {
public void before() throws Exception {
ApplicationContext.reset();
DataStore.setGlobal(new MemStore());
DataStore.useGlobal();
Standalone.synchronizeSchema(ICodeStore.getInstance(), DataStore.getInstance());
}

Expand Down
33 changes: 27 additions & 6 deletions Server/src/test/java/prompto/store/TestRemoteMongoStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,30 @@

import java.io.IOException;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.experimental.categories.Category;

import prompto.server.HeadlessTests;
import prompto.store.mongo.BaseMongoTest;
import prompto.store.mongo.MongoStore;

@Category(HeadlessTests.class)
public class TestRemoteMongoStore extends TestRemoteStoreBase {

static class MongoTest extends BaseMongoTest {

public static void beforeClass() throws IOException {
__before_class__();
}

public static void afterClass() throws IOException {
__after_class__();
}

public void before() throws IOException {
__before__();
}
Expand All @@ -23,8 +34,8 @@ public void after() throws IOException {
__after__();
}

public IStore newStore(String name) {
return createStore(name);
public MongoStore createStore(String name) {
return super.createStore(name);
}

}
Expand All @@ -33,19 +44,29 @@ public IStore newStore(String name) {

@BeforeClass
public static void beforeClass() throws Exception {
mongo.before();
MongoTest.beforeClass();
}

@AfterClass
public static void after() throws IOException {
mongo.after();
public static void afterClass() throws Exception {
MongoTest.afterClass();
}

@Before
public void before() throws Exception {
mongo.before();
}

@After
public void after() throws Exception {
mongo.after();
}

static int counter = 0;

@Override
protected IStore getDataStore() {
return mongo.newStore("STUFF_" + counter++);
return mongo.createStore("STUFF_" + counter++);
}

@Ignore
Expand Down

0 comments on commit cd5e1e8

Please sign in to comment.