forked from bitcoin-core/gui-qml
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathqml_test_shutdown.py
More file actions
executable file
·178 lines (141 loc) · 5.4 KB
/
Copy pathqml_test_shutdown.py
File metadata and controls
executable file
·178 lines (141 loc) · 5.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#!/usr/bin/env python3
# Copyright (c) 2026 The Bitcoin Core developers
# Distributed under the MIT software license, see the accompanying
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
"""Test QML shutdown while load_on_startup wallets are loading."""
import os
import signal
import subprocess
import sys
import time
from qml_test_harness import qsettings_sandbox_args
from qml_wallet_test_lib import WalletFlowHarness, find_bitcoind, rpc_call, wait_for_rpc
STARTUP_WALLET_COUNT = 8
SHUTDOWN_TIMEOUT_SECS = 30
def start_node(datadir):
process = subprocess.Popen(
[find_bitcoind(), f"-datadir={datadir}"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
return process
def stop_node(process, rpc_port):
if process.poll() is not None:
return
try:
rpc_call(rpc_port, "stop")
except Exception:
process.send_signal(signal.SIGTERM)
try:
process.wait(timeout=SHUTDOWN_TIMEOUT_SECS)
except subprocess.TimeoutExpired:
process.kill()
process.wait()
def create_load_on_startup_wallets(harness, prefix):
process = start_node(harness.gui_datadir)
try:
wait_for_rpc(harness.gui_rpc_port)
for index in range(STARTUP_WALLET_COUNT):
rpc_call(
harness.gui_rpc_port,
"createwallet",
{
"wallet_name": f"{prefix}_{index}",
"load_on_startup": True,
},
)
finally:
stop_node(process, harness.gui_rpc_port)
def wait_for_process_exit(process, description):
deadline = time.time() + SHUTDOWN_TIMEOUT_SECS
while time.time() < deadline:
if process.poll() is not None:
return process.returncode
time.sleep(0.25)
raise TimeoutError(f"GUI did not exit after {description}")
def debug_log_path(harness):
return os.path.join(harness.gui_datadir, "regtest", "debug.log")
def read_debug_log(harness):
path = debug_log_path(harness)
if not os.path.isfile(path):
return ""
with open(path, "r", encoding="utf8", errors="replace") as log_file:
return log_file.read()
def wait_for_startup_initialization(harness, process, baseline_wallet_loads_completed):
deadline = time.time() + SHUTDOWN_TIMEOUT_SECS
initialization_seen_at = None
while time.time() < deadline:
if process.poll() is not None:
raise RuntimeError(
f"GUI exited before startup could be interrupted, return code {process.returncode}"
)
debug_log = read_debug_log(harness)
wallet_loads_completed = debug_log.count("Wallet completed loading")
if (
baseline_wallet_loads_completed
< wallet_loads_completed
< baseline_wallet_loads_completed + STARTUP_WALLET_COUNT
):
return
if "Running initialization in thread" in debug_log:
initialization_seen_at = initialization_seen_at or time.time()
if time.time() - initialization_seen_at >= 0.2:
return
time.sleep(0.05)
raise TimeoutError("Timed out waiting for GUI startup initialization")
def start_gui_without_driver(harness):
env = dict(os.environ)
env["QT_QPA_PLATFORM"] = "offscreen"
settings_args = qsettings_sandbox_args(env, harness.config_home)
args = [
harness.gui_binary,
f"-datadir={harness.gui_datadir}",
f"-test-automation={harness.socket_path}",
] + settings_args + [
"-qml_onboarded=1",
"-logtimemicros",
"-debug",
"-debugexclude=leveldb",
"-nolisten",
]
harness.gui_process = subprocess.Popen(
args,
env=env,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
def case_close_window_during_load_on_startup():
harness = WalletFlowHarness("qml_shutdown_close", 970)
try:
create_load_on_startup_wallets(harness, "shutdown_close")
harness.start_gui()
gui = harness.driver
gui.close_window()
gui.wait_for_page("shutdownPage", timeout_ms=5000)
return_code = wait_for_process_exit(harness.gui_process, "closing during load_on_startup")
assert return_code == 0, f"Expected GUI exit code 0, got {return_code}"
finally:
harness.stop()
def case_sigint_during_load_on_startup():
harness = WalletFlowHarness("qml_shutdown_sigint", 980)
try:
create_load_on_startup_wallets(harness, "shutdown_sigint")
baseline_wallet_loads_completed = read_debug_log(harness).count("Wallet completed loading")
start_gui_without_driver(harness)
wait_for_startup_initialization(harness, harness.gui_process, baseline_wallet_loads_completed)
harness.gui_process.send_signal(signal.SIGINT)
return_code = wait_for_process_exit(harness.gui_process, "SIGINT during load_on_startup")
assert return_code == 0, f"Expected GUI exit code 0 after SIGINT, got {return_code}"
finally:
harness.stop()
def run_tests():
try:
case_close_window_during_load_on_startup()
print("Close during load_on_startup shutdown completed successfully.")
case_sigint_during_load_on_startup()
print("SIGINT during load_on_startup shutdown completed successfully.")
except Exception as err:
print(f"\nFAILED: {err}", file=sys.stderr)
raise
if __name__ == "__main__":
run_tests()