Skip to content

Commit

Permalink
timing sensitivity
Browse files Browse the repository at this point in the history
  • Loading branch information
FooBarWidget committed Oct 4, 2024
1 parent a275382 commit 7a3fdee
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
18 changes: 9 additions & 9 deletions test/cxx/IOTools/BufferedIOTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,14 +123,14 @@ namespace tut {
ensure_equals(io.readUntil(a_twoBytesRead), 2u);
ensure_equals(readData, "aa");
ensure("At least 18 msec elapsed", timer1.elapsed() >= 18);
ensure("At most 90 msec elapsed", timer1.elapsed() <= 90);
ensure("At most 150 msec elapsed", timer1.elapsed() <= 150);

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 90 msec elapsed", timer2.elapsed() <= 90);
ensure("At most 150 msec elapsed", timer2.elapsed() <= 150);
}

TEST_METHOD(6) {
Expand All @@ -145,7 +145,7 @@ namespace tut {
fail("TimeoutException expected");
} catch (const TimeoutException &) {
ensure("At least 45 msec elapsed", timer.elapsed() >= 45);
ensure("At most 90 msec elapsed", timer.elapsed() < 90);
ensure("At most 150 msec elapsed", timer.elapsed() < 150);
ensure("It deducts the waited time from the timeout", timeout < 5000);
ensure_equals(readData, "hello");
ensure_equals(io.getBuffer(), "");
Expand Down Expand Up @@ -207,20 +207,20 @@ namespace tut {
}

TEST_METHOD(15) {
// It blocks until the given number of bytes are read or until EOF.
set_test_name("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 90 msec elapsed", timer1.elapsed() <= 90);
ensure("At most 150 msec elapsed", timer1.elapsed() <= 150);

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 90 msec elapsed", timer2.elapsed() <= 90);
ensure("At most 150 msec elapsed", timer2.elapsed() <= 150);
}

TEST_METHOD(16) {
Expand All @@ -235,7 +235,7 @@ namespace tut {
fail("TimeoutException expected");
} catch (const TimeoutException &) {
ensure("At least 45 msec elapsed", timer.elapsed() >= 45);
ensure("At most 95 msec elapsed", timer.elapsed() < 95);
ensure("At most 150 msec elapsed", timer.elapsed() < 150);
ensure("It deducts the waited time from the timeout", timeout < 5000);
ensure_equals(io.getBuffer(), "");
}
Expand Down Expand Up @@ -268,7 +268,7 @@ namespace tut {
fail("TimeoutException expected");
} catch (const TimeoutException &) {
ensure("At least 45 msec elapsed", timer.elapsed() >= 45);
ensure("At most 95 msec elapsed", timer.elapsed() < 95);
ensure("At most 150 msec elapsed", timer.elapsed() <= 150);
ensure("It deducts the waited time from the timeout", timeout < 5000);
ensure_equals(io.getBuffer(), "");
}
Expand Down Expand Up @@ -358,7 +358,7 @@ namespace tut {
fail("TimeoutException expected");
} catch (const TimeoutException &) {
ensure("At least 25 msec elapsed", timer.elapsed() >= 25);
ensure("At most 90 msec elapsed", timer.elapsed() < 90);
ensure("At most 150 msec elapsed", timer.elapsed() < 150);
ensure("It deducts the waited time from the timeout", timeout < 5000);
ensure_equals(io.getBuffer(), "");
}
Expand Down
8 changes: 4 additions & 4 deletions test/cxx/IOTools/IOUtilsTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -619,18 +619,18 @@ namespace tut {
}

TEST_METHOD(58) {
// readExact() deducts the amount of time spent on waiting from the timeout variable.
set_test_name("readExact() deducts the amount of time spent on waiting from the timeout variable");
Pipe p = createPipe(__FILE__, __LINE__);
unsigned long long timeout = 100000;
unsigned long long timeout = 500000;
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 <= 100000 - 20000);
ensure("Should have taken at most 50 msec", timeout >= 100000 - 50000);
ensure("Should have taken at least 25 msec", timeout <= 500000 - 25000);
ensure("Should have taken at most 150 msec", timeout >= 500000 - 150000);
}

TEST_METHOD(59) {
Expand Down

0 comments on commit 7a3fdee

Please sign in to comment.