tdb2: limit coalescing based on how successful we are.
[ccan] / ccan / read_write_all / test / run-write_all.c
1 #include <ccan/read_write_all/read_write_all.h>
2 #include <ccan/tap/tap.h>
3 #include <unistd.h>
4 #include <sys/types.h>
5 #include <signal.h>
6 #include <limits.h>
7 #include <sys/wait.h>
8 #include <err.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <stdio.h>
12
13 static ssize_t test_write(int fd, const void *buf, size_t count);
14 #define write test_write
15 #include <ccan/read_write_all/read_write_all.c>
16 #undef write
17
18 static ssize_t write_return;
19
20 static ssize_t test_write(int fd, const void *buf, size_t count)
21 {
22         if (write_return == 0) {
23                 errno = ENOSPC;
24                 return 0;
25         }
26
27         if (write_return < 0) {
28                 errno = -write_return;
29                 /* Don't return EINTR more than once! */
30                 if (errno == EINTR)
31                         write_return = count;
32                 return -1;
33         }
34
35         if (write_return < count)
36                 return write_return;
37         return count;
38 }
39
40 #define BUFSZ 1024
41
42 int main(int argc, char *argv[])
43 {
44         char *buffer;
45
46         buffer = malloc(BUFSZ);
47         plan_tests(8);
48
49         write_return = -ENOSPC;
50         ok1(!write_all(100, buffer, BUFSZ));
51         ok1(errno == ENOSPC);
52
53         write_return = -EINTR;
54         ok1(write_all(100, buffer, BUFSZ));
55         ok1(errno == EINTR);
56
57         write_return = 1;
58         errno = 0;
59         ok1(write_all(100, buffer, BUFSZ));
60         ok1(errno == 0);
61
62         write_return = BUFSZ;
63         ok1(write_all(100, buffer, BUFSZ));
64         ok1(errno == 0);
65
66         return exit_status();
67 }