diff --git a/e2e/fixtures/photon/server.cjs b/e2e/fixtures/photon/server.cjs index 71e4c0d2e..9952fbf24 100644 --- a/e2e/fixtures/photon/server.cjs +++ b/e2e/fixtures/photon/server.cjs @@ -173,6 +173,14 @@ server.addService(defs.MessageService.service, { }); }, }); +// Both endpoints go out on one stdout line once both listen: the test waits +// on that line, which also ends at once if this process dies first. +const bound = {}; +function announce() { + if (bound.address && bound.cloud) { + process.stdout.write(JSON.stringify(bound) + "\n"); + } +} const key = fs.readFileSync(path.join(__dirname, "localhost.key")); const cert = fs.readFileSync(path.join(__dirname, "localhost.pem")); server.bindAsync( @@ -182,7 +190,8 @@ server.bindAsync( ]), (err, port) => { if (err) throw err; - fs.writeFileSync(path.join(state, "address"), "127.0.0.1:" + port); + bound.address = "127.0.0.1:" + port; + announce(); }, ); @@ -234,8 +243,6 @@ const cloud = http.createServer(async (request, response) => { .end(JSON.stringify({ succeed: true, data })); }); cloud.listen(0, "127.0.0.1", () => { - fs.writeFileSync( - path.join(state, "cloud"), - `http://127.0.0.1:${cloud.address().port}`, - ); + bound.cloud = `http://127.0.0.1:${cloud.address().port}`; + announce(); }); diff --git a/e2e/photon_test.py b/e2e/photon_test.py index 94a2ecd46..eca5067a2 100644 --- a/e2e/photon_test.py +++ b/e2e/photon_test.py @@ -3,6 +3,7 @@ import base64 import json from pathlib import Path +import selectors import shutil import subprocess import threading @@ -51,20 +52,31 @@ class PhotonTests(unittest.TestCase): helper = shutil.which("albedo-photon") assert helper is not None, "the development shell must provide albedo-photon" server = subprocess.Popen( - ["node", FIXTURE / "server.cjs", Path(helper).resolve(), app.root] + ["node", FIXTURE / "server.cjs", Path(helper).resolve(), app.root], + stdout=subprocess.PIPE, + text=True, ) + output = server.stdout + assert output is not None + self.addCleanup(output.close) self.addCleanup(server.wait) self.addCleanup(server.terminate) - address = app.root / "address" - cloud = app.root / "cloud" - wait_until( - lambda: address.exists() and cloud.exists(), 10, "fixture failed to bind" - ) + # The fixture prints both endpoints once both listen. A wait on the + # pipe sees a line written before the deadline even when this thread + # runs again only after it, and ends at once if the fixture dies. + with selectors.DefaultSelector() as pipe: + pipe.register(output, selectors.EVENT_READ) + if not pipe.select(10): + raise AssertionError("fixture did not bind in 10 s") + line = output.readline() + if not line: + raise AssertionError(f"fixture exited with {server.wait()} before binding") + bound = json.loads(line) app.env.update( SSL_CERT_FILE=str(FIXTURE / "localhost.pem"), NIX_SSL_CERT_FILE=str(FIXTURE / "localhost.pem"), ) - return address.read_text(), cloud.read_text() + return bound["address"], bound["cloud"] def test_text_and_resume(self): provider = Provider(self.respond)