Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check log multithread safeness #93

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions test/log/check_log.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <check.h>

#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>

Expand Down Expand Up @@ -238,6 +239,76 @@ START_TEST(test_write_skip_metrics)
}
END_TEST

#define LOGOBJECTSIZE 100
#define LOGOBJECTCOUNT 50
struct produce_consume_st {
struct logger* logger;
bool finished_producing;
char *produced;
size_t produced_len;
};

static void*
_produce_log(void *_task)
{
struct timespec wait = {0, 1};
struct produce_consume_st *task = (struct produce_consume_st *)_task;
size_t i;
char logstr[LOGOBJECTSIZE];

for (i = 1; i <= LOGOBJECTCOUNT; i++) {
memset(logstr, i, LOGOBJECTSIZE);
if (log_write(task->logger, logstr, LOGOBJECTSIZE)) {
memcpy(&task->produced[task->produced_len], logstr, LOGOBJECTSIZE);
task->produced_len += LOGOBJECTSIZE;
}
if (i % 5 == 0) {
// yielding to another thread
nanosleep(&wait, NULL);
}
}
task->finished_producing = true;
return NULL;
}

static void
_consume_log(struct produce_consume_st *task)
{
while (!task->finished_producing) {
log_flush(task->logger);
log_reopen(task->logger);
}
}

START_TEST(test_thread_produce_thread_flush)
{
struct produce_consume_st task;
char *tmpname = tmpname_create();

pthread_t producer_thread;

test_reset();

task.logger = log_create(tmpname, LOGOBJECTSIZE);
task.produced = malloc(LOGOBJECTSIZE * LOGOBJECTCOUNT);
task.produced_len = 0;
task.finished_producing = false;

pthread_create(&producer_thread, NULL, _produce_log, &task);
_consume_log(&task);
pthread_join(producer_thread, NULL);
log_destroy(&task.logger);

ck_assert_int_ge(task.produced_len, 2 * LOGOBJECTSIZE);
assert_file_contents(tmpname, task.produced, task.produced_len);

tmpname_destroy(tmpname);
free(task.produced);
}
END_TEST
#undef LOGOBJECTSIZE
#undef LOGOBJECTCOUNT

/*
* test suite
*/
Expand All @@ -259,6 +330,7 @@ log_suite(void)
tcase_add_test(tc_log, test_write_metrics_file_nobuf);
tcase_add_test(tc_log, test_write_metrics_stderr_nobuf);
tcase_add_test(tc_log, test_write_skip_metrics);
tcase_add_test(tc_log, test_thread_produce_thread_flush);

return s;
}
Expand Down