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