Skip to content

Commit 41c45e0

Browse files
committed
Add an append test by writing, closing, then seeking and writing
This adds a test for appending to a file by writing, closing the file, opening it again and seeking to the end, then writing again. I added a clarifying comment before the test describing why this method is used as opposed to just opening the file with "O_APPEND".
1 parent 7c65156 commit 41c45e0

1 file changed

Lines changed: 46 additions & 0 deletions

File tree

src/cc/qfsc/test-qfsc.c

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,49 @@ static char* test_qfs_get_data_locations() {
339339
return 0;
340340
}
341341

342+
// Append to a file by Writing, closing, seeking, and then writing again. This
343+
// emulates a typical unix O_APPEND operation without using the flag.
344+
//
345+
// Using O_APPEND itself does not produce the same result. Opening a file with
346+
// O_APPEND and writing to it will create hole of the appropriate size to make
347+
// the previous chunk reach the chunksize.
348+
static char* test_qfs_append() {
349+
check_qfs_call(qfs_write(qfs, fd, testdata, strlen(testdata)));
350+
check_qfs_call(qfs_sync(qfs, fd));
351+
352+
check_qfs_call(qfs_close(qfs, fd));
353+
354+
// Now write the same data again to the end of the file by seeking
355+
check_qfs_call(fd = qfs_open_file(qfs, "/unit-test/file", O_WRONLY, 0, ""));
356+
check_qfs_call(qfs_seek(qfs, fd, 0, SEEK_END));
357+
check_qfs_call(qfs_write(qfs, fd, testdata, strlen(testdata)));
358+
check_qfs_call(qfs_sync(qfs, fd));
359+
360+
// Check that the file has the correct size
361+
// Reopen the file to enable stat to work
362+
check_qfs_call(qfs_close(qfs, fd));
363+
check_qfs_call(fd = qfs_open_file(qfs, "/unit-test/file", O_RDWR, 0, ""));
364+
365+
struct qfs_attr attr;
366+
check_qfs_call(qfs_stat(qfs, "/unit-test/file", &attr));
367+
368+
int expected_len = strlen(testdata) * 2;
369+
check(attr.size == expected_len,
370+
"file size should be correct: %li != %d",
371+
(long)attr.size, expected_len);
372+
373+
// Generate the string consisting of the testdata twice
374+
char expected_str[expected_len];
375+
memcpy(expected_str, testdata, strlen(testdata));
376+
memcpy(expected_str + strlen(testdata), testdata, strlen(testdata));
377+
378+
char buf[4096];
379+
check_qfs_call(qfs_read(qfs, fd, buf, expected_len));
380+
check(strncmp(expected_str, buf, expected_len) == 0, "The contents of the file are not correct:\n\tExpected: %s\n\tActual: %s", expected_str, buf);
381+
382+
return 0;
383+
}
384+
342385
static char * all_tests() {
343386
run(test_qfs_connect);
344387
run(test_get_metaserver_location);
@@ -367,6 +410,9 @@ static char * all_tests() {
367410
run(test_qfs_open);
368411
run(test_qfs_pread);
369412
run(test_qfs_get_data_locations);
413+
run(test_qfs_close);
414+
run(test_qfs_open_file);
415+
run(test_qfs_append);
370416
run(test_qfs_cleanup);
371417
run(test_qfs_release);
372418
return 0;

0 commit comments

Comments
 (0)