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

Skip to content

Commit 26432a9

Browse files
committed
tests: set racy times manually
1 parent 6c5eaea commit 26432a9

File tree

1 file changed

+26
-6
lines changed

1 file changed

+26
-6
lines changed

tests/diff/racy.c

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include "../checkout/checkout_helpers.h"
33

44
#include "buffer.h"
5+
#include "index.h"
56

67
static git_repository *g_repo;
78

@@ -52,29 +53,48 @@ void test_diff_racy__write_index_just_after_file(void)
5253
git_index *index;
5354
git_diff *diff;
5455
git_buf path = GIT_BUF_INIT;
56+
struct timeval times[2];
5557

5658
/* Make sure we do have a timestamp */
5759
cl_git_pass(git_repository_index(&index, g_repo));
5860
cl_git_pass(git_index_write(index));
59-
/* The timestamp will be one second before we change the file */
60-
sleep(1);
6161

6262
cl_git_pass(git_buf_joinpath(&path, git_repository_workdir(g_repo), "A"));
6363
cl_git_mkfile(path.ptr, "A");
64+
/* Force the file's timestamp to be a second after we wrote the index */
65+
times[0].tv_sec = index->stamp.mtime + 1;
66+
times[0].tv_usec = 0;
67+
times[1].tv_sec = index->stamp.mtime + 1;
68+
times[1].tv_usec = 0;
69+
cl_git_pass(p_utimes(path.ptr, times));
6470

6571
/*
6672
* Put 'A' into the index, the size field will be filled,
6773
* because the index' on-disk timestamp does not match the
68-
* file's timestamp. Both timestamps will however match after
69-
* writing out the index.
74+
* file's timestamp.
7075
*/
71-
cl_git_pass(git_repository_index(&index, g_repo));
7276
cl_git_pass(git_index_add_bypath(index, "A"));
7377
cl_git_pass(git_index_write(index));
7478

75-
/* Change its contents quickly, so we get the same timestamp */
7679
cl_git_mkfile(path.ptr, "B");
80+
/*
81+
* Pretend this index' modification happend a second after the
82+
* file update, and rewrite the file in that same second.
83+
*/
84+
times[0].tv_sec = index->stamp.mtime + 2;
85+
times[0].tv_usec = 0;
86+
times[1].tv_sec = index->stamp.mtime + 2;
87+
times[0].tv_usec = 0;
88+
89+
cl_git_pass(p_utimes(git_index_path(index), times));
90+
cl_git_pass(p_utimes(path.ptr, times));
91+
92+
cl_git_pass(git_index_read(index, true));
7793

7894
cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, index, NULL));
7995
cl_assert_equal_i(1, git_diff_num_deltas(diff));
96+
97+
git_buf_free(&path);
98+
git_diff_free(diff);
99+
git_index_free(index);
80100
}

0 commit comments

Comments
 (0)