package com.gentics.mesh.search.migration;

import static com.gentics.mesh.test.TestSize.FULL;
import static com.gentics.mesh.test.context.ElasticsearchTestMode.CONTAINER;

import java.util.concurrent.TimeUnit;
import java.util.stream.IntStream;

import org.junit.Test;

import com.gentics.mesh.core.rest.node.NodeResponse;
import com.gentics.mesh.search.AbstractNodeSearchEndpointTest;
import com.gentics.mesh.search.verticle.eventhandler.SyncEventHandler;
import com.gentics.mesh.test.context.MeshTestSetting;

@MeshTestSetting(elasticsearch = CONTAINER, testSize = FULL, startServer = true)
public class NodeMigrationDuringSyncTest extends AbstractNodeSearchEndpointTest {

	@Test
	public void testSyncDuringMigration() throws Exception {
		recreateIndices();
		NodeResponse parent = createNode();
		// Create some nodes for load during migration
		IntStream.range(0, 1000).forEach(i -> createNode(parent));
		waitForSearchIdleEvent();

		migrateSchema("folder", false).blockingAwait();

		// Wait so that sync does not happen at the beginning of the migration
		Thread.sleep(2000);
		SyncEventHandler.invokeSyncCompletable().blockingAwait(30, TimeUnit.SECONDS);

		waitForSearchIdleEvent();
	}
}

@@ -37,16 +37,82 @@
 ation;%0A%0A
+import static com.gentics.mesh.assertj.MeshAssertions.assertThat;%0A
 import s
@@ -154,16 +154,16 @@
 e.FULL;%0A
-
 import s
@@ -270,16 +270,66 @@
 meUnit;%0A
+import java.util.concurrent.atomic.AtomicInteger;%0A
 import j
@@ -380,16 +380,61 @@
 .Test;%0A%0A
+import com.gentics.mesh.core.rest.MeshEvent;%0A
 import c
@@ -478,16 +478,67 @@
 sponse;%0A
+import com.gentics.mesh.rest.client.MeshWebsocket;%0A
 import c
@@ -1196,16 +1196,238 @@
 ait();%0A%0A
+%09%09AtomicInteger fullSyncs = new AtomicInteger(0);%0A%09%09MeshWebsocket eventbus = client().eventbus();%0A%09%09eventbus.registerEvents(MeshEvent.INDEX_SYNC_REQUEST);%0A%09%09eventbus.events().subscribe(ev -%3E fullSyncs.incrementAndGet());%0A%0A
 %09%09// Wai
@@ -1510,9 +1510,9 @@
 eep(
-2
+1
 000)
@@ -1594,16 +1594,16 @@
 ONDS);%0A%0A
-
 %09%09waitFo
@@ -1621,14 +1621,79 @@
 Event();
+%0A%0A%09%09assertThat(fullSyncs.get()).isEqualTo(1);%0A%09%09eventbus.close();
 %0A%09%7D%0A%7D%0A

Add assertion to test