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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
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);
}