From 6f870dfe399461fc556da4a8dea29eeef62b91ad Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Tue, 6 Oct 2020 20:15:35 +0200 Subject: [PATCH] pytest: Don't give up on the first psql connection error Since we start a new instance of postgres for each test we may end up swamped and the startup can take a bit longer. So let's loop until we get a success. --- contrib/pyln-testing/pyln/testing/db.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/contrib/pyln-testing/pyln/testing/db.py b/contrib/pyln-testing/pyln/testing/db.py index 7024b5eeb..b7bb805f7 100644 --- a/contrib/pyln-testing/pyln/testing/db.py +++ b/contrib/pyln-testing/pyln/testing/db.py @@ -177,9 +177,14 @@ class PostgresDbProvider(object): '-F', '-i', ]) - # Hacky but seems to work ok (might want to make the postgres proc a TailableProc as well if too flaky). - time.sleep(1) - self.conn = psycopg2.connect("dbname=template1 user=postgres host=localhost port={}".format(self.port)) + # Hacky but seems to work ok (might want to make the postgres proc a + # TailableProc as well if too flaky). + for i in range(30): + try: + self.conn = psycopg2.connect("dbname=template1 user=postgres host=localhost port={}".format(self.port)) + break + except Exception: + time.sleep(0.5) # Required for CREATE DATABASE to work self.conn.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT)