Change in osmo-trx[master]: TimevalTest: Make test deterministic with fake time

This is merely a historical archive of years 2008-2021, before the migration to mailman3.

A maintained and still updated list archive can be found at https://lists.osmocom.org/hyperkitty/list/gerrit-log@lists.osmocom.org/.

Pau Espin Pedrol gerrit-no-reply at lists.osmocom.org
Wed Dec 12 17:07:45 UTC 2018


Pau Espin Pedrol has uploaded this change for review. ( https://gerrit.osmocom.org/12282


Change subject: TimevalTest: Make test deterministic with fake time
......................................................................

TimevalTest: Make test deterministic with fake time

Change-Id: I74e577a0142fb6d1ef3630e02aff9910b191bff9
---
M tests/CommonLibs/TimevalTest.cpp
1 file changed, 18 insertions(+), 7 deletions(-)



  git pull ssh://gerrit.osmocom.org:29418/osmo-trx refs/changes/82/12282/1

diff --git a/tests/CommonLibs/TimevalTest.cpp b/tests/CommonLibs/TimevalTest.cpp
index 61a92c9..b822982 100644
--- a/tests/CommonLibs/TimevalTest.cpp
+++ b/tests/CommonLibs/TimevalTest.cpp
@@ -31,34 +31,45 @@
 #include <assert.h>
 #include <sys/time.h>
 
+extern "C" {
+#include <osmocom/core/timer.h>
+}
+
 using namespace std;
 
 int main(int argc, char *argv[])
 {
-	long last_remaining = 10000;
+
+	osmo_clock_override_enable(CLOCK_REALTIME, true);
+
+	struct timespec *clk = osmo_clock_override_gettimespec(CLOCK_REALTIME);
+	clk->tv_sec = 0;
+	clk->tv_nsec = 1000;
+
+	long last_remaining = 10000; /*10 sec */
 	Timeval then(last_remaining);
 	assert(then.elapsed() == -last_remaining);
 	cerr << then << " elapsed: " << then.elapsed() << endl;
 
 	/* Check that last_remaining parameter affects setting time in the future */
-	usleep(10000);
+	osmo_clock_override_add(CLOCK_REALTIME, 0, 10*1000*1000);
 	double increased_time_secs = Timeval().seconds();
-	assert(increased_time_secs <= then.seconds());
+	assert(increased_time_secs < then.seconds());
 
 	struct timespec invariant_time  = then.timespec();
 	int loops = 0;
 
 	while (!then.passed()) {
 		struct timespec tspecnow = then.timespec();
-		cerr << "now: " << Timeval().seconds() << " then: " << then << " remaining: " << then.remaining() << endl;
+		cerr << "["<< loops << "] now: " << Timeval().seconds() << " then: " << then << " remaining: " << then.remaining() << endl;
 		assert(last_remaining >= then.remaining());
 		assert(tspecnow.tv_sec == invariant_time.tv_sec && tspecnow.tv_nsec == invariant_time.tv_nsec);
-		usleep(500000);
+		osmo_clock_override_add(CLOCK_REALTIME, 0, 500000*1000);
 		loops++;
 	}
 	cerr << "now: " << Timeval() << " then: " << then << " remaining: " << then.remaining() << endl;
-	assert(then.remaining() <= 0);
-	assert(loops >= 18);
+	assert(then.remaining() == -10);
+	assert(loops == 20);
 
 	printf("Done\n");
 }

-- 
To view, visit https://gerrit.osmocom.org/12282
To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings

Gerrit-Project: osmo-trx
Gerrit-Branch: master
Gerrit-MessageType: newchange
Gerrit-Change-Id: I74e577a0142fb6d1ef3630e02aff9910b191bff9
Gerrit-Change-Number: 12282
Gerrit-PatchSet: 1
Gerrit-Owner: Pau Espin Pedrol <pespin at sysmocom.de>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osmocom.org/pipermail/gerrit-log/attachments/20181212/143705bb/attachment.htm>


More information about the gerrit-log mailing list