Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_ffpuppet_26(tmp_path):
"""test multiprocess target"""
prefs = (tmp_path / "prefs.js")
prefs.write_bytes(b"//fftest_multi_proc\n")
with FFPuppet() as ffp:
with HTTPTestServer() as srv:
ffp.launch(TESTFF_BIN, prefs_js=str(prefs), location=srv.get_addr())
assert ffp.is_running()
assert not ffp.wait(timeout=0)
c_procs = Process(ffp.get_pid()).children()
assert c_procs
# terminate one of the child processes
c_procs[-1].terminate()
assert ffp.is_running()
ffp.close()
assert not ffp.is_running()
assert ffp.wait(timeout=0)
def test_ffpuppet_26(tmp_path):
"""test multiprocess target"""
prefs = (tmp_path / "prefs.js")
prefs.write_bytes(b"//fftest_multi_proc\n")
with FFPuppet() as ffp:
with HTTPTestServer() as srv:
ffp.launch(TESTFF_BIN, prefs_js=str(prefs), location=srv.get_addr())
assert ffp.is_running()
assert not ffp.wait(timeout=0)
c_procs = Process(ffp.get_pid()).children()
assert c_procs
# terminate one of the child processes
c_procs[-1].terminate()
assert ffp.is_running()
ffp.close()
assert not ffp.is_running()
assert ffp.wait(timeout=0)
def test_ffpuppet_01():
"""test basic launch and close"""
with FFPuppet() as ffp:
assert ffp.launches == 0
assert ffp.reason == ffp.RC_CLOSED
with HTTPTestServer() as srv:
ffp.launch(TESTFF_BIN, location=srv.get_addr())
assert not ffp._checks
assert ffp.launches == 1
assert not ffp.wait(timeout=0)
assert ffp.is_running()
assert ffp.is_healthy()
assert ffp.reason is None
ffp.close()
assert ffp.reason == ffp.RC_CLOSED
assert ffp._proc is None
assert not ffp.is_running()
assert not ffp.is_healthy()
assert ffp.wait(timeout=10)
def test_ffpuppet_33():
"""test cpu_usage()"""
with FFPuppet() as ffp:
assert not any(ffp.cpu_usage())
with HTTPTestServer() as srv:
ffp.launch(TESTFF_BIN, location=srv.get_addr())
usage = tuple(ffp.cpu_usage())
assert len(usage) == 1
assert len(usage[0]) == 2
assert usage[0][0] == ffp.get_pid()
assert usage[0][1] <= 100
assert usage[0][1] >= 0
ffp.close()
assert ffp.wait(timeout=10)
def test_ffpuppet_08(tmp_path):
"""test clone_log()"""
logs = (tmp_path / "logs.txt")
with FFPuppet() as ffp:
assert ffp.clone_log("stdout", target_file=str(logs)) is None
with HTTPTestServer() as srv:
ffp.launch(TESTFF_BIN, location=srv.get_addr())
ffp.wait(timeout=0.25) # wait for log prints
# make sure logs are available
assert ffp.clone_log("stdout", target_file=str(logs)) == str(logs)
orig = logs.read_text()
assert len(orig) > 5
assert ffp.clone_log("stdout", target_file=str(logs), offset=5) == str(logs)
assert logs.read_text() == orig[5:]
# grab log without giving a target file name
rnd_log = ffp.clone_log("stdout")
assert rnd_log is not None
try:
ffp.close()
# make sure logs are available
assert ffp.clone_log("stdout", target_file=str(logs)) == str(logs)
assert logs.read_text().startswith(orig)
def test_bootstrapper_05(mocker):
"""test Bootstrapper.wait() target crashed"""
fake_sock = mocker.Mock(socket.socket)
fake_conn = mocker.Mock(socket.socket)
# return empty buffer for test coverage
fake_conn.recv.return_value = ""
fake_sock.accept.return_value = (fake_conn, None)
mocker.patch("ffpuppet.bootstrapper.socket.socket", return_value=fake_sock)
with Bootstrapper() as bts:
with pytest.raises(BrowserTerminatedError, match="Failure during browser startup"):
bts.wait(lambda: False)
assert fake_conn.close.call_count == 1
def test_bootstrapper_03(mocker):
"""test Bootstrapper.wait() failure waiting for request"""
fake_sock = mocker.Mock(socket.socket)
fake_conn = mocker.Mock(socket.socket)
fake_conn.recv.side_effect = socket.timeout
fake_sock.accept.return_value = (fake_conn, None)
mocker.patch("ffpuppet.bootstrapper.socket.socket", return_value=fake_sock)
with Bootstrapper() as bts:
# test failure
with pytest.raises(BrowserTerminatedError, match="Failure waiting for request"):
bts.wait(lambda: False)
assert fake_conn.recv.call_count == 1
assert fake_conn.close.call_count == 1
fake_conn.reset_mock()
# test timeout
mocker.patch("ffpuppet.bootstrapper.time.time", side_effect=(1, 1, 1, 1, 2))
with pytest.raises(BrowserTimeoutError, match="Timeout waiting for request"):
bts.wait(lambda: True, timeout=0.1)
# should call recv() at least 2x for positive and negative timeout check
assert fake_conn.recv.call_count > 1
assert fake_conn.close.call_count == 1
def test_bootstrapper_07(mocker):
"""test Bootstrapper.wait() successful with redirect"""
fake_sock = mocker.Mock(socket.socket)
fake_conn = mocker.Mock(socket.socket)
fake_conn.recv.return_value = "AAAA"
fake_sock.accept.return_value = (fake_conn, None)
mocker.patch("ffpuppet.bootstrapper.socket.socket", return_value=fake_sock)
# without redirect
with Bootstrapper() as bts:
bts.wait(lambda: True, url="http://127.0.0.1:9999/test.html")
assert fake_conn.close.call_count == 1
assert fake_conn.recv.call_count == 1
assert fake_conn.sendall.call_count == 1
def test_bootstrapper_04(mocker):
"""test Bootstrapper.wait() failure sending response"""
fake_sock = mocker.Mock(socket.socket)
fake_conn = mocker.Mock(socket.socket)
fake_conn.recv.return_value = "A"
fake_conn.sendall.side_effect = socket.timeout
fake_sock.accept.return_value = (fake_conn, None)
mocker.patch("ffpuppet.bootstrapper.socket.socket", return_value=fake_sock)
with Bootstrapper() as bts:
# test timeout
with pytest.raises(BrowserTimeoutError, match="Timeout sending response"):
bts.wait(lambda: True)
assert fake_conn.recv.call_count == 1
assert fake_conn.sendall.call_count == 1
assert fake_conn.close.call_count == 1
fake_conn.reset_mock()
# test failure
with pytest.raises(BrowserTerminatedError, match="Failure during browser startup"):
bts.wait(lambda: False)
assert fake_conn.recv.call_count == 1
assert fake_conn.sendall.call_count == 1
assert fake_conn.close.call_count == 1
try:
conn.connect(("127.0.0.1", port))
break
except socket.timeout:
if not attempt:
raise
# send request and receive response
try:
conn.settimeout(10)
conn.sendall(b"A" * payload_size)
conn.send(b"")
conn.recv(8192)
finally:
conn.close()
with Bootstrapper() as bts:
browser_thread = threading.Thread(target=_fake_browser, args=(bts.port,))
try:
browser_thread.start()
bts.wait(lambda: True, timeout=10)
finally:
browser_thread.join()