rpc_uptime.py raw

   1  #!/usr/bin/env python3
   2  # Copyright (c) 2017-present The Bitcoin Core developers
   3  # Distributed under the MIT software license, see the accompanying
   4  # file COPYING or http://www.opensource.org/licenses/mit-license.php.
   5  """Test the RPC call related to the uptime command.
   6  
   7  Test corresponds to code in rpc/server.cpp.
   8  """
   9  
  10  import time
  11  
  12  from test_framework.test_framework import BitcoinTestFramework
  13  
  14  
  15  class UptimeTest(BitcoinTestFramework):
  16      def set_test_params(self):
  17          self.num_nodes = 1
  18          self.setup_clean_chain = True
  19  
  20      def run_test(self):
  21          self._test_uptime()
  22  
  23      def _test_uptime(self):
  24          time.sleep(1) # Do some work before checking uptime
  25          uptime_before = self.nodes[0].uptime()
  26          assert uptime_before > 0, "uptime should begin at app start"
  27  
  28          wait_time = 20_000
  29          self.nodes[0].setmocktime(int(time.time()) + wait_time)
  30          uptime_after = self.nodes[0].uptime()
  31          self.nodes[0].setmocktime(0)
  32          assert uptime_after - uptime_before < wait_time, "uptime should not jump with wall clock"
  33  
  34  
  35  if __name__ == '__main__':
  36      UptimeTest(__file__).main()
  37