Skip to content

Commit

Permalink
Fix the append test by writing, closing, then seeking and writing
Browse files Browse the repository at this point in the history
This fixes the append test to correctly run by writing, closing the
file, opening it again and seeking, then writing again.  This will
result in a typical append.
  • Loading branch information
noahgoldman committed Nov 16, 2016
1 parent 066c1bb commit c35348d
Showing 1 changed file with 26 additions and 23 deletions.
49 changes: 26 additions & 23 deletions src/cc/qfsc/test-qfsc.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ static char mbuf[4096];

static int tests_run;
static char* metaserver_host = "localhost";
static int metaserver_port = 10000;
static int metaserver_port = 20000;

// Global file system and file descriptor under test.
struct QFS* qfs;
Expand Down Expand Up @@ -191,11 +191,6 @@ static char* test_qfs_stat() {
return 0;
}

static char* test_qfs_stat_multiple() {
struct qfs_attr attr;

}

static char* test_qfs_create() {
check_qfs_call(fd = qfs_create(qfs, "/unit-test/file"));

Expand Down Expand Up @@ -344,16 +339,23 @@ static char* test_qfs_get_data_locations() {
return 0;
}

// Append to a file by Writing, closing, seeking, and then writing again. This
// emulates a typical unix O_APPEND operation without using the flag.
//
// Using O_APPEND itself does not produce the same result. Opening a file with
// O_APPEND and writing to it will create hole of the appropriate size to make
// the previous chunk reach the chunksize. This can be useful as an atomic
// append, but is not the behavior you'd expect from a normal filesystem.
static char* test_qfs_append() {
char single_byte = 'a';
check_qfs_call(qfs_write(qfs, fd, &single_byte, 1));
check_qfs_call(qfs_write(qfs, fd, testdata, strlen(testdata)));
check_qfs_call(qfs_sync(qfs, fd));

check_qfs_call(qfs_close(qfs, fd));

// Now write the same data again (appending this time)
check_qfs_call(fd = qfs_open_file(qfs, "/unit-test/file", O_WRONLY|O_APPEND, 0, ""));
check_qfs_call(qfs_write(qfs, fd, &single_byte, 1));
// Now write the same data again to the end of the file by seeking
check_qfs_call(fd = qfs_open_file(qfs, "/unit-test/file", O_WRONLY, 0, ""));
check_qfs_call(qfs_seek(qfs, fd, 0, SEEK_END));
check_qfs_call(qfs_write(qfs, fd, testdata, strlen(testdata)));
check_qfs_call(qfs_sync(qfs, fd));

// Check that the file has the correct size
Expand All @@ -364,18 +366,19 @@ static char* test_qfs_append() {
struct qfs_attr attr;
check_qfs_call(qfs_stat(qfs, "/unit-test/file", &attr));

// This check for file size fails.
/* check(attr.size == 2, */
/* "file size should be correct: %li != %d", */
/* (long)attr.size, 2); */
ssize_t chunk_size = qfs_get_chunksize(qfs, "/unit-test/file");
printf("Block size is: %li\n", (long)chunk_size);
printf("Size of appended file is: %li\n", (long)attr.size);
int expected_len = strlen(testdata) * 2;
check(attr.size == expected_len,
"file size should be correct: %li != %d",
(long)attr.size, expected_len);

// Try and read the file
char buf[2];
check_qfs_call(qfs_read(qfs, fd, buf, 2));
check(strcmp("aa", buf) == 0, "The contents of the file are not correct");
// Generate the string consisting of the testdata twice
char expected_str[expected_len];
strcpy(expected_str, testdata);
strcat(expected_str, testdata);

char buf[expected_len];
check_qfs_call(qfs_read(qfs, fd, buf, expected_len));
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);

return 0;
}
Expand Down Expand Up @@ -407,10 +410,10 @@ static char * all_tests() {
run(test_qfs_close);
run(test_qfs_open);
run(test_qfs_pread);
run(test_qfs_get_data_locations);
run(test_qfs_close);
run(test_qfs_open_file);
run(test_qfs_append);
run(test_qfs_get_data_locations);
run(test_qfs_cleanup);
run(test_qfs_release);
return 0;
Expand Down

0 comments on commit c35348d

Please sign in to comment.