Commit 8edadbf97889fbd80289a72209fdd485ddea3521

Edward Thomson 2016-03-07T17:37:17

index::racy: force racy entry Instead of hoping that we can get a racy entry by going real fast and praying real hard, just create a racy entry.

diff --git a/tests/index/racy.c b/tests/index/racy.c
index 68aa460..1768f5e 100644
--- a/tests/index/racy.c
+++ b/tests/index/racy.c
@@ -105,8 +105,8 @@ static void setup_race(void)
 {
 	git_buf path = GIT_BUF_INIT;
 	git_index *index;
-	const git_index_entry *entry;
-	int i, found_race = 0;
+	git_index_entry *entry;
+	struct stat st;
 
 	/* Make sure we do have a timestamp */
 	cl_git_pass(git_repository_index__weakptr(&index, g_repo));
@@ -114,27 +114,20 @@ static void setup_race(void)
 
 	cl_git_pass(git_buf_joinpath(&path, git_repository_workdir(g_repo), "A"));
 
-	/* Make sure writing the file, adding and rewriting happen in the same second */
-	for (i = 0; i < 10; i++) {
-		struct stat st;
-		cl_git_mkfile(path.ptr, "A");
+	cl_git_mkfile(path.ptr, "A");
+	cl_git_pass(git_index_add_bypath(index, "A"));
 
-		cl_git_pass(git_index_add_bypath(index, "A"));
-		cl_git_mkfile(path.ptr, "B");
-		cl_git_pass(git_index_write(index));
+	cl_git_mkfile(path.ptr, "B");
+	cl_git_pass(git_index_write(index));
 
-		cl_git_mkfile(path.ptr, "");
+	cl_git_mkfile(path.ptr, "");
 
-		cl_git_pass(p_stat(path.ptr, &st));
-		cl_assert(entry = git_index_get_bypath(index, "A", 0));
-		if (entry->mtime.seconds == (int32_t) st.st_mtime) {
-			found_race = 1;
-			break;
-		}
-	}
+	cl_git_pass(p_stat(path.ptr, &st));
+	cl_assert(entry = (git_index_entry *)git_index_get_bypath(index, "A", 0));
 
-	if (!found_race)
-		cl_fail("failed to find race after 10 attempts");
+	/* force a race */
+	entry->mtime.seconds = st.st_mtime;
+	entry->mtime.nanoseconds = st.st_mtime_nsec;
 
 	git_buf_free(&path);
 }