-
Notifications
You must be signed in to change notification settings - Fork 207
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
68 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 56 additions & 29 deletions
85
counterparty-core/counterpartycore/test/integrations/shutdown_test.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,76 @@ | ||
import os | ||
import random | ||
import signal | ||
import socket | ||
import time | ||
from io import StringIO | ||
|
||
import sh | ||
from counterpartycore.lib.cli import server | ||
from counterpartycore.lib.cli.main import arg_parser | ||
from counterpartycore.test.integrations import reparsetest | ||
from http2https import PROXY_PORT, start_http_proxy, stop_http_proxy | ||
|
||
|
||
def is_port_in_used(port): | ||
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | ||
try: | ||
s.bind(("127.0.0.1", port)) | ||
return False | ||
except socket.error: | ||
return True | ||
finally: | ||
s.close() | ||
|
||
|
||
def test_shutdown(): | ||
sh_counterparty_server, backend_url, db_file, api_url = reparsetest.prepare("mainnet") | ||
sh_counterparty_server, backend_url, db_file, api_url = reparsetest.prepare("testnet4") | ||
|
||
out = StringIO() | ||
try: | ||
start_http_proxy(backend_url) | ||
server_process = sh_counterparty_server( | ||
"start", | ||
"--backend-connect", | ||
"127.0.0.1", | ||
"--backend-port", | ||
PROXY_PORT, | ||
"--wsgi-server", | ||
"gunicorn", | ||
_bg=True, | ||
_out=out, | ||
_err_to_out=True, | ||
_bg_exc=False, | ||
|
||
parser = arg_parser(no_config_file=True) | ||
args = parser.parse_args( | ||
[ | ||
"--testnet4", | ||
"--data-dir", | ||
reparsetest.DATA_DIR, | ||
"--cache-dir", | ||
reparsetest.DATA_DIR, | ||
"start", | ||
"--backend-connect", | ||
"127.0.0.1", | ||
"--backend-port", | ||
f"{PROXY_PORT}", | ||
"--wsgi-server", | ||
"gunicorn", | ||
] | ||
) | ||
|
||
duration = random.randint(1, 60) # noqa S311 | ||
log_stream = StringIO() | ||
server.initialise_log_and_config(args, log_stream=log_stream) | ||
|
||
test_duration = random.randint(1, 60) # noqa S311 | ||
start_time = time.time() | ||
|
||
print(f"Waiting random time: {duration}s") | ||
while time.time() - start_time < duration: | ||
time.sleep(1) | ||
print("Test duration: ", test_duration) | ||
|
||
print("Shutting down server...") | ||
# server_process.terminate() | ||
os.kill(server_process.pid, signal.SIGTERM) | ||
counterparty_server = server.CounterpartyServer(args, log_stream) | ||
counterparty_server.start() | ||
while time.time() - start_time < test_duration: | ||
counterparty_server.join(1) | ||
|
||
assert is_port_in_used(44000) | ||
|
||
except sh.SignalException_SIGTERM: | ||
pass | ||
finally: | ||
print("Waiting for server to shutdown...") | ||
time.sleep(30) | ||
print("Shutting down server...") | ||
counterparty_server.stop() | ||
stop_http_proxy() | ||
print(out.getvalue()) | ||
|
||
logs = log_stream.getvalue() | ||
|
||
assert "Ledger.Main - Shutting down..." in logs | ||
assert "Ledger.Main - Asset Conservation Checker thread stopped." in logs | ||
assert "Ledger.BackendHeight - BackendHeight Thread stopped." in logs | ||
assert "Ledger.Main - API Server v1 thread stopped." in logs | ||
assert "Ledger.Main - API Server process stopped." in logs | ||
assert "Ledger.Main - Shutdown complete." in logs | ||
|
||
assert not is_port_in_used(44000) |