usleep tests: Avoid failure due to known Cygwin 3.5.3 bug.
[gnulib.git] / tests / test-nonblocking-reader.h
blobe04178ffd489a23ed99c95f574cc96f3f5c0a9ea
1 /* The reader part of a test program for non-blocking communication.
3 Copyright (C) 2011-2024 Free Software Foundation, Inc.
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
18 /* This program implements 4 tests:
20 test == 0:
21 Test blocking write() with blocking read().
23 Timeline Main process Child process
24 0 s Start Start, read(10000)
25 1 s write(20000) Return from read(10000)
26 2 s Next read(10000)
27 2 s Return from write(20000) Return from read(10000)
29 test == 1:
30 Test non-blocking write() with blocking read().
32 Timeline Main process Child process
33 0 s Start Start, read(10000)
34 1 s write(20000) Return from read(10000)
35 Return with at least 10000,
36 Repeatedly continue
37 write() of the rest
38 2 s Next read(10000)
39 2 s Return from write(10000) Return from read(10000)
41 test == 2:
42 Test blocking write() with non-blocking read().
44 Timeline Main process Child process
45 0 s Start Start, read(10000)
46 repeatedly polling
47 1 s write(20000) Return from read(10000)
48 2 s Next read(10000)
49 2 s Return from write(20000) Return from read(10000)
51 test == 3:
52 Test non-blocking write() with non-blocking read().
55 #include "test-nonblocking-misc.h"
57 static ssize_t
58 full_read (size_t fd, void *buf, size_t count)
60 size_t bytes_read;
62 bytes_read = 0;
63 while (bytes_read < count)
65 TIMING_DECLS
66 ssize_t ret;
67 int saved_errno;
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 (void) spent_time;
78 if (ret < 0)
79 return -1;
80 else
82 ASSERT (ret > 0);
83 bytes_read += ret;
86 return bytes_read;
89 static ssize_t
90 full_read_from_nonblocking_fd (size_t fd, void *buf, size_t count)
92 size_t bytes_read;
94 bytes_read = 0;
95 while (bytes_read < count)
97 TIMING_DECLS
98 ssize_t ret;
99 int saved_errno;
101 dbgfprintf (stderr, "%s: >> read (%lu)\n", PROG_ROLE,
102 (unsigned long) (count - bytes_read));
103 START_TIMING
104 ret = read (fd, (char *) buf + bytes_read, count - bytes_read);
105 saved_errno = errno;
106 END_TIMING
107 dbgfprintf (stderr, "%s: << read -> %ld%s\n", PROG_ROLE,
108 (long) ret, dbgstrerror (ret < 0, saved_errno));
109 /* This assertion fails if the non-blocking flag is effectively not set
110 on fd. */
111 ASSERT (spent_time < 0.5);
112 if (ret < 0)
114 ASSERT (saved_errno == EAGAIN || saved_errno == EWOULDBLOCK);
115 usleep (SMALL_DELAY);
117 else
119 ASSERT (ret > 0);
120 bytes_read += ret;
123 return bytes_read;
126 /* Execute the reader loop. */
127 static void
128 main_reader_loop (int test, size_t data_block_size, int fd)
130 unsigned char *expected;
131 unsigned char *data;
133 /* Set up the expected data. */
134 expected = init_data (data_block_size);
136 data = (unsigned char *) malloc (2 * data_block_size);
137 ASSERT (data != NULL);
139 switch (test)
141 TIMING_DECLS
142 ssize_t ret;
144 case 0: /* Test blocking write() with blocking read(). */
145 case 1: /* Test non-blocking write() with blocking read(). */
146 START_TIMING
147 ret = full_read (fd, data, data_block_size);
148 END_TIMING
149 ASSERT (ret == data_block_size);
150 ASSERT (memcmp (data, expected, data_block_size) == 0);
151 ASSERT (spent_time > 0.5);
152 /* This assertion fails if data_block_size is very large and
153 ENABLE_DEBUGGING is 1. */
154 ASSERT (spent_time < 1.5);
156 usleep (1000000);
158 START_TIMING
159 ret = full_read (fd, data, data_block_size);
160 END_TIMING
161 ASSERT (ret == data_block_size);
162 ASSERT (memcmp (data, expected + data_block_size, data_block_size) == 0);
163 /* This assertion fails if data_block_size is much larger than needed
164 and SMALL_DELAY is too large. */
165 ASSERT (spent_time < 0.5);
167 break;
169 case 2: /* Test blocking write() with non-blocking read(). */
170 case 3: /* Test non-blocking write() with non-blocking read(). */
171 START_TIMING
172 ret = full_read_from_nonblocking_fd (fd, data, data_block_size);
173 END_TIMING
174 ASSERT (ret == data_block_size);
175 ASSERT (memcmp (data, expected, data_block_size) == 0);
176 ASSERT (spent_time > 0.5);
177 /* This assertion fails if data_block_size is much larger than needed
178 and SMALL_DELAY is too large, or if data_block_size is very large and
179 ENABLE_DEBUGGING is 1. */
180 ASSERT (spent_time < 1.5);
182 usleep (1000000);
184 START_TIMING
185 ret = full_read_from_nonblocking_fd (fd, data, data_block_size);
186 END_TIMING
187 ASSERT (ret == data_block_size);
188 ASSERT (memcmp (data, expected + data_block_size, data_block_size) == 0);
189 /* This assertion fails if data_block_size is much larger than needed
190 and SMALL_DELAY is too large. */
191 ASSERT (spent_time < 0.5);
193 break;
195 default:
196 abort ();
199 free (data);
200 free (expected);