Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8edadbf

Browse files
author
Edward Thomson
committed
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 parent ccfacb8 commit 8edadbf

File tree

1 file changed

+12
-19
lines changed

1 file changed

+12
-19
lines changed

tests/index/racy.c

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -105,36 +105,29 @@ static void setup_race(void)
105105
{
106106
git_buf path = GIT_BUF_INIT;
107107
git_index *index;
108-
const git_index_entry *entry;
109-
int i, found_race = 0;
108+
git_index_entry *entry;
109+
struct stat st;
110110

111111
/* Make sure we do have a timestamp */
112112
cl_git_pass(git_repository_index__weakptr(&index, g_repo));
113113
cl_git_pass(git_index_write(index));
114114

115115
cl_git_pass(git_buf_joinpath(&path, git_repository_workdir(g_repo), "A"));
116116

117-
/* Make sure writing the file, adding and rewriting happen in the same second */
118-
for (i = 0; i < 10; i++) {
119-
struct stat st;
120-
cl_git_mkfile(path.ptr, "A");
117+
cl_git_mkfile(path.ptr, "A");
118+
cl_git_pass(git_index_add_bypath(index, "A"));
121119

122-
cl_git_pass(git_index_add_bypath(index, "A"));
123-
cl_git_mkfile(path.ptr, "B");
124-
cl_git_pass(git_index_write(index));
120+
cl_git_mkfile(path.ptr, "B");
121+
cl_git_pass(git_index_write(index));
125122

126-
cl_git_mkfile(path.ptr, "");
123+
cl_git_mkfile(path.ptr, "");
127124

128-
cl_git_pass(p_stat(path.ptr, &st));
129-
cl_assert(entry = git_index_get_bypath(index, "A", 0));
130-
if (entry->mtime.seconds == (int32_t) st.st_mtime) {
131-
found_race = 1;
132-
break;
133-
}
134-
}
125+
cl_git_pass(p_stat(path.ptr, &st));
126+
cl_assert(entry = (git_index_entry *)git_index_get_bypath(index, "A", 0));
135127

136-
if (!found_race)
137-
cl_fail("failed to find race after 10 attempts");
128+
/* force a race */
129+
entry->mtime.seconds = st.st_mtime;
130+
entry->mtime.nanoseconds = st.st_mtime_nsec;
138131

139132
git_buf_free(&path);
140133
}

0 commit comments

Comments
 (0)