+}
+
+static void consume_expectation(ex_t exps[], const char *line) {
+ for (size_t i = 0; !is_end(exps[i]); i++) {
+ if (line_problem(&exps[i], line) == NULL) {
+ exps[i] = CONSUMED;
+ return;
+ }
+ }
+ die("Wrong contents in log file: Line didn't match any expectation");
+}
+
+static void verify_log_contents_unordered(ex_t exps[]) {
+ FILE *f = fopen(FILENAME, "r");
+ if (f == NULL)
+ die_err("Error opening log file");
+ for (;;) {
+ const int MAX_LEN = 9999;
+ char buf[MAX_LEN];
+ if (fgets(buf, MAX_LEN, f) == NULL) {
+ if (feof(f))
+ break;
+ die("Error reading log file");
+ }
+ if (ferror(f))
+ die("Error reading log file");
+ consume_expectation(exps, buf);
+ }
+ if (fclose(f) != 0)
+ die_err("Error closing log file");
+ for (size_t i = 0; !is_end(exps[i]); i++) {
+ if (!is_consumed(exps[i]))
+ die("Wrong contents in log file: Unconsumed expectation");
+ }
+}
+
+static void test_encode_time() {
+ /* localtime_r to set tm's timezone */
+ time_t now_time = time(NULL);
+ struct tm tm;
+ if (localtime_r(&now_time, &tm) == NULL)
+ die_err("Can't unpack current time?");
+
+ const char *strptime_result = strptime("2011-12-13 14:15:16", "%F %T", &tm);
+ if (strptime_result == NULL || *strptime_result != '\0')
+ die("Couldn't parse time?");
+ time_t tt = mktime(&tm);
+ if (tt == (time_t)-1)
+ die_err("Can't pack time?");
+
+ const char *encoded = encode_time(tt);
+ /* Loose check to allow for daylight savings time changes between the current
+ * time and the target time. :( */
+ assert(encoded[0] == '2');
+ assert(encoded[1] == '0');
+ assert(encoded[2] == '1');
+ assert(encoded[3] == '1');
+ assert(encoded[4] == ' ');
+ assert(encoded[5] == '1');
+ assert(encoded[6] == '2');
+ assert(encoded[7] == ' ');
+ assert(encoded[8] == '1');
+ assert(encoded[9] == '3');
+ assert(encoded[10] == ' ');
+ assert(encoded[11] == '1');
+ assert(isdigit(encoded[12]));
+ assert(encoded[13] == ' ');
+ assert(isdigit(encoded[14]));
+ assert(isdigit(encoded[15]));
+ assert(encoded[16] == ' ');
+ assert(encoded[17] == '1');
+ assert(encoded[18] == '6');
+ assert(encoded[19] == '\0');
+}
+
+static FILE *take_lock(FILE *f, char *lock_type) {
+ int fd = fileno(f);
+ if (fd == -1)
+ die_err("Couldn't get file descriptor for locking");
+ if (strcmp(lock_type, "fcntl") == 0) {
+ if (fcntl(fd, F_SETLK,
+ &(struct flock){.l_type = F_WRLCK,
+ .l_whence = SEEK_SET,
+ .l_start = 0,
+ .l_len = 0}) == -1)
+ die_err("Couldn't take fcntl lock");
+ } else if (strcmp(lock_type, "flock") == 0) {
+ if (flock(fd, LOCK_EX) == -1)
+ die_err("Couldn't take flock lock");
+ } else {
+ die("Bad lock type");
+ }
+ return f;
+}
+
+static void release_lock(FILE *f) {
+ if (fclose(f) != 0)
+ die_err("Error releasing lock");
+}
+
+static void *release_lock_after_delay(void *f) {
+ sleep(1);
+ release_lock((FILE *)f);
+ return NULL;
+}
+
+static void *writer_thread(void *start_signal) {
+ ex_t *ex = (ex_t *)malloc(sizeof(ex_t));
+ if (ex == NULL)
+ die_err("Couldn't allocate memory");
+ char *message;
+ if (asprintf(&message, "Hello from thread %lu\n", pthread_self()) == -1)
+ die("Couldn't prepare message");
+ pthread_rwlock_rdlock((pthread_rwlock_t *)start_signal);
+ *ex = write_to_tl_append(message);
+ return ex;