Skip to content

Commit e6ed0d2

Browse files
author
Edward Thomson
committed
odb_loose: fsync tests
Introduce a simple counter that `p_fsync` implements. This is useful for ensuring that `p_fsync` is called when we expect it to be, for example when we have enabled an odb backend to perform `fsync`s when writing objects.
1 parent 6d3ad7e commit e6ed0d2

File tree

5 files changed

+50
-1
lines changed

5 files changed

+50
-1
lines changed

src/posix.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
#include <stdio.h>
1111
#include <ctype.h>
1212

13+
size_t p_fsync__cnt = 0;
14+
1315
#ifndef GIT_WIN32
1416

1517
#ifdef NO_ADDRINFO

src/posix.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,12 @@ extern int p_rename(const char *from, const char *to);
111111
extern int git__page_size(size_t *page_size);
112112
extern int git__mmap_alignment(size_t *page_size);
113113

114+
/* The number of times `p_fsync` has been called. Note that this is for
115+
* test code only; it it not necessarily thread-safe and should not be
116+
* relied upon in production.
117+
*/
118+
extern size_t p_fsync__cnt;
119+
114120
/**
115121
* Platform-dependent methods
116122
*/

src/unix/posix.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,14 @@ typedef int GIT_SOCKET;
4040
#define p_link(o,n) link(o, n)
4141
#define p_unlink(p) unlink(p)
4242
#define p_mkdir(p,m) mkdir(p, m)
43-
#define p_fsync(fd) fsync(fd)
4443
extern char *p_realpath(const char *, char *);
4544

45+
GIT_INLINE(int) p_fsync(int fd)
46+
{
47+
p_fsync__cnt++;
48+
return fsync(fd);
49+
}
50+
4651
#define p_recv(s,b,l,f) recv(s,b,l,f)
4752
#define p_send(s,b,l,f) send(s,b,l,f)
4853
#define p_inet_pton(a, b, c) inet_pton(a, b, c)

src/win32/posix_w32.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ int p_fsync(int fd)
113113
{
114114
HANDLE fh = (HANDLE)_get_osfhandle(fd);
115115

116+
p_fsync__cnt++;
117+
116118
if (fh == INVALID_HANDLE_VALUE) {
117119
errno = EBADF;
118120
return -1;

tests/odb/loose.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,13 @@ static void test_read_object(object_data *data)
5656

5757
void test_odb_loose__initialize(void)
5858
{
59+
p_fsync__cnt = 0;
5960
cl_must_pass(p_mkdir("test-objects", GIT_OBJECT_DIR_MODE));
6061
}
6162

6263
void test_odb_loose__cleanup(void)
6364
{
65+
cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_SYNCHRONIZED_OBJECT_CREATION, 0));
6466
cl_fixture_cleanup("test-objects");
6567
}
6668

@@ -150,3 +152,35 @@ void test_odb_loose__permissions_readwrite(void)
150152
{
151153
test_write_object_permission(0777, 0666, 0777, 0666);
152154
}
155+
156+
static void write_object_to_loose_odb(int fsync)
157+
{
158+
git_odb *odb;
159+
git_odb_backend *backend;
160+
git_oid oid;
161+
162+
cl_git_pass(git_odb_new(&odb));
163+
cl_git_pass(git_odb_backend_loose(&backend, "test-objects", -1, fsync, 0777, 0666));
164+
cl_git_pass(git_odb_add_backend(odb, backend, 1));
165+
cl_git_pass(git_odb_write(&oid, odb, "Test data\n", 10, GIT_OBJ_BLOB));
166+
git_odb_free(odb);
167+
}
168+
169+
void test_odb_loose__does_not_fsync_by_default(void)
170+
{
171+
write_object_to_loose_odb(0);
172+
cl_assert_equal_sz(0, p_fsync__cnt);
173+
}
174+
175+
void test_odb_loose__fsync_obeys_odb_option(void)
176+
{
177+
write_object_to_loose_odb(1);
178+
cl_assert(p_fsync__cnt > 0);
179+
}
180+
181+
void test_odb_loose__fsync_obeys_global_setting(void)
182+
{
183+
cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_SYNCHRONIZED_OBJECT_CREATION, 1));
184+
write_object_to_loose_odb(0);
185+
cl_assert(p_fsync__cnt > 0);
186+
}

0 commit comments

Comments
 (0)