Fix flaky Elasticsearch tests
The tests were failing sometimes with a failure to create an index which already existed. Instead of creating and deleting indices between every test, create unique indices by prefixing the indices name with test method name. The indices are now clean up after all the tests are executed, when the server is stopped and temp folder deleted. Change-Id: I6539845e09af56183dd315f112a35d572db7c920
This commit is contained in:
@@ -21,7 +21,6 @@ import com.google.inject.Guice;
|
||||
import com.google.inject.Injector;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import org.eclipse.jgit.lib.Config;
|
||||
import org.junit.After;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.BeforeClass;
|
||||
|
||||
@@ -35,7 +34,6 @@ public class ElasticQueryAccountsTest extends AbstractQueryAccountsTest {
|
||||
return;
|
||||
}
|
||||
nodeInfo = ElasticTestUtils.startElasticsearchNode();
|
||||
ElasticTestUtils.createAllIndexes(nodeInfo);
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
@@ -47,19 +45,13 @@ public class ElasticQueryAccountsTest extends AbstractQueryAccountsTest {
|
||||
}
|
||||
}
|
||||
|
||||
@After
|
||||
public void cleanupIndex() {
|
||||
if (nodeInfo != null) {
|
||||
ElasticTestUtils.deleteAllIndexes(nodeInfo);
|
||||
ElasticTestUtils.createAllIndexes(nodeInfo);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Injector createInjector() {
|
||||
Config elasticsearchConfig = new Config(config);
|
||||
InMemoryModule.setDefaults(elasticsearchConfig);
|
||||
ElasticTestUtils.configure(elasticsearchConfig, nodeInfo.port);
|
||||
String indicesPrefix = testName.getMethodName().toLowerCase() + "_";
|
||||
ElasticTestUtils.configure(elasticsearchConfig, nodeInfo.port, indicesPrefix);
|
||||
ElasticTestUtils.createAllIndexes(nodeInfo, indicesPrefix);
|
||||
return Guice.createInjector(new InMemoryModule(elasticsearchConfig, notesMigration));
|
||||
}
|
||||
}
|
||||
|
@@ -23,12 +23,15 @@ import com.google.inject.Injector;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import org.eclipse.jgit.junit.TestRepository;
|
||||
import org.eclipse.jgit.lib.Config;
|
||||
import org.junit.After;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.TestName;
|
||||
|
||||
public class ElasticQueryChangesTest extends AbstractQueryChangesTest {
|
||||
@Rule public final TestName testName = new TestName();
|
||||
|
||||
private static ElasticNodeInfo nodeInfo;
|
||||
|
||||
@BeforeClass
|
||||
@@ -38,16 +41,6 @@ public class ElasticQueryChangesTest extends AbstractQueryChangesTest {
|
||||
return;
|
||||
}
|
||||
nodeInfo = ElasticTestUtils.startElasticsearchNode();
|
||||
|
||||
ElasticTestUtils.createAllIndexes(nodeInfo);
|
||||
}
|
||||
|
||||
@After
|
||||
public void cleanupIndex() {
|
||||
if (nodeInfo != null) {
|
||||
ElasticTestUtils.deleteAllIndexes(nodeInfo);
|
||||
ElasticTestUtils.createAllIndexes(nodeInfo);
|
||||
}
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
@@ -63,7 +56,9 @@ public class ElasticQueryChangesTest extends AbstractQueryChangesTest {
|
||||
protected Injector createInjector() {
|
||||
Config elasticsearchConfig = new Config(config);
|
||||
InMemoryModule.setDefaults(elasticsearchConfig);
|
||||
ElasticTestUtils.configure(elasticsearchConfig, nodeInfo.port);
|
||||
String indicesPrefix = testName.getMethodName().toLowerCase() + "_";
|
||||
ElasticTestUtils.configure(elasticsearchConfig, nodeInfo.port, indicesPrefix);
|
||||
ElasticTestUtils.createAllIndexes(nodeInfo, indicesPrefix);
|
||||
return Guice.createInjector(new InMemoryModule(elasticsearchConfig, notesMigration));
|
||||
}
|
||||
|
||||
|
@@ -21,7 +21,6 @@ import com.google.inject.Guice;
|
||||
import com.google.inject.Injector;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import org.eclipse.jgit.lib.Config;
|
||||
import org.junit.After;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.BeforeClass;
|
||||
|
||||
@@ -35,7 +34,6 @@ public class ElasticQueryGroupsTest extends AbstractQueryGroupsTest {
|
||||
return;
|
||||
}
|
||||
nodeInfo = ElasticTestUtils.startElasticsearchNode();
|
||||
ElasticTestUtils.createAllIndexes(nodeInfo);
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
@@ -47,19 +45,13 @@ public class ElasticQueryGroupsTest extends AbstractQueryGroupsTest {
|
||||
}
|
||||
}
|
||||
|
||||
@After
|
||||
public void cleanupIndex() {
|
||||
if (nodeInfo != null) {
|
||||
ElasticTestUtils.deleteAllIndexes(nodeInfo);
|
||||
ElasticTestUtils.createAllIndexes(nodeInfo);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Injector createInjector() {
|
||||
Config elasticsearchConfig = new Config(config);
|
||||
InMemoryModule.setDefaults(elasticsearchConfig);
|
||||
ElasticTestUtils.configure(elasticsearchConfig, nodeInfo.port);
|
||||
String indicesPrefix = testName.getMethodName().toLowerCase() + "_";
|
||||
ElasticTestUtils.configure(elasticsearchConfig, nodeInfo.port, indicesPrefix);
|
||||
ElasticTestUtils.createAllIndexes(nodeInfo, indicesPrefix);
|
||||
return Guice.createInjector(new InMemoryModule(elasticsearchConfig, notesMigration));
|
||||
}
|
||||
}
|
||||
|
@@ -66,11 +66,12 @@ final class ElasticTestUtils {
|
||||
}
|
||||
}
|
||||
|
||||
static void configure(Config config, String port) {
|
||||
static void configure(Config config, String port, String prefix) {
|
||||
config.setEnum("index", null, "type", IndexType.ELASTICSEARCH);
|
||||
config.setString("elasticsearch", "test", "protocol", "http");
|
||||
config.setString("elasticsearch", "test", "hostname", "localhost");
|
||||
config.setString("elasticsearch", "test", "port", port);
|
||||
config.setString("elasticsearch", null, "prefix", prefix);
|
||||
}
|
||||
|
||||
static ElasticNodeInfo startElasticsearchNode() throws InterruptedException, ExecutionException {
|
||||
@@ -117,7 +118,7 @@ final class ElasticTestUtils {
|
||||
Map<String, NodeInfo> nodes;
|
||||
}
|
||||
|
||||
static void createAllIndexes(ElasticNodeInfo nodeInfo) {
|
||||
static void createAllIndexes(ElasticNodeInfo nodeInfo, String prefix) {
|
||||
Schema<ChangeData> changeSchema = ChangeSchemaDefinitions.INSTANCE.getLatest();
|
||||
ChangeMapping openChangesMapping = new ChangeMapping(changeSchema);
|
||||
ChangeMapping closedChangesMapping = new ChangeMapping(changeSchema);
|
||||
@@ -128,7 +129,7 @@ final class ElasticTestUtils {
|
||||
.client()
|
||||
.admin()
|
||||
.indices()
|
||||
.prepareCreate(String.format("%s_%04d", CHANGES, changeSchema.getVersion()))
|
||||
.prepareCreate(String.format("%s%s_%04d", prefix, CHANGES, changeSchema.getVersion()))
|
||||
.addMapping(OPEN_CHANGES, gson.toJson(openChangesMapping))
|
||||
.addMapping(CLOSED_CHANGES, gson.toJson(closedChangesMapping))
|
||||
.execute()
|
||||
@@ -141,7 +142,7 @@ final class ElasticTestUtils {
|
||||
.client()
|
||||
.admin()
|
||||
.indices()
|
||||
.prepareCreate(String.format("%s_%04d", ACCOUNTS, accountSchema.getVersion()))
|
||||
.prepareCreate(String.format("%s%s_%04d", prefix, ACCOUNTS, accountSchema.getVersion()))
|
||||
.addMapping(ElasticAccountIndex.ACCOUNTS, gson.toJson(accountMapping))
|
||||
.execute()
|
||||
.actionGet();
|
||||
@@ -153,7 +154,7 @@ final class ElasticTestUtils {
|
||||
.client()
|
||||
.admin()
|
||||
.indices()
|
||||
.prepareCreate(String.format("%s_%04d", GROUPS, groupSchema.getVersion()))
|
||||
.prepareCreate(String.format("%s%s_%04d", prefix, GROUPS, groupSchema.getVersion()))
|
||||
.addMapping(ElasticGroupIndex.GROUPS, gson.toJson(groupMapping))
|
||||
.execute()
|
||||
.actionGet();
|
||||
|
Reference in New Issue
Block a user