This source file includes following definitions.
- full_read
- full_read_from_nonblocking_fd
- main_reader_loop
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55 #include "test-nonblocking-misc.h"
56
57 static ssize_t
58 full_read (size_t fd, void *buf, size_t count)
59 {
60 size_t bytes_read;
61
62 bytes_read = 0;
63 while (bytes_read < count)
64 {
65 TIMING_DECLS
66 ssize_t ret;
67 int saved_errno;
68
69 dbgfprintf (stderr, "%s: >> read (%lu)\n", PROG_ROLE,
70 (unsigned long) (count - bytes_read));
71 START_TIMING
72 ret = read (fd, (char *) buf + bytes_read, count - bytes_read);
73 saved_errno = errno;
74 END_TIMING
75 dbgfprintf (stderr, "%s: << read -> %ld%s\n", PROG_ROLE,
76 (long) ret, dbgstrerror (ret < 0, saved_errno));
77 if (ret < 0)
78 return -1;
79 else
80 {
81 ASSERT (ret > 0);
82 bytes_read += ret;
83 }
84 }
85 return bytes_read;
86 }
87
88 static ssize_t
89 full_read_from_nonblocking_fd (size_t fd, void *buf, size_t count)
90 {
91 size_t bytes_read;
92
93 bytes_read = 0;
94 while (bytes_read < count)
95 {
96 TIMING_DECLS
97 ssize_t ret;
98 int saved_errno;
99
100 dbgfprintf (stderr, "%s: >> read (%lu)\n", PROG_ROLE,
101 (unsigned long) (count - bytes_read));
102 START_TIMING
103 ret = read (fd, (char *) buf + bytes_read, count - bytes_read);
104 saved_errno = errno;
105 END_TIMING
106 dbgfprintf (stderr, "%s: << read -> %ld%s\n", PROG_ROLE,
107 (long) ret, dbgstrerror (ret < 0, saved_errno));
108
109
110 ASSERT (spent_time < 0.5);
111 if (ret < 0)
112 {
113 ASSERT (saved_errno == EAGAIN || saved_errno == EWOULDBLOCK);
114 usleep (SMALL_DELAY);
115 }
116 else
117 {
118 ASSERT (ret > 0);
119 bytes_read += ret;
120 }
121 }
122 return bytes_read;
123 }
124
125
126 static void
127 main_reader_loop (int test, size_t data_block_size, int fd)
128 {
129 unsigned char *expected;
130 unsigned char *data;
131
132
133 expected = init_data (data_block_size);
134
135 data = (unsigned char *) malloc (2 * data_block_size);
136 ASSERT (data != NULL);
137
138 switch (test)
139 {
140 TIMING_DECLS
141 ssize_t ret;
142
143 case 0:
144 case 1:
145 START_TIMING
146 ret = full_read (fd, data, data_block_size);
147 END_TIMING
148 ASSERT (ret == data_block_size);
149 ASSERT (memcmp (data, expected, data_block_size) == 0);
150 ASSERT (spent_time > 0.5);
151
152
153 ASSERT (spent_time < 1.5);
154
155 usleep (1000000);
156
157 START_TIMING
158 ret = full_read (fd, data, data_block_size);
159 END_TIMING
160 ASSERT (ret == data_block_size);
161 ASSERT (memcmp (data, expected + data_block_size, data_block_size) == 0);
162
163
164 ASSERT (spent_time < 0.5);
165
166 break;
167
168 case 2:
169 case 3:
170 START_TIMING
171 ret = full_read_from_nonblocking_fd (fd, data, data_block_size);
172 END_TIMING
173 ASSERT (ret == data_block_size);
174 ASSERT (memcmp (data, expected, data_block_size) == 0);
175 ASSERT (spent_time > 0.5);
176
177
178
179 ASSERT (spent_time < 1.5);
180
181 usleep (1000000);
182
183 START_TIMING
184 ret = full_read_from_nonblocking_fd (fd, data, data_block_size);
185 END_TIMING
186 ASSERT (ret == data_block_size);
187 ASSERT (memcmp (data, expected + data_block_size, data_block_size) == 0);
188
189
190 ASSERT (spent_time < 0.5);
191
192 break;
193
194 default:
195 abort ();
196 }
197
198 free (data);
199 free (expected);
200 }