github.com/mit-dci/lit@v0.0.0-20221102210550-8c3d3b49f2ce/test/testutil.py (about)

     1  #!/usr/bin/env python3
     2  # Copyright (c) 2018 The lit developers
     3  # Distributed under the MIT software license, see the accompanying
     4  # file LICENSE or http://www.opensource.org/licenses/mit-license.php.
     5  """Utils for lit testing"""
     6  import time
     7  import logging
     8  import socket
     9  
    10  logger = logging.getLogger("testframework")
    11  
    12  def assert_equal(thing1, thing2, *args):
    13      if thing1 != thing2 or any(thing1 != arg for arg in args):
    14          raise AssertionError("not(%s)" % " == ".join(str(arg) for arg in (thing1, thing2) + args))
    15  
    16  def wait_until(predicate, *, attempts=120, dt=0.25, errmsg=None): # up to 30 seconds
    17      attempt = 0
    18  
    19      while attempt < attempts:
    20          if predicate():
    21              return True
    22          attempt += 1
    23          time.sleep(dt)
    24  
    25      if errmsg is not None:
    26          if errmsg == False:
    27              return False
    28          else:
    29              raise AssertionError(str(errmsg))
    30      else:
    31          raise AssertionError("wait_until() timed out")
    32  
    33  def check_port_open(host, port, timeout=0.05):
    34      sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    35      sock.settimeout(timeout)
    36      res = sock.connect_ex((host, port))
    37      if res == 0:
    38          sock.close()
    39          return True
    40      else:
    41          return False
    42  
    43  def wait_until_port(host, port, errmsg=None):
    44      def p():
    45          return check_port_open(host, port)
    46      return wait_until(p, errmsg=errmsg)