Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThomas Wolf2019-11-22 05:42:55 -0500
committerThomas Wolf2019-11-22 06:46:06 -0500
commit89de01fb8835e9840f302a78d3f8f8b342097294 (patch)
treea74369795b455b29c80d3bdc680057baacb6d2a9
parent51127a18ec2f74cdbaf45978f6400ef702cbae52 (diff)
downloadegit-89de01fb8835e9840f302a78d3f8f8b342097294.tar.gz
egit-89de01fb8835e9840f302a78d3f8f8b342097294.tar.xz
egit-89de01fb8835e9840f302a78d3f8f8b342097294.zip
[tests] Try to stabilize IndexDiffCacheTest
This test waits for a listener to be called. IndexDiff calculations happen in the background. So when several files are added to the workspace, several calculations may get scheduled, and the test may end up not waiting long enough to pick up the expected change but operate on an IndexDiff computed before the wanted file was added. Run multiple workspace changes in a workspace operation with IWorkspace.AVOID_UPDATE. Change-Id: Ia58db7a5f9ad1cf7dedec387d8b4499a87c69fbc Signed-off-by: Thomas Wolf <thomas.wolf@paranor.ch>
-rw-r--r--org.eclipse.egit.core.test/src/org/eclipse/egit/core/internal/indexdiff/IndexDiffCacheTest.java86
1 files changed, 62 insertions, 24 deletions
diff --git a/org.eclipse.egit.core.test/src/org/eclipse/egit/core/internal/indexdiff/IndexDiffCacheTest.java b/org.eclipse.egit.core.test/src/org/eclipse/egit/core/internal/indexdiff/IndexDiffCacheTest.java
index 166f5f92f..b92e36f66 100644
--- a/org.eclipse.egit.core.test/src/org/eclipse/egit/core/internal/indexdiff/IndexDiffCacheTest.java
+++ b/org.eclipse.egit.core.test/src/org/eclipse/egit/core/internal/indexdiff/IndexDiffCacheTest.java
@@ -18,10 +18,14 @@ import static org.junit.Assert.fail;
import java.io.OutputStream;
import java.nio.file.Files;
+import java.util.concurrent.Callable;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
import org.eclipse.core.resources.IFile;
+import org.eclipse.core.resources.IWorkspace;
+import org.eclipse.core.resources.ResourcesPlugin;
+import org.eclipse.core.runtime.CoreException;
import org.eclipse.egit.core.Activator;
import org.eclipse.egit.core.op.ConnectProviderOperation;
import org.eclipse.egit.core.test.GitTestCase;
@@ -107,18 +111,23 @@ public class IndexDiffCacheTest extends GitTestCase {
testRepository.connect(project.project);
testRepository.addToIndex(project.project);
testRepository.createInitialCommit("testAddFileFromUntrackedFolder\n\nfirst commit\n");
- prepareCacheEntry();
- project.createFolder("folder");
- project.createFolder("folder/a");
- project.createFolder("folder/b");
- IFile fileA = project.createFile("folder/a/file", new byte[] {});
- project.createFile("folder/b/file", new byte[] {});
+ IFile[] fileA = { null };
+ runInWorkspace(() -> {
+ project.createFolder("folder");
+ project.createFolder("folder/a");
+ project.createFolder("folder/b");
+ fileA[0] = project.createFile("folder/a/file", new byte[] {});
+ project.createFile("folder/b/file", new byte[] {});
+ return null;
+ });
+
+ prepareCacheEntry();
IndexDiffData data1 = waitForListenerCalled();
assertThat(data1.getUntrackedFolders(), hasItem("Project-1/folder/"));
- testRepository.track(fileA.getLocation().toFile());
+ testRepository.track(fileA[0].getLocation().toFile());
IndexDiffData data2 = waitForListenerCalled();
assertThat(data2.getAdded(), hasItem("Project-1/folder/a/file"));
@@ -130,10 +139,13 @@ public class IndexDiffCacheTest extends GitTestCase {
@Test
public void testAddIgnoredFolder() throws Exception {
testRepository.connect(project.project);
- project.createFile(".gitignore", "ignore\n".getBytes("UTF-8"));
- project.createFolder("ignore");
- project.createFile("ignore/file.txt", new byte[] {});
- project.createFolder("sub");
+ runInWorkspace(() -> {
+ project.createFile(".gitignore", "ignore\n".getBytes("UTF-8"));
+ project.createFolder("ignore");
+ project.createFile("ignore/file.txt", new byte[] {});
+ project.createFolder("sub");
+ return null;
+ });
testRepository.addToIndex(project.project);
testRepository.createInitialCommit("testAddFileInIgnoredFolder\n\nfirst commit\n");
prepareCacheEntry();
@@ -160,9 +172,13 @@ public class IndexDiffCacheTest extends GitTestCase {
@Test
public void testRemoveIgnoredFile() throws Exception {
testRepository.connect(project.project);
- project.createFile(".gitignore", "ignore\n".getBytes("UTF-8"));
- project.createFolder("sub");
- IFile file = project.createFile("sub/ignore", new byte[] {});
+ IFile file[] = { null };
+ runInWorkspace(() -> {
+ project.createFile(".gitignore", "ignore\n".getBytes("UTF-8"));
+ project.createFolder("sub");
+ file[0] = project.createFile("sub/ignore", new byte[] {});
+ return null;
+ });
testRepository.addToIndex(project.project);
testRepository.createInitialCommit("testRemoveIgnoredFile\n\nfirst commit\n");
IndexDiffCacheEntry entry = prepareCacheEntry();
@@ -177,7 +193,7 @@ public class IndexDiffCacheTest extends GitTestCase {
IndexDiffData data2 = waitForListenerCalled();
assertThat(data2.getIgnoredNotInIndex(), hasItem("Project-1/sub/ignore"));
- file.delete(false, null);
+ file[0].delete(false, null);
waitForListenerNotCalled();
entry.refresh(); // need explicit as ignored file shall not trigger.
@@ -188,9 +204,12 @@ public class IndexDiffCacheTest extends GitTestCase {
@Test
public void testAddAndRemoveGitIgnoreFileToIgnoredDir() throws Exception {
testRepository.connect(project.project);
- project.createFile(".gitignore", "ignore\n".getBytes("UTF-8"));
- project.createFolder("sub");
- project.createFile("sub/ignore", new byte[] {});
+ runInWorkspace(() -> {
+ project.createFile(".gitignore", "ignore\n".getBytes("UTF-8"));
+ project.createFolder("sub");
+ project.createFile("sub/ignore", new byte[] {});
+ return null;
+ });
testRepository.addToIndex(project.project);
testRepository
.createInitialCommit("testRemoveIgnoredFile\n\nfirst commit\n");
@@ -202,6 +221,8 @@ public class IndexDiffCacheTest extends GitTestCase {
project.createFile("sub/ignored", "Ignored".getBytes("UTF-8"));
+ waitForListenerCalled();
+
// adding this file will trigger a refresh, so no manual refresh must be
// required.
project.createFile("sub/.gitignore", "ignored\n".getBytes("UTF-8"));
@@ -220,9 +241,12 @@ public class IndexDiffCacheTest extends GitTestCase {
@Test
public void testAddAndRemoveFileToIgnoredDir() throws Exception {
testRepository.connect(project.project);
- project.createFile(".gitignore", "sub\n".getBytes("UTF-8"));
- project.createFolder("sub");
- project.createFile("sub/ignore", new byte[] {});
+ runInWorkspace(() -> {
+ project.createFile(".gitignore", "sub\n".getBytes("UTF-8"));
+ project.createFolder("sub");
+ project.createFile("sub/ignore", new byte[] {});
+ return null;
+ });
testRepository.addToIndex(project.project);
testRepository
.createInitialCommit("testRemoveIgnoredFile\n\nfirst commit\n");
@@ -243,9 +267,12 @@ public class IndexDiffCacheTest extends GitTestCase {
@Test
public void testModifyFileInIgnoredDir() throws Exception {
testRepository.connect(project.project);
- project.createFile(".gitignore", "ignore\n".getBytes("UTF-8"));
- project.createFolder("sub");
- project.createFile("sub/ignore", new byte[] {});
+ runInWorkspace(() -> {
+ project.createFile(".gitignore", "ignore\n".getBytes("UTF-8"));
+ project.createFolder("sub");
+ project.createFile("sub/ignore", new byte[] {});
+ return null;
+ });
testRepository.addToIndex(project.project);
testRepository
.createInitialCommit("testRemoveIgnoredFile\n\nfirst commit\n");
@@ -265,6 +292,17 @@ public class IndexDiffCacheTest extends GitTestCase {
waitForListenerNotCalled();
}
+ private void runInWorkspace(Callable<Void> action) throws CoreException {
+ ResourcesPlugin.getWorkspace().run(m -> {
+ try {
+ action.call();
+ } catch (Exception e) {
+ throw new CoreException(
+ Activator.error("Test preparation error", e));
+ }
+ }, project.getProject(), IWorkspace.AVOID_UPDATE, null);
+ }
+
private IndexDiffCacheEntry prepareCacheEntry() {
listenerCalled.set(false);
indexDiffDataResult.set(null);

Back to the top