dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2008 Free Software Foundation, Inc. |
| 3 | * |
| 4 | * |
| 5 | * This software is distributed under the terms of the GNU Affero Public License. |
| 6 | * See the COPYING file in the main directory for details. |
| 7 | * |
| 8 | * This use of this software may be subject to additional restrictions. |
| 9 | * See the LEGAL file in the main directory for details. |
| 10 | |
| 11 | This program is free software: you can redistribute it and/or modify |
| 12 | it under the terms of the GNU Affero General Public License as published by |
| 13 | the Free Software Foundation, either version 3 of the License, or |
| 14 | (at your option) any later version. |
| 15 | |
| 16 | This program is distributed in the hope that it will be useful, |
| 17 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | GNU Affero General Public License for more details. |
| 20 | |
| 21 | You should have received a copy of the GNU Affero General Public License |
| 22 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 23 | |
| 24 | */ |
| 25 | |
| 26 | |
| 27 | |
| 28 | |
| 29 | #include "Timeval.h" |
| 30 | #include <iostream> |
Pau Espin Pedrol | 2652f2b | 2018-01-09 19:12:21 +0100 | [diff] [blame] | 31 | #include <assert.h> |
Pau Espin Pedrol | 28ce315 | 2018-01-15 11:46:26 +0100 | [diff] [blame] | 32 | #include <sys/time.h> |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 33 | |
Pau Espin Pedrol | 2518588 | 2018-12-12 17:39:08 +0100 | [diff] [blame] | 34 | extern "C" { |
| 35 | #include <osmocom/core/timer.h> |
| 36 | } |
| 37 | |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 38 | using namespace std; |
| 39 | |
| 40 | int main(int argc, char *argv[]) |
| 41 | { |
Pau Espin Pedrol | 2518588 | 2018-12-12 17:39:08 +0100 | [diff] [blame] | 42 | |
| 43 | osmo_clock_override_enable(CLOCK_REALTIME, true); |
| 44 | |
| 45 | struct timespec *clk = osmo_clock_override_gettimespec(CLOCK_REALTIME); |
| 46 | clk->tv_sec = 0; |
| 47 | clk->tv_nsec = 1000; |
| 48 | |
| 49 | long last_remaining = 10000; /*10 sec */ |
Pau Espin Pedrol | 28ce315 | 2018-01-15 11:46:26 +0100 | [diff] [blame] | 50 | Timeval then(last_remaining); |
| 51 | assert(then.elapsed() == -last_remaining); |
| 52 | cerr << then << " elapsed: " << then.elapsed() << endl; |
| 53 | |
| 54 | /* Check that last_remaining parameter affects setting time in the future */ |
Pau Espin Pedrol | 2518588 | 2018-12-12 17:39:08 +0100 | [diff] [blame] | 55 | osmo_clock_override_add(CLOCK_REALTIME, 0, 10*1000*1000); |
Pau Espin Pedrol | 28ce315 | 2018-01-15 11:46:26 +0100 | [diff] [blame] | 56 | double increased_time_secs = Timeval().seconds(); |
Pau Espin Pedrol | 2518588 | 2018-12-12 17:39:08 +0100 | [diff] [blame] | 57 | assert(increased_time_secs < then.seconds()); |
Pau Espin Pedrol | 28ce315 | 2018-01-15 11:46:26 +0100 | [diff] [blame] | 58 | |
| 59 | struct timespec invariant_time = then.timespec(); |
Pau Espin Pedrol | 2652f2b | 2018-01-09 19:12:21 +0100 | [diff] [blame] | 60 | int loops = 0; |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 61 | |
| 62 | while (!then.passed()) { |
Pau Espin Pedrol | 28ce315 | 2018-01-15 11:46:26 +0100 | [diff] [blame] | 63 | struct timespec tspecnow = then.timespec(); |
Pau Espin Pedrol | 2518588 | 2018-12-12 17:39:08 +0100 | [diff] [blame] | 64 | cerr << "["<< loops << "] now: " << Timeval().seconds() << " then: " << then << " remaining: " << then.remaining() << endl; |
Pau Espin Pedrol | 28ce315 | 2018-01-15 11:46:26 +0100 | [diff] [blame] | 65 | assert(last_remaining >= then.remaining()); |
| 66 | assert(tspecnow.tv_sec == invariant_time.tv_sec && tspecnow.tv_nsec == invariant_time.tv_nsec); |
Pau Espin Pedrol | 2518588 | 2018-12-12 17:39:08 +0100 | [diff] [blame] | 67 | osmo_clock_override_add(CLOCK_REALTIME, 0, 500000*1000); |
Pau Espin Pedrol | 2652f2b | 2018-01-09 19:12:21 +0100 | [diff] [blame] | 68 | loops++; |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 69 | } |
Pau Espin Pedrol | 2652f2b | 2018-01-09 19:12:21 +0100 | [diff] [blame] | 70 | cerr << "now: " << Timeval() << " then: " << then << " remaining: " << then.remaining() << endl; |
Pau Espin Pedrol | 2518588 | 2018-12-12 17:39:08 +0100 | [diff] [blame] | 71 | assert(then.remaining() == -10); |
| 72 | assert(loops == 20); |
Pau Espin Pedrol | 2652f2b | 2018-01-09 19:12:21 +0100 | [diff] [blame] | 73 | |
| 74 | printf("Done\n"); |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 75 | } |