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 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846
|
#include "TestSupport.h"
#include <Utils/IOUtils.h>
#include <Utils/SystemTime.h>
#include <oxt/system_calls.hpp>
#include <boost/bind.hpp>
#include <sys/types.h>
#include <cerrno>
#include <string>
using namespace Passenger;
using namespace std;
using namespace boost;
using namespace oxt;
namespace tut {
static ssize_t writevResult;
static int writevErrno;
static int writevCalled;
static string writevData;
static ssize_t writev_mock(int fildes, const struct iovec *iov, int iovcnt) {
if (writevResult >= 0) {
string data;
for (int i = 0; i < iovcnt && data.size() < (size_t) writevResult; i++) {
data.append(
(const char *) iov[i].iov_base,
iov[i].iov_len);
}
data.resize(writevResult);
writevData.append(data);
}
writevCalled++;
errno = writevErrno;
return writevResult;
}
struct IOUtilsTest {
string restBuffer;
IOUtilsTest() {
writevResult = 0;
writevErrno = 0;
writevCalled = 0;
writevData.clear();
setWritevFunction(writev_mock);
}
~IOUtilsTest() {
setWritevFunction(NULL);
}
Pipe createNonBlockingPipe() {
Pipe p = createPipe();
setNonBlocking(p.second);
return p;
}
static void writeDataAfterSomeTime(int fd, unsigned int sleepTimeInUsec) {
try {
syscalls::usleep(sleepTimeInUsec);
syscalls::write(fd, "hi", 2);
} catch (const boost::thread_interrupted &) {
// Do nothing.
}
}
static void writeDataSlowly(int fd, unsigned int bytesToWrite, unsigned int bytesPerSec) {
try {
for (unsigned i = 0; i < bytesToWrite && !this_thread::interruption_requested(); i++) {
syscalls::write(fd, "x", 1);
syscalls::usleep(1000000 / bytesPerSec);
}
} catch (const boost::thread_interrupted &) {
// Do nothing.
}
}
static void readDataAfterSomeTime(int fd, unsigned int sleepTimeInUsec) {
try {
char buf[1024 * 8];
syscalls::usleep(sleepTimeInUsec);
syscalls::read(fd, buf, sizeof(buf));
} catch (const boost::thread_interrupted &) {
// Do nothing.
}
}
static void readDataSlowly(int fd, int bytesToRead, int bytesPerSec) {
try {
unsigned long long start = SystemTime::getUsec();
unsigned long long deadline = start +
(bytesToRead * 1000000.0 / bytesPerSec);
int alreadyRead = 0;
while (alreadyRead < bytesToRead && !this_thread::interruption_requested()) {
unsigned long long elapsed = SystemTime::getUsec();
double progress = (elapsed - start) / (double) (deadline - start);
int shouldHaveRead = progress * bytesToRead;
int shouldNowRead = shouldHaveRead - alreadyRead;
if (shouldNowRead > 0) {
char *buf = new char[shouldNowRead];
ssize_t ret = syscalls::read(fd, buf, shouldNowRead);
int e = errno;
delete buf;
if (ret == -1) {
throw SystemException("read error", e);
} else if (ret == 0) {
break;
}
alreadyRead += ret;
}
syscalls::usleep(1000);
}
} catch (const boost::thread_interrupted &) {
// Do nothing.
}
}
};
DEFINE_TEST_GROUP_WITH_LIMIT(IOUtilsTest, 100);
/***** Test gatheredWrite() with empty input rest buffer *****/
TEST_METHOD(1) {
// Test complete write of a single data buffer.
StaticString data = "hello world";
writevResult = data.size();
ensure_equals(gatheredWrite(0, &data, 1, restBuffer), writevResult);
ensure_equals(writevData, "hello world");
ensure(restBuffer.empty());
}
TEST_METHOD(2) {
// Test complete write of multiple data buffers.
StaticString data[] = { "hello ", "world", "!!!!!!" };
writevResult = strlen("hello world!!!!!!");
ensure_equals(gatheredWrite(0, data, 3, restBuffer), writevResult);
ensure_equals(writevData, "hello world!!!!!!");
ensure(restBuffer.empty());
}
TEST_METHOD(3) {
// Test partial write of a single data buffer.
StaticString data = "hello world";
writevResult = 3;
ensure_equals(gatheredWrite(0, &data, 1, restBuffer), writevResult);
ensure_equals(writevData, "hel");
ensure_equals(restBuffer, "lo world");
}
TEST_METHOD(4) {
// Test partial write of multiple data buffers:
// first buffer is partially written.
StaticString data[] = { "hello ", "world", "!!!!!!" };
writevResult = 2;
ensure_equals(gatheredWrite(0, data, 3, restBuffer), writevResult);
ensure_equals(writevData, "he");
ensure_equals(restBuffer, "llo world!!!!!!");
}
TEST_METHOD(5) {
// Test partial write of multiple data buffers:
// first buffer is completely written.
StaticString data[] = { "hello ", "world", "!!!!!!" };
writevResult = 6;
ensure_equals(gatheredWrite(0, data, 3, restBuffer), writevResult);
ensure_equals(writevData, "hello ");
ensure_equals(restBuffer, "world!!!!!!");
}
TEST_METHOD(6) {
// Test partial write of multiple data buffers:
// non-first buffer is partially written.
StaticString data[] = { "hello ", "world", "!!!!!!" };
writevResult = 8;
ensure_equals(gatheredWrite(0, data, 3, restBuffer), writevResult);
ensure_equals(writevData, "hello wo");
ensure_equals(restBuffer, "rld!!!!!!");
}
TEST_METHOD(7) {
// Test partial write of multiple data buffers:
// non-first buffer is completely written.
StaticString data[] = { "hello ", "world", "!!!!!!" };
writevResult = 11;
ensure_equals(gatheredWrite(0, data, 3, restBuffer), writevResult);
ensure_equals(writevData, "hello world");
ensure_equals(restBuffer, "!!!!!!");
}
TEST_METHOD(8) {
// Test failed write of a single data buffer: blocking error.
StaticString data = "hello world";
writevResult = -1;
writevErrno = EAGAIN;
ensure_equals(gatheredWrite(0, &data, 1, restBuffer), 0);
ensure_equals(restBuffer, "hello world");
}
TEST_METHOD(9) {
// Test failed write of a single data buffer: other error.
StaticString data = "hello world";
writevResult = -1;
writevErrno = EBADF;
ssize_t ret = gatheredWrite(0, &data, 1, restBuffer);
int e = errno;
ensure_equals(ret, -1);
ensure_equals(e, EBADF);
ensure_equals("Rest buffer remains untouched", restBuffer, "");
}
TEST_METHOD(10) {
// Test failed write of multiple data buffers: blocking error.
StaticString data[] = { "hello ", "world", "!!!" };
writevResult = -1;
writevErrno = EAGAIN;
ensure_equals(gatheredWrite(0, data, 3, restBuffer), 0);
ensure_equals(restBuffer, "hello world!!!");
}
TEST_METHOD(11) {
// Test failed write of multiple data buffers: other error.
StaticString data[] = { "hello ", "world", "!!!" };
writevResult = -1;
writevErrno = EBADF;
ssize_t ret = gatheredWrite(0, data, 3, restBuffer);
int e = errno;
ensure_equals(ret, -1);
ensure_equals(e, EBADF);
ensure_equals("Rest buffer remains untouched", restBuffer, "");
}
TEST_METHOD(12) {
// Test writing nothing.
StaticString data[] = { "", "", "" };
ssize_t ret = gatheredWrite(0, data, 3, restBuffer);
int e = errno;
ensure_equals(ret, 0);
ensure_equals(e, 0);
ensure_equals(writevCalled, 0);
ensure_equals(restBuffer, "");
}
TEST_METHOD(13) {
// Test writing multiple buffers where some are empty.
StaticString data[] = { "hello ", "", "world" };
writevResult = strlen("hello world");
ensure_equals(gatheredWrite(0, data, 3, restBuffer), writevResult);
ensure_equals(writevData, "hello world");
ensure_equals(restBuffer, "");
}
/***** Test gatheredWrite() with non-empty input rest buffer *****/
TEST_METHOD(15) {
// Test complete write with a single data buffer.
restBuffer = "oh ";
StaticString data = "hello world";
writevResult = restBuffer.size() + data.size();
ensure_equals(gatheredWrite(0, &data, 1, restBuffer), writevResult);
ensure_equals(writevData, "oh hello world");
ensure(restBuffer.empty());
}
TEST_METHOD(16) {
// Test complete write with multiple data buffers.
restBuffer = "oh ";
StaticString data[] = { "hello ", "world", "!!!" };
writevResult = strlen("oh hello world!!!");
ensure_equals(gatheredWrite(0, data, 3, restBuffer), writevResult);
ensure_equals(writevData, "oh hello world!!!");
ensure(restBuffer.empty());
}
TEST_METHOD(17) {
// Test partial write of a single data buffer.
StaticString data = "hello world";
writevResult = 3;
ensure_equals(gatheredWrite(0, &data, 1, restBuffer), writevResult);
ensure_equals(writevData, "hel");
ensure_equals(restBuffer, "lo world");
}
TEST_METHOD(18) {
// Test partial write of multiple data buffers:
// rest buffer is partially written.
restBuffer = "oh ";
StaticString data[] = { "hello ", "world", "!!!" };
writevResult = 2;
ensure_equals(gatheredWrite(0, data, 3, restBuffer), writevResult);
ensure_equals(writevData, "oh");
ensure_equals(restBuffer, " hello world!!!");
}
TEST_METHOD(19) {
// Test partial write of multiple data buffers:
// rest buffer is completely written.
restBuffer = "oh ";
StaticString data[] = { "hello ", "world", "!!!" };
writevResult = strlen("oh ");
ensure_equals(gatheredWrite(0, data, 3, restBuffer), writevResult);
ensure_equals(writevData, "oh ");
ensure_equals(restBuffer, "hello world!!!");
}
TEST_METHOD(20) {
// Test partial write of multiple data buffers:
// first buffer is partially written.
restBuffer = "oh ";
StaticString data[] = { "hello ", "world", "!!!" };
writevResult = strlen("oh h");
ensure_equals(gatheredWrite(0, data, 3, restBuffer), writevResult);
ensure_equals(writevData, "oh h");
ensure_equals(restBuffer, "ello world!!!");
}
TEST_METHOD(21) {
// Test partial write of multiple data buffers:
// first buffer is completely written.
restBuffer = "oh ";
StaticString data[] = { "hello ", "world", "!!!" };
writevResult = strlen("oh hello ");
ensure_equals(gatheredWrite(0, data, 3, restBuffer), writevResult);
ensure_equals(writevData, "oh hello ");
ensure_equals(restBuffer, "world!!!");
}
TEST_METHOD(22) {
// Test partial write of multiple data buffers:
// non-first buffer is partially written.
restBuffer = "oh ";
StaticString data[] = { "hello ", "world", "!!!" };
writevResult = strlen("oh hello wo");
ensure_equals(gatheredWrite(0, data, 3, restBuffer), writevResult);
ensure_equals(writevData, "oh hello wo");
ensure_equals(restBuffer, "rld!!!");
}
TEST_METHOD(23) {
// Test partial write of multiple data buffers:
// non-first buffer is completely written.
restBuffer = "oh ";
StaticString data[] = { "hello ", "world", "!!!" };
writevResult = strlen("oh hello world");
ensure_equals(gatheredWrite(0, data, 3, restBuffer), writevResult);
ensure_equals(writevData, "oh hello world");
ensure_equals(restBuffer, "!!!");
}
TEST_METHOD(24) {
// Test failed write of a single data buffer: blocking error.
restBuffer = "oh ";
StaticString data = "hello world";
writevResult = -1;
writevErrno = EAGAIN;
ensure_equals(gatheredWrite(0, &data, 1, restBuffer), 0);
ensure_equals(restBuffer, "oh hello world");
}
TEST_METHOD(25) {
// Test failed write of a single data buffer: other error.
restBuffer = "oh ";
StaticString data = "hello world";
writevResult = -1;
writevErrno = EBADF;
ssize_t ret = gatheredWrite(0, &data, 1, restBuffer);
int e = errno;
ensure_equals(ret, -1);
ensure_equals(e, EBADF);
ensure_equals("Rest buffer remains untouched", restBuffer, "oh ");
}
TEST_METHOD(26) {
// Test failed write of multiple data buffers: blocking error.
restBuffer = "oh ";
StaticString data[] = { "hello ", "world", "!!!" };
writevResult = -1;
writevErrno = EAGAIN;
ensure_equals(gatheredWrite(0, data, 3, restBuffer), 0);
ensure_equals(restBuffer, "oh hello world!!!");
}
TEST_METHOD(27) {
// Test failed write of multiple data buffers: other error.
restBuffer = "oh ";
StaticString data[] = { "hello ", "world", "!!!" };
writevResult = -1;
writevErrno = EBADF;
ssize_t ret = gatheredWrite(0, data, 3, restBuffer);
int e = errno;
ensure_equals(ret, -1);
ensure_equals(e, EBADF);
ensure_equals("Rest buffer remains untouched", restBuffer, "oh ");
}
TEST_METHOD(28) {
// Test writing multiple buffers that are all empty.
restBuffer = "oh ";
StaticString data[] = { "", "", "" };
writevResult = 3;
ensure_equals(gatheredWrite(0, data, 3, restBuffer), writevResult);
ensure_equals(writevData, "oh ");
ensure_equals(restBuffer, "");
}
TEST_METHOD(29) {
// Test writing multiple buffers where one is empty.
restBuffer = "oh ";
StaticString data[] = { "hello ", "", "world" };
writevResult = strlen("oh hello world");
ensure_equals(gatheredWrite(0, data, 3, restBuffer), writevResult);
ensure_equals(writevData, "oh hello world");
ensure_equals(restBuffer, "");
}
/***** Test gatheredWrite() blocking version *****/
TEST_METHOD(35) {
// It doesn't call writev() if requested to send 0 bytes.
StaticString data[2] = { "", "" };
gatheredWrite(0, data, 2);
ensure_equals(writevCalled, 0);
}
TEST_METHOD(36) {
// Test sending all data in a single writev() call.
StaticString data[] = { "hello", "my", "world" };
writevResult = strlen("hellomyworld");
gatheredWrite(0, data, 3);
ensure_equals(writevData, "hellomyworld");
ensure_equals(writevCalled, 1);
}
TEST_METHOD(42) {
// Test writing byte-by-byte.
StaticString data[] = { "hello", "my", "world", "!!" };
writevResult = 1;
gatheredWrite(0, data, 4);
ensure_equals(writevCalled, (int) strlen("hellomyworld!!"));
ensure_equals(writevData, "hellomyworld!!");
}
TEST_METHOD(43) {
// Test writev() writing in chunks of 2 bytes.
StaticString data[] = { "hello", "my", "world", "!!" };
writevResult = 2;
gatheredWrite(0, data, 4);
ensure_equals(writevCalled, (int) strlen("hellomyworld!!") / 2);
ensure_equals(writevData, "hellomyworld!!");
}
static ssize_t writev_mock_44(int fildes, const struct iovec *iov, int iovcnt) {
if (writevCalled == 3) {
// Have the last call return 2 instead of 4.
writevResult = 2;
}
return writev_mock(fildes, iov, iovcnt);
}
TEST_METHOD(44) {
// Test writev() writing in chunks of 4 bytes.
setWritevFunction(writev_mock_44);
StaticString data[] = { "hello", "my", "world", "!!" };
writevResult = 4;
gatheredWrite(0, data, 4);
ensure_equals(writevCalled, 4);
ensure_equals(writevData, "hellomyworld!!");
}
TEST_METHOD(45) {
// Test writev() timeout support.
setWritevFunction(NULL);
Pipe p = createPipe();
unsigned long long startTime = SystemTime::getUsec();
unsigned long long timeout = 30000;
try {
StaticString data[] = { "hello", "world" };
for (int i = 0; i < 1024 * 1024; i++) {
gatheredWrite(p[1], data, 2, &timeout);
}
fail("TimeoutException expected");
} catch (const TimeoutException &) {
unsigned long long elapsed = SystemTime::getUsec() - startTime;
ensure("30 msec have passed", elapsed >= 29000 && elapsed <= 45000);
ensure(timeout <= 2000);
}
}
/***** Test waitUntilReadable() *****/
TEST_METHOD(50) {
// waitUntilReadable() waits for the specified timeout if no data is readable.
Pipe p = createPipe();
unsigned long long timeout = 25000;
ensure("No data is available", !waitUntilReadable(p.first, &timeout));
ensure("The passed time is deducted from the timeout", timeout < 5000);
}
TEST_METHOD(51) {
// waitUntilReadable() waits for less than the specified timeout if data
// is not available immediately but still available before the timeout.
Pipe p = createPipe();
TempThread thr(boost::bind(&writeDataAfterSomeTime, p.second, 35000));
unsigned long long timeout = 1000000;
ensure("Data is available", waitUntilReadable(p.first, &timeout));
ensure("At least 35 msec passed.", timeout <= 1000000 - 35000);
ensure("At most 70 msec passed.", timeout >= 1000000 - 70000); // depends on system scheduler though
}
TEST_METHOD(52) {
// waitUntilReadable() returns immediately if timeout is 0.
Pipe p = createPipe();
unsigned long long timeout = 0;
ensure("No data is available", !waitUntilReadable(p.first, &timeout));
ensure_equals("Timeout is not modified", timeout, 0u);
write(p.second, "hi", 2);
ensure("Data is available", waitUntilReadable(p.first, &timeout));
ensure_equals("Timeout is not modified", timeout, 0u);
}
TEST_METHOD(53) {
// waitUntilReadable() returns immediately if there's data immediately available.
Pipe p = createPipe();
unsigned long long timeout = 100000;
write(p.second, "hi", 2);
ensure("Data is available", waitUntilReadable(p.first, &timeout));
ensure("Timeout is not modified", timeout >= 100000 - 5000);
}
/***** Test readExact() *****/
TEST_METHOD(54) {
// readExact() throws TimeoutException if no data is received within the timeout.
Pipe p = createPipe();
unsigned long long timeout = 50000;
char buf;
try {
readExact(p.first, &buf, 1, &timeout);
fail("No TimeoutException thrown.");
} catch (const TimeoutException &) {
ensure("The passed time is deducted from timeout", timeout < 5000);
}
}
TEST_METHOD(55) {
// readExact() throws TimeoutException if not enough data is received within the timeout.
Pipe p = createPipe();
unsigned long long timeout = 20000;
char buf[100];
TempThread thr(boost::bind(&writeDataSlowly, p.second, sizeof(buf), 1));
try {
readExact(p.first, &buf, sizeof(buf), &timeout);
fail("No TimeoutException thrown.");
} catch (const TimeoutException &) {
ensure("The passed time is deducted from timeout", timeout < 5000);
}
}
TEST_METHOD(56) {
// readExact() throws TimeoutException if timeout is 0 and no data is immediately available.
Pipe p = createPipe();
unsigned long long timeout = 0;
char buf;
try {
readExact(p.first, &buf, 1, &timeout);
fail("No TimeoutException thrown.");
} catch (const TimeoutException &) {
ensure_equals("Timeout unchanged", timeout, 0u);
}
}
TEST_METHOD(57) {
// readExact() throws TimeoutException if timeout is 0 and not enough data is
// immediately available.
Pipe p = createPipe();
unsigned long long timeout = 0;
write(p.second, "hi", 2);
try {
char buf[100];
readExact(p.first, &buf, sizeof(buf), &timeout);
fail("No TimeoutException thrown.");
} catch (const TimeoutException &) {
ensure_equals("Timeout is unchanged", timeout, 0u);
}
}
TEST_METHOD(58) {
// readExact() deducts the amount of time spent on waiting from the timeout variable.
Pipe p = createPipe();
unsigned long long timeout = 60000;
char buf[3];
// Spawn a thread that writes 100 bytes per second, i.e. each byte takes 10 msec.
TempThread thr(boost::bind(&writeDataSlowly, p.second, 1000, 100));
// We read 3 bytes.
ensure_equals(readExact(p.first, &buf, sizeof(buf), &timeout), 3u);
ensure("Should have taken at least 20 msec", timeout <= 60000 - 20000);
ensure("Should have taken at most 40 msec", timeout >= 60000 - 40000);
}
TEST_METHOD(59) {
// readExact() does not wait and does not modify the timeout variable if there's
// immediately enough data available.
Pipe p = createPipe();
unsigned long long timeout = 100000;
char buf[2];
write(p.second, "hi", 2);
ensure_equals(readExact(p.first, &buf, 2, &timeout), 2u);
ensure("Timeout not modified", timeout >= 95000);
}
/***** Test waitUntilWritable() *****/
TEST_METHOD(60) {
// waitUntilWritable() waits for the specified timeout if no data is writable.
Pipe p = createNonBlockingPipe();
writeUntilFull(p.second);
unsigned long long timeout = 25000;
ensure("Socket did not become writable", !waitUntilWritable(p.second, &timeout));
ensure("The passed time is deducted from the timeout", timeout < 5000);
}
TEST_METHOD(61) {
// waitUntilWritable() waits for less than the specified timeout if the fd
// is not immediately writable but still writable before the timeout.
Pipe p = createNonBlockingPipe();
writeUntilFull(p.second);
TempThread thr(boost::bind(&readDataAfterSomeTime, p.first, 35000));
unsigned long long timeout = 1000000;
ensure("Socket became writable", waitUntilWritable(p.second, &timeout));
ensure("At least 35 msec passed.", timeout <= 1000000 - 35000);
ensure("At most 70 msec passed.", timeout >= 1000000 - 70000); // depends on system scheduler though
}
TEST_METHOD(62) {
// waitUntilWritable() returns immediately if timeout is 0.
Pipe p = createNonBlockingPipe();
writeUntilFull(p.second);
unsigned long long timeout = 0;
ensure("Socket is not writable", !waitUntilWritable(p.second, &timeout));
ensure_equals("Timeout is not modified", timeout, 0u);
char buf[1024 * 8];
read(p.first, buf, sizeof(buf));
ensure("Socket became writable", waitUntilWritable(p.second, &timeout));
ensure_equals("Timeout is not modified", timeout, 0u);
}
TEST_METHOD(63) {
// waitUntilWritable() returns immediately if the fd is immediately writable.
Pipe p = createNonBlockingPipe();
writeUntilFull(p.second);
unsigned long long timeout = 100000;
char buf[1024 * 8];
read(p.first, buf, sizeof(buf));
ensure("Socket became writable", waitUntilWritable(p.second, &timeout));
ensure("Timeout is not modified", timeout >= 100000 - 5000);
}
/***** Test readExact() *****/
TEST_METHOD(64) {
// writeExact() throws TimeoutException if fd does not become writable within the timeout.
Pipe p = createNonBlockingPipe();
writeUntilFull(p.second);
unsigned long long timeout = 50000;
try {
writeExact(p.second, "x", 1, &timeout);
fail("No TimeoutException thrown.");
} catch (const TimeoutException &) {
ensure("The passed time is deducted from timeout", timeout < 5000);
}
}
TEST_METHOD(65) {
// writeExact() throws TimeoutException if not enough data is written within the timeout.
Pipe p = createNonBlockingPipe();
writeUntilFull(p.second);
unsigned long long timeout = 20000;
char buf[1024 * 3];
TempThread thr(boost::bind(&readDataSlowly, p.first, sizeof(buf), 512));
try {
writeExact(p.second, "x", 1, &timeout);
fail("No TimeoutException thrown.");
} catch (const TimeoutException &) {
ensure("The passed time is deducted from timeout", timeout < 5000);
}
}
TEST_METHOD(66) {
// writeExact() throws TimeoutException if timeout is 0 and the fd is not immediately writable.
Pipe p = createNonBlockingPipe();
writeUntilFull(p.second);
unsigned long long timeout = 0;
try {
writeExact(p.second, "x", 1, &timeout);
fail("No TimeoutException thrown.");
} catch (const TimeoutException &) {
ensure_equals("Timeout unchanged", timeout, 0u);
}
}
TEST_METHOD(67) {
// writeExact() throws TimeoutException if timeout is 0 not enough data could be written immediately.
Pipe p = createNonBlockingPipe();
writeUntilFull(p.second);
unsigned long long timeout = 0;
char buf[1024];
read(p.first, buf, sizeof(buf));
char buf2[1024 * 8];
memset(buf2, 0, sizeof(buf2));
try {
writeExact(p.second, buf2, sizeof(buf2), &timeout);
fail("No TimeoutException thrown.");
} catch (const TimeoutException &) {
ensure_equals("Timeout is unchanged", timeout, 0u);
}
}
TEST_METHOD(68) {
// readExact() deducts the amount of time spent on waiting from the timeout variable.
Pipe p = createNonBlockingPipe();
unsigned long long timeout = 60000;
// Spawn a thread that reads 200000 bytes in 35 msec.
TempThread thr(boost::bind(&readDataSlowly, p.first, 5714286, 5714286));
// We write 200000 bytes.
char buf[200000];
writeExact(p.second, &buf, sizeof(buf), &timeout);
ensure("Should have taken at least 20 msec", timeout <= 60000 - 20000);
ensure("Should have taken at most 40 msec", timeout >= 60000 - 40000);
}
TEST_METHOD(69) {
// writeExact() does not wait and does not modify the timeout variable if
// all data can be written immediately.
Pipe p = createNonBlockingPipe();
unsigned long long timeout = 100000;
char buf[1024];
writeExact(p.second, buf, sizeof(buf), &timeout);
ensure("Timeout not modified", timeout >= 95000);
}
/***** Test getSocketAddressType() *****/
TEST_METHOD(70) {
ensure_equals(getSocketAddressType(""), SAT_UNKNOWN);
ensure_equals(getSocketAddressType("/foo.socket"), SAT_UNKNOWN);
ensure_equals(getSocketAddressType("unix:"), SAT_UNKNOWN);
ensure_equals(getSocketAddressType("unix:/"), SAT_UNIX);
ensure_equals(getSocketAddressType("unix:/foo.socket"), SAT_UNIX);
ensure_equals(getSocketAddressType("tcp:"), SAT_UNKNOWN);
ensure_equals(getSocketAddressType("tcp://"), SAT_UNKNOWN);
// Doesn't check whether it contains port
ensure_equals(getSocketAddressType("tcp://127.0.0.1"), SAT_TCP);
ensure_equals(getSocketAddressType("tcp://127.0.0.1:80"), SAT_TCP);
}
TEST_METHOD(71) {
ensure_equals(parseUnixSocketAddress("unix:/foo.socket"), "/foo.socket");
try {
parseUnixSocketAddress("unix:");
fail("ArgumentException expected");
} catch (const ArgumentException &e) {
// Pass.
}
}
TEST_METHOD(72) {
string host;
unsigned short port;
parseTcpSocketAddress("tcp://127.0.0.1:80", host, port);
ensure_equals(host, "127.0.0.1");
ensure_equals(port, 80);
try {
parseTcpSocketAddress("tcp://", host, port);
fail("ArgumentException expected");
} catch (const ArgumentException &e) {
// Pass.
}
}
/***** Test readFileDescriptor() and writeFileDescriptor() *****/
TEST_METHOD(80) {
// Test whether it works.
SocketPair sockets = createUnixSocketPair();
Pipe pipes = createPipe();
writeFileDescriptor(sockets[0], pipes[1]);
FileDescriptor fd = readFileDescriptor(sockets[1]);
writeExact(fd, "hello");
char buf[6];
ensure_equals(readExact(pipes[0], buf, 5), 5u);
buf[5] = '\0';
ensure_equals(StaticString(buf), "hello");
}
TEST_METHOD(81) {
// Test whether timeout works.
SocketPair sockets = createUnixSocketPair();
Pipe pipes = createPipe();
unsigned long long timeout = 30000;
unsigned long long startTime = SystemTime::getUsec();
try {
FileDescriptor fd = readFileDescriptor(sockets[0], &timeout);
fail("TimeoutException expected");
} catch (const TimeoutException &) {
unsigned long long elapsed = SystemTime::getUsec() - startTime;
ensure("readFileDescriptor() timed out after 30 msec",
elapsed >= 29000 && elapsed <= 45000);
ensure(timeout <= 2000);
}
writeUntilFull(sockets[0]);
startTime = SystemTime::getUsec();
timeout = 30000;
try {
writeFileDescriptor(sockets[0], pipes[0], &timeout);
fail("TimeoutException expected");
} catch (const TimeoutException &) {
unsigned long long elapsed = SystemTime::getUsec() - startTime;
ensure("writeFileDescriptor() timed out after 30 msec",
elapsed >= 29000 && elapsed <= 45000);
ensure(timeout <= 2000);
}
}
}
|