File: BufferedIOTest.cpp

package info (click to toggle)
ruby-passenger 3.0.13debian-1%2Bdeb7u2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 15,920 kB
  • sloc: cpp: 99,104; ruby: 18,098; ansic: 9,846; sh: 8,632; python: 141; makefile: 30
file content (364 lines) | stat: -rw-r--r-- 11,314 bytes parent folder | download
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
#include "TestSupport.h"
#include "Utils/BufferedIO.h"
#include "Utils/Timer.h"
#include "Utils/IOUtils.h"
#include <algorithm>

using namespace Passenger;
using namespace std;

namespace tut {
	struct BufferedIOTest {
		FileDescriptor reader, writer;
		BufferedIO io;
		
		string readData;
		unsigned int counter;
		char buf[100];
		BufferedIO::AcceptFunction a_eof;
		BufferedIO::AcceptFunction a_twoBytesRead;
		
		BufferedIOTest() {
			Pipe p = createPipe();
			reader = p.first;
			writer = p.second;
			io = BufferedIO(reader);
			counter = 0;
			memset(buf, 0, sizeof(buf));
			a_eof = boost::bind(&BufferedIOTest::eof, this, _1, _2);
			a_twoBytesRead = boost::bind(&BufferedIOTest::twoBytesRead, this, _1, _2);
		}
		
		void write(const StaticString &data) {
			::write(writer, data.c_str(), data.size());
		}
		
		pair<unsigned int, bool> twoBytesRead(const char *data, unsigned int size) {
			if (counter == 2) {
				return make_pair(0, true);
			} else {
				unsigned int toRead = min(2u - counter, size);
				readData.append(data, toRead);
				counter += toRead;
				return make_pair(toRead, counter == 2);
			}
		}
		
		pair<unsigned int, bool> eof(const char *data, unsigned int size) {
			readData.append(data, size);
			return make_pair(size, false);
		}
		
		static void writeAfterSomeTime(int fd, int sleepTime, string data) {
			syscalls::usleep(sleepTime);
			writeExact(fd, data);
		}
		
		static void closeAfterSomeTime(FileDescriptor fd, int sleepTime) {
			syscalls::usleep(sleepTime);
			fd.close();
		}
	};
	
	DEFINE_TEST_GROUP(BufferedIOTest);

	/***** Test readUntil() *****/
	
	TEST_METHOD(1) {
		// If the connection is already closed and the buffer is empty, then it returns 0.
		writer.close();
		ensure_equals(io.readUntil(a_eof), 0u);
		ensure_equals(readData, "");
		ensure_equals(io.getBuffer(), "");
	}
	
	TEST_METHOD(2) {
		// If the connection is already closed and the buffer is non-empty,
		// then it reads from the buffer.
		writer.close();
		io.unread("hello world");
		ensure_equals(io.readUntil(a_twoBytesRead), 2u);
		ensure_equals(readData, "he");
		ensure_equals(io.readUntil(a_eof), 9u);
		ensure_equals(readData, "hello world");
		ensure_equals(io.getBuffer(), "");
	}
	
	TEST_METHOD(3) {
		// If the buffer is empty then it reads from the connection.
		write("hello world");
		writer.close();
		ensure_equals("(1)", io.readUntil(a_twoBytesRead), 2u);
		ensure_equals("(2)", readData, "he");
		ensure_equals("(5)", io.readUntil(a_eof), 9u);
		ensure_equals("(6)", readData, "hello world");
		ensure_equals("(7)", io.readUntil(a_eof), 0u);
		ensure_equals("(8)", readData, "hello world");
		ensure_equals(io.getBuffer(), "");
	}
	
	TEST_METHOD(4) {
		// If the buffer is non-empty then it reads from the
		// buffer first, then from the connection.
		io.unread("hel");
		write("lo world");
		writer.close();
		
		ensure_equals("(1)", io.readUntil(a_twoBytesRead), 2u);
		ensure_equals("(2)", readData, "he");
		counter = 0;
		ensure_equals("(3)", io.readUntil(a_twoBytesRead), 2u);
		ensure_equals("(4)", readData, "hell");
		ensure_equals("(5)", io.readUntil(a_eof), 7u);
		ensure_equals("(6)", readData, "hello world");
		ensure_equals("(7)", io.readUntil(a_eof), 0u);
		ensure_equals("(8)", readData, "hello world");
		ensure_equals(io.getBuffer(), "");
	}
	
	TEST_METHOD(5) {
		// It blocks until the acceptor function says it's done or until EOF.
		TempThread thr1(boost::bind(writeAfterSomeTime, writer, 20000, "aa"));
		Timer timer1;
		ensure_equals(io.readUntil(a_twoBytesRead), 2u);
		ensure_equals(readData, "aa");
		ensure("At least 18 msec elapsed", timer1.elapsed() >= 18);
		ensure("At most 30 msec elapsed", timer1.elapsed() <= 30);
		
		TempThread thr2(boost::bind(closeAfterSomeTime, writer, 20000));
		Timer timer2;
		ensure_equals(io.readUntil(a_twoBytesRead), 0u);
		ensure_equals(readData, "aa");
		ensure("At least 18 msec elapsed", timer2.elapsed() >= 18);
		ensure("At most 30 msec elapsed", timer2.elapsed() <= 30);
	}
	
	TEST_METHOD(6) {
		// It throws TimeoutException if it cannot read enough data
		// within the specified timeout.
		unsigned long long timeout = 50000;
		io.unread("he");
		write("llo");
		Timer timer;
		try {
			io.readUntil(a_eof, &timeout);
			fail("TimeoutException expected");
		} catch (const TimeoutException &) {
			ensure("At least 45 msec elapsed", timer.elapsed() >= 45);
			ensure("At most 65 msec elapsed", timer.elapsed() < 65);
			ensure("It deducts the waited time from the timeout", timeout < 5000);
			ensure_equals(readData, "hello");
			ensure_equals(io.getBuffer(), "");
		}
	}
	
	/***** Test read() *****/
	
	TEST_METHOD(10) {
		// If the connection is already closed and the buffer is empty, then it returns 0.
		writer.close();
		ensure_equals(io.read(buf, sizeof(buf)), 0u);
		ensure_equals(io.getBuffer(), "");
	}
	
	TEST_METHOD(11) {
		// If the connection is already closed and the buffer is non-empty
		// and >= N bytes, then it reads everything from the buffer.
		io.unread("hel");
		write("lo world");
		writer.close();
		ensure_equals(io.read(buf, 5), 5u);
		ensure_equals(StaticString(buf), "hello");
		ensure_equals(io.getBuffer(), " world");
	}
	
	TEST_METHOD(12) {
		// If the connection is already closed and the buffer is non-empty
		// and < N bytes, then it reads N bytes from the buffer and the rest
		// from the connection.
		io.unread("hel");
		write("lo world");
		writer.close();
		ensure_equals(io.read(buf, sizeof(buf)), 11u);
		ensure_equals(StaticString(buf), "hello world");
		ensure_equals(io.getBuffer(), "");
	}
	
	TEST_METHOD(13) {
		// If the buffer is empty then it reads from the connection.
		write("hello world");
		ensure_equals(io.read(buf, 5), 5u);
		ensure_equals(StaticString(buf), "hello");
		ensure_equals(io.getBuffer(), " world");
	}
	
	TEST_METHOD(14) {
		// If the buffer is non-empty then it reads from the
		// buffer first, then from the connection.
		write("hello world");
		
		ensure_equals(io.read(buf, 2), 2u);
		ensure_equals(StaticString(buf), "he");
		ensure_equals(io.getBuffer(), "llo world");
		
		ensure_equals(io.read(buf, 7), 7u);
		ensure_equals(StaticString(buf), "llo wor");
		ensure_equals(io.getBuffer(), "ld");
	}
	
	TEST_METHOD(15) {
		// It blocks until the given number of bytes are read or until EOF.
		TempThread thr1(boost::bind(writeAfterSomeTime, writer, 20000, "aa"));
		Timer timer1;
		ensure_equals(io.read(buf, 2), 2u);
		ensure_equals(StaticString(buf), "aa");
		ensure("At least 18 msec elapsed", timer1.elapsed() >= 18);
		ensure("At most 30 msec elapsed", timer1.elapsed() <= 30);
		
		TempThread thr2(boost::bind(closeAfterSomeTime, writer, 20000));
		Timer timer2;
		ensure_equals(io.read(buf, sizeof(buf)), 0u);
		ensure_equals(StaticString(buf), "aa");
		ensure("At least 18 msec elapsed", timer2.elapsed() >= 18);
		ensure("At most 30 msec elapsed", timer2.elapsed() <= 30);
	}
	
	TEST_METHOD(16) {
		// It throws TimeoutException if it cannot read enough data
		// within the specified timeout.
		unsigned long long timeout = 50000;
		io.unread("he");
		write("llo");
		Timer timer;
		try {
			io.read(buf, sizeof(buf), &timeout);
			fail("TimeoutException expected");
		} catch (const TimeoutException &) {
			ensure("At least 45 msec elapsed", timer.elapsed() >= 45);
			ensure("At most 65 msec elapsed", timer.elapsed() < 65);
			ensure("It deducts the waited time from the timeout", timeout < 5000);
			ensure_equals(io.getBuffer(), "");
		}
	}
	
	/***** Test readAll() *****/
	
	TEST_METHOD(20) {
		// It reads everything until EOF.
		TempThread thr1(boost::bind(writeAfterSomeTime, writer, 20000, "aa"));
		TempThread thr2(boost::bind(closeAfterSomeTime, writer, 40000));
		Timer timer;
		ensure_equals(io.readAll(), "aa");
		ensure_equals(io.getBuffer(), "");
		ensure("At least 38 msec elapsed", timer.elapsed() >= 38);
		ensure("At most 50 msec elapsed", timer.elapsed() <= 50);
	}
	
	TEST_METHOD(21) {
		// It throws TimeoutException if it cannot read enough data
		// within the specified timeout.
		unsigned long long timeout = 50000;
		io.unread("he");
		write("llo");
		Timer timer;
		try {
			io.readAll(&timeout);
			fail("TimeoutException expected");
		} catch (const TimeoutException &) {
			ensure("At least 45 msec elapsed", timer.elapsed() >= 45);
			ensure("At most 65 msec elapsed", timer.elapsed() < 65);
			ensure("It deducts the waited time from the timeout", timeout < 5000);
			ensure_equals(io.getBuffer(), "");
		}
	}
	
	/***** Test readLine() *****/
	
	TEST_METHOD(25) {
		// If the connection is already closed and the buffer is empty,
		// then it returns the empty string.
		writer.close();
		ensure_equals(io.readLine(), "");
		ensure_equals(io.getBuffer(), "");
	}
	
	TEST_METHOD(26) {
		// If the connection is already closed and the buffer is non-empty,
		// then it returns the first line in the buffer.
		writer.close();
		io.unread("hello\nworld\n.");
		ensure_equals(io.readLine(), "hello\n");
		ensure_equals(io.getBuffer(), "world\n.");
		ensure_equals(io.readLine(), "world\n");
		ensure_equals(io.getBuffer(), ".");
		ensure_equals(io.readLine(), ".");
		ensure_equals(io.getBuffer(), "");
	}
	
	TEST_METHOD(27) {
		// If the buffer is empty then it reads from the connection.
		write("hello\nworld\n.");
		ensure_equals(io.readLine(), "hello\n");
		ensure_equals(io.getBuffer(), "world\n.");
	}
	
	TEST_METHOD(28) {
		// If the buffer is non-empty then it reads from the
		// buffer first, then from the connection.
		io.unread("hello");
		write("\nworld\n.");
		ensure_equals(io.readLine(), "hello\n");
		ensure_equals(io.getBuffer(), "world\n.");
		ensure_equals(io.readLine(), "world\n");
		ensure_equals(io.getBuffer(), ".");
	}
	
	TEST_METHOD(29) {
		// If the line is too long then it throws a SecurityException.
		write("abcd");
		try {
			io.readLine(3);
			fail("SecurityException expected");
		} catch (const SecurityException &) {
			// Pass.
		}
	}
	
	TEST_METHOD(30) {
		// It blocks until a line can be read or until EOF.
		TempThread thr1(boost::bind(writeAfterSomeTime, writer, 20000, "hello"));
		TempThread thr2(boost::bind(writeAfterSomeTime, writer, 35000, "\nworld\n."));
		Timer timer1;
		ensure_equals(io.readLine(), "hello\n");
		ensure_equals(io.getBuffer(), "world\n.");
		ensure("At least 33 msec elapsed", timer1.elapsed() >= 33);
		ensure("At most 45 msec elapsed", timer1.elapsed() <= 45);
		
		TempThread thr3(boost::bind(closeAfterSomeTime, writer, 20000));
		Timer timer2;
		ensure_equals(io.readLine(), "world\n");
		ensure_equals(io.getBuffer(), ".");
		ensure_equals(io.readLine(), ".");
		ensure_equals(io.getBuffer(), "");
		ensure("At least 18 msec elapsed", timer2.elapsed() >= 18);
		ensure("At most 30 msec elapsed", timer2.elapsed() <= 30);
	}
	
	TEST_METHOD(31) {
		// It throws TimeoutException if it cannot read enough data
		// within the specified timeout.
		unsigned long long timeout = 30000;
		io.unread("he");
		write("llo");
		Timer timer;
		try {
			io.readLine(1024, &timeout);
			fail("TimeoutException expected");
		} catch (const TimeoutException &) {
			ensure("At least 25 msec elapsed", timer.elapsed() >= 25);
			ensure("At most 40 msec elapsed", timer.elapsed() < 40);
			ensure("It deducts the waited time from the timeout", timeout < 5000);
			ensure_equals(io.getBuffer(), "");
		}
	}
}