1 #define _POSIX_C_SOURCE 2
19 const size_t TIMESTAMP_LEN
= 19;
21 typedef struct expectation
{
26 const ex_t END
= {((time_t)-1), ((time_t)-1), NULL
};
27 const ex_t CONSUMED
= {((time_t)-2), ((time_t)-2), NULL
};
28 static int is_end(ex_t exp
) {
29 return exp
.a
== END
.a
&& exp
.b
== END
.b
&& exp
.message
== END
.message
;
31 static void consume(ex_t
*exp
) {
35 static int is_consumed(ex_t exp
) {
36 return exp
.a
== CONSUMED
.a
&& exp
.b
== CONSUMED
.b
;
38 static ex_t
expectation(time_t a
, time_t b
, const char *message
) {
42 exp
.message
= message
;
46 static void remove_logfile() {
47 if (remove(FILENAME
) != 0) {
48 if (errno
!= ENOENT
) {
49 die_err("Error removing log file");
54 static ex_t
write_to_tl_append(const char *content
) {
55 FILE *p
= popen("./tl-append", "w");
57 die_err("Couldn't run tl-append");
58 time_t start
= time(NULL
);
59 if (fputs(content
, p
) == EOF
)
60 die("Couldn't write to pipe");
61 int status
= pclose(p
);
62 time_t end
= time(NULL
);
64 die_err("Error closing pipe");
66 die("tl-append exited abnormally");
67 return expectation(start
, end
, content
);
70 static char *timestamp_problem(const ex_t
*ex
, const char *line
) {
73 /* localtime_r to set tm's timezone */
74 time_t now_time
= time(NULL
);
75 if (localtime_r(&now_time
, &tm
) == NULL
)
76 return "Can't unpack current time?";
78 const char *strptime_result
= strptime(line
, "%Y %m %d %H %M %S", &tm
);
79 if (strptime_result
== NULL
|| strptime_result
!= &line
[TIMESTAMP_LEN
])
80 return "Wrong contents in log file: Couldn't parse timestamp";
81 time_t t
= mktime(&tm
);
82 int t_in_range
= ex
->a
<= t
&& t
<= ex
->b
;
84 return "Wrong contents in log file: Wrong time";
88 static char *line_problem(const ex_t
*ex
, const char *line
) {
89 size_t line_len
= strlen(line
);
90 if (line_len
< TIMESTAMP_LEN
+ 1)
91 return "Wrong contents in log file: Line too short";
92 char *trouble
= timestamp_problem(ex
, line
);
95 if (line
[TIMESTAMP_LEN
] != ' ')
96 return "Wrong contents in log file: Bad format";
97 if (strncmp(ex
->message
, &line
[TIMESTAMP_LEN
+ 1], strlen(ex
->message
) + 1) !=
99 return "Wrong contents in log file";
103 static void verify_line(const ex_t
*ex
, const char *line
) {
104 char *trouble
= line_problem(ex
, line
);
109 static void verify_log_contents(const ex_t exps
[]) {
110 FILE *f
= fopen(FILENAME
, "r");
112 die_err("Error opening log file");
113 for (size_t i
= 0; !is_end(exps
[i
]); i
++) {
114 size_t len
= TIMESTAMP_LEN
+ 1 + strlen(exps
[i
].message
);
115 if (len
> INT_MAX
- 1)
116 die("message too long");
117 char *buf
= (char *)malloc(len
+ 2);
118 if (fgets(buf
, len
+ 1, f
) == NULL
)
119 die("Error reading log file");
121 die("Error reading log file");
122 verify_line(&exps
[i
], buf
);
126 die_err("Error closing log file");
129 static void consume_expectation(ex_t exps
[], const char *line
) {
130 for (size_t i
= 0; !is_end(exps
[i
]); i
++) {
131 if (line_problem(&exps
[i
], line
) == NULL
) {
136 die("Wrong contents in log file: Line didn't match any expectation");
139 static void verify_log_contents_unordered(ex_t exps
[]) {
140 FILE *f
= fopen(FILENAME
, "r");
142 die_err("Error opening log file");
144 const int MAX_LEN
= 9999;
146 if (fgets(buf
, MAX_LEN
, f
) == NULL
) {
149 die("Error reading log file");
152 die("Error reading log file");
153 consume_expectation(exps
, buf
);
156 die_err("Error closing log file");
157 for (size_t i
= 0; !is_end(exps
[i
]); i
++) {
158 if (!is_consumed(exps
[i
]))
159 die("Wrong contents in log file: Unconsumed expectation");
163 static void test_encode_time() {
164 /* localtime_r to set tm's timezone */
165 time_t now_time
= time(NULL
);
167 if (localtime_r(&now_time
, &tm
) == NULL
)
168 die_err("Can't unpack current time?");
170 const char *strptime_result
= strptime("2011-12-13 14:15:16", "%F %T", &tm
);
171 if (strptime_result
== NULL
|| *strptime_result
!= '\0')
172 die("Couldn't parse time?");
173 time_t tt
= mktime(&tm
);
174 if (tt
== (time_t)-1)
175 die_err("Can't pack time?");
177 char *encoded
= encode_time(tt
);
178 /* Loose check to allow for daylight savings time changes between the current
179 * time and the target time. :( */
180 assert(encoded
[0] == '2');
181 assert(encoded
[1] == '0');
182 assert(encoded
[2] == '1');
183 assert(encoded
[3] == '1');
184 assert(encoded
[4] == ' ');
185 assert(encoded
[5] == '1');
186 assert(encoded
[6] == '2');
187 assert(encoded
[7] == ' ');
188 assert(encoded
[8] == '1');
189 assert(encoded
[9] == '3');
190 assert(encoded
[10] == ' ');
191 assert(encoded
[11] == '1');
192 assert(isdigit(encoded
[12]));
193 assert(encoded
[13] == ' ');
194 assert(isdigit(encoded
[14]));
195 assert(isdigit(encoded
[15]));
196 assert(encoded
[16] == ' ');
197 assert(encoded
[17] == '1');
198 assert(encoded
[18] == '6');
199 assert(encoded
[19] == '\0');
203 static FILE *take_lock(FILE *f
, char *lock_type
) {
206 die_err("Couldn't get file descriptor for locking");
207 if (strcmp(lock_type
, "fcntl") == 0) {
208 if (fcntl(fd
, F_SETLK
,
209 &(struct flock
){.l_type
= F_WRLCK
,
210 .l_whence
= SEEK_SET
,
213 die_err("Couldn't take fcntl lock");
214 } else if (strcmp(lock_type
, "flock") == 0) {
215 if (flock(fd
, LOCK_EX
) == -1)
216 die_err("Couldn't take flock lock");
218 die("Bad lock type");
223 static void release_lock(FILE *f
) {
225 die_err("Error releasing lock");
228 static void *release_lock_after_delay(void *f
) {
230 release_lock((FILE *)f
);
234 static void *writer_thread(void *start_signal
) {
235 ex_t
*ex
= (ex_t
*)malloc(sizeof(ex_t
));
237 die_err("Couldn't allocate memory");
239 if (asprintf(&message
, "Hello from thread %lu\n", pthread_self()) == -1)
240 die("Couldn't prepare message");
241 pthread_rwlock_rdlock((pthread_rwlock_t
*)start_signal
);
242 *ex
= write_to_tl_append(message
);
246 static void write_and_read_line() {
248 ex_t e
= write_to_tl_append("foo\n");
249 verify_log_contents((ex_t
[]){e
, END
});
252 static void write_and_read_two_lines() {
254 ex_t e1
= write_to_tl_append("foo\n");
255 ex_t e2
= write_to_tl_append("bar\n");
256 verify_log_contents((ex_t
[]){e1
, e2
, END
});
259 static void write_to_locked_log(char *lock_types
[]) {
261 ex_t e1
= write_to_tl_append("begin\n");
262 FILE *f
= fopen(FILENAME
, "ae");
264 die_err("Couldn't open file for locking");
265 for (int i
= 0; lock_types
[i
]; i
++)
266 take_lock(f
, lock_types
[0]);
267 pthread_t unlock_thread
;
269 pthread_create(&unlock_thread
, NULL
, &release_lock_after_delay
, f
);
270 if (create_ret
!= 0) {
272 die_err("Couldn't start thread");
274 ex_t e2
= write_to_tl_append("delayed\n");
275 int join_ret
= pthread_join(unlock_thread
, NULL
);
278 die_err("Couldn't join thread");
280 verify_log_contents((ex_t
[]){e1
, e2
, END
});
283 static void write_concurrently() {
285 const int PARALLELISM
= 250;
286 pthread_t threads
[PARALLELISM
];
287 pthread_rwlock_t start_signal
;
288 pthread_rwlock_init(&start_signal
, NULL
);
289 for (int i
= 0; i
< PARALLELISM
; i
++) {
291 pthread_create(&threads
[i
], NULL
, &writer_thread
, &start_signal
);
292 if (create_ret
!= 0) {
294 die_err("Couldn't start thread");
297 pthread_rwlock_unlock(&start_signal
);
298 ex_t results
[PARALLELISM
+ 1];
299 for (int i
= 0; i
< PARALLELISM
; i
++) {
301 int join_ret
= pthread_join(threads
[i
], (void **)&ex
);
304 die_err("Couldn't join thread");
309 results
[PARALLELISM
] = END
;
310 verify_log_contents_unordered(results
);
311 for (int i
= 0; i
< PARALLELISM
; i
++) {
312 free((void *)results
[i
].message
);
318 write_and_read_line();
319 write_and_read_two_lines();
320 write_to_locked_log((char *[]){NULL
});
321 write_to_locked_log((char *[]){"fcntl", NULL
});
322 write_to_locked_log((char *[]){"flock", NULL
});
323 write_to_locked_log((char *[]){"flock", "fcntl", NULL
}); /* Deadlock risk! */
324 write_to_locked_log((char *[]){"fcntl", "flock", NULL
});
325 write_concurrently();