2022-09-10 18:51:29 +00:00
|
|
|
"""
|
|
|
|
Unit test file for rpc/external_message_consumer.py
|
|
|
|
"""
|
2022-09-11 05:57:17 +00:00
|
|
|
import asyncio
|
|
|
|
import functools
|
2022-09-10 19:44:27 +00:00
|
|
|
import logging
|
|
|
|
from datetime import datetime, timezone
|
2022-09-11 05:57:17 +00:00
|
|
|
from unittest.mock import MagicMock
|
2022-09-10 19:44:27 +00:00
|
|
|
|
2022-09-10 18:51:29 +00:00
|
|
|
import pytest
|
2022-09-11 05:57:17 +00:00
|
|
|
import websockets
|
2022-09-10 18:51:29 +00:00
|
|
|
|
|
|
|
from freqtrade.data.dataprovider import DataProvider
|
2022-09-12 17:50:22 +00:00
|
|
|
from freqtrade.exceptions import OperationalException
|
2022-09-10 18:51:29 +00:00
|
|
|
from freqtrade.rpc.external_message_consumer import ExternalMessageConsumer
|
2022-09-10 19:44:27 +00:00
|
|
|
from tests.conftest import log_has, log_has_re, log_has_when
|
2022-09-10 18:51:29 +00:00
|
|
|
|
|
|
|
|
2022-09-11 05:57:17 +00:00
|
|
|
_TEST_WS_TOKEN = "secret_Ws_t0ken"
|
|
|
|
_TEST_WS_HOST = "localhost"
|
|
|
|
_TEST_WS_PORT = 9989
|
|
|
|
|
|
|
|
|
2022-09-10 19:50:36 +00:00
|
|
|
@pytest.fixture
|
2022-09-10 18:51:29 +00:00
|
|
|
def patched_emc(default_conf, mocker):
|
|
|
|
default_conf.update({
|
|
|
|
"external_message_consumer": {
|
|
|
|
"enabled": True,
|
|
|
|
"producers": [
|
|
|
|
{
|
|
|
|
"name": "default",
|
2022-09-12 20:12:39 +00:00
|
|
|
"url": "ws://null:9891/api/v1/message/ws",
|
2022-09-11 05:57:17 +00:00
|
|
|
"ws_token": _TEST_WS_TOKEN
|
2022-09-10 18:51:29 +00:00
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
})
|
|
|
|
dataprovider = DataProvider(default_conf, None, None, None)
|
|
|
|
emc = ExternalMessageConsumer(default_conf, dataprovider)
|
|
|
|
|
|
|
|
try:
|
|
|
|
yield emc
|
|
|
|
finally:
|
|
|
|
emc.shutdown()
|
|
|
|
|
|
|
|
|
|
|
|
def test_emc_start(patched_emc, caplog):
|
|
|
|
# Test if the message was printed
|
|
|
|
assert log_has_when("Starting ExternalMessageConsumer", caplog, "setup")
|
|
|
|
# Test if the thread and loop objects were created
|
|
|
|
assert patched_emc._thread and patched_emc._loop
|
|
|
|
|
|
|
|
# Test we call start again nothing happens
|
|
|
|
prev_thread = patched_emc._thread
|
|
|
|
patched_emc.start()
|
|
|
|
assert prev_thread == patched_emc._thread
|
|
|
|
|
|
|
|
|
|
|
|
def test_emc_shutdown(patched_emc, caplog):
|
|
|
|
patched_emc.shutdown()
|
|
|
|
|
|
|
|
assert log_has("Stopping ExternalMessageConsumer", caplog)
|
|
|
|
# Test the loop has stopped
|
|
|
|
assert patched_emc._loop is None
|
|
|
|
# Test if the thread has stopped
|
|
|
|
assert patched_emc._thread is None
|
|
|
|
|
|
|
|
caplog.clear()
|
|
|
|
patched_emc.shutdown()
|
|
|
|
|
|
|
|
# Test func didn't run again as it was called once already
|
|
|
|
assert not log_has("Stopping ExternalMessageConsumer", caplog)
|
|
|
|
|
|
|
|
|
2022-09-12 17:50:22 +00:00
|
|
|
def test_emc_init(patched_emc, default_conf):
|
2022-09-10 18:51:29 +00:00
|
|
|
# Test the settings were set correctly
|
|
|
|
assert patched_emc.initial_candle_limit <= 1500
|
|
|
|
assert patched_emc.wait_timeout > 0
|
|
|
|
assert patched_emc.sleep_time > 0
|
|
|
|
|
|
|
|
default_conf.update({
|
|
|
|
"external_message_consumer": {
|
|
|
|
"enabled": True,
|
|
|
|
"producers": []
|
|
|
|
}
|
|
|
|
})
|
|
|
|
dataprovider = DataProvider(default_conf, None, None, None)
|
2022-09-12 17:50:22 +00:00
|
|
|
with pytest.raises(OperationalException,
|
|
|
|
match="You must specify at least 1 Producer to connect to."):
|
2022-09-10 18:51:29 +00:00
|
|
|
ExternalMessageConsumer(default_conf, dataprovider)
|
|
|
|
|
2022-09-10 19:44:27 +00:00
|
|
|
|
2022-09-10 20:29:15 +00:00
|
|
|
# Parametrize this?
|
2022-09-10 19:44:27 +00:00
|
|
|
def test_emc_handle_producer_message(patched_emc, caplog, ohlcv_history):
|
|
|
|
test_producer = {"name": "test", "url": "ws://test", "ws_token": "test"}
|
2022-09-10 20:29:15 +00:00
|
|
|
producer_name = test_producer['name']
|
|
|
|
|
2022-09-10 19:44:27 +00:00
|
|
|
caplog.set_level(logging.DEBUG)
|
|
|
|
|
|
|
|
# Test handle whitelist message
|
|
|
|
whitelist_message = {"type": "whitelist", "data": ["BTC/USDT"]}
|
|
|
|
patched_emc.handle_producer_message(test_producer, whitelist_message)
|
|
|
|
|
2022-09-10 20:29:15 +00:00
|
|
|
assert log_has(f"Received message of type `whitelist` from `{producer_name}`", caplog)
|
|
|
|
assert log_has(
|
|
|
|
f"Consumed message from `{producer_name}` of type `RPCMessageType.WHITELIST`", caplog)
|
2022-09-10 19:44:27 +00:00
|
|
|
|
|
|
|
# Test handle analyzed_df message
|
|
|
|
df_message = {
|
|
|
|
"type": "analyzed_df",
|
|
|
|
"data": {
|
|
|
|
"key": ("BTC/USDT", "5m", "spot"),
|
|
|
|
"df": ohlcv_history,
|
|
|
|
"la": datetime.now(timezone.utc)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
patched_emc.handle_producer_message(test_producer, df_message)
|
|
|
|
|
2022-09-10 20:29:15 +00:00
|
|
|
assert log_has(f"Received message of type `analyzed_df` from `{producer_name}`", caplog)
|
|
|
|
assert log_has(
|
|
|
|
f"Consumed message from `{producer_name}` of type `RPCMessageType.ANALYZED_DF`", caplog)
|
2022-09-10 19:44:27 +00:00
|
|
|
|
|
|
|
# Test unhandled message
|
|
|
|
unhandled_message = {"type": "status", "data": "RUNNING"}
|
|
|
|
patched_emc.handle_producer_message(test_producer, unhandled_message)
|
|
|
|
|
|
|
|
assert log_has_re(r"Received unhandled message\: .*", caplog)
|
|
|
|
|
2022-09-10 20:29:15 +00:00
|
|
|
# Test malformed messages
|
2022-09-10 19:44:27 +00:00
|
|
|
caplog.clear()
|
|
|
|
malformed_message = {"type": "whitelist", "data": {"pair": "BTC/USDT"}}
|
|
|
|
patched_emc.handle_producer_message(test_producer, malformed_message)
|
|
|
|
|
2022-09-10 20:29:15 +00:00
|
|
|
assert log_has_re(r"Invalid message .+", caplog)
|
2022-09-10 19:44:27 +00:00
|
|
|
|
|
|
|
malformed_message = {
|
|
|
|
"type": "analyzed_df",
|
|
|
|
"data": {
|
|
|
|
"key": "BTC/USDT",
|
|
|
|
"df": ohlcv_history,
|
|
|
|
"la": datetime.now(timezone.utc)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
patched_emc.handle_producer_message(test_producer, malformed_message)
|
|
|
|
|
2022-09-10 20:29:15 +00:00
|
|
|
assert log_has(f"Received message of type `analyzed_df` from `{producer_name}`", caplog)
|
|
|
|
assert log_has_re(r"Invalid message .+", caplog)
|
|
|
|
|
|
|
|
caplog.clear()
|
|
|
|
malformed_message = {"some": "stuff"}
|
|
|
|
patched_emc.handle_producer_message(test_producer, malformed_message)
|
|
|
|
|
|
|
|
assert log_has_re(r"Invalid message .+", caplog)
|
|
|
|
|
|
|
|
caplog.clear()
|
|
|
|
malformed_message = {"type": "whitelist", "data": None}
|
|
|
|
patched_emc.handle_producer_message(test_producer, malformed_message)
|
|
|
|
|
2022-09-12 20:12:39 +00:00
|
|
|
assert log_has_re(r"Empty message .+", caplog)
|
2022-09-11 05:57:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_emc_create_connection_success(default_conf, caplog, mocker):
|
|
|
|
default_conf.update({
|
|
|
|
"external_message_consumer": {
|
|
|
|
"enabled": True,
|
|
|
|
"producers": [
|
|
|
|
{
|
|
|
|
"name": "default",
|
|
|
|
"url": f"ws://{_TEST_WS_HOST}:{_TEST_WS_PORT}/api/v1/message/ws",
|
|
|
|
"ws_token": _TEST_WS_TOKEN
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"wait_timeout": 60,
|
|
|
|
"ping_timeout": 60,
|
|
|
|
"sleep_timeout": 60
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
mocker.patch('freqtrade.rpc.external_message_consumer.ExternalMessageConsumer.start',
|
|
|
|
MagicMock())
|
|
|
|
dp = DataProvider(default_conf, None, None, None)
|
|
|
|
emc = ExternalMessageConsumer(default_conf, dp)
|
|
|
|
|
|
|
|
test_producer = default_conf['external_message_consumer']['producers'][0]
|
|
|
|
lock = asyncio.Lock()
|
|
|
|
|
|
|
|
async def eat(websocket):
|
|
|
|
pass
|
|
|
|
|
|
|
|
try:
|
|
|
|
async with websockets.serve(eat, _TEST_WS_HOST, _TEST_WS_PORT):
|
|
|
|
emc._running = True
|
|
|
|
await emc._create_connection(test_producer, lock)
|
|
|
|
emc._running = False
|
|
|
|
|
|
|
|
assert log_has_re(r"Producer connection success.+", caplog)
|
|
|
|
finally:
|
|
|
|
emc.shutdown()
|
|
|
|
|
|
|
|
|
|
|
|
async def test_emc_create_connection_invalid(default_conf, caplog, mocker):
|
|
|
|
default_conf.update({
|
|
|
|
"external_message_consumer": {
|
|
|
|
"enabled": True,
|
|
|
|
"producers": [
|
|
|
|
{
|
|
|
|
"name": "default",
|
|
|
|
"url": "ws://localhost:8080/api/v1/message/ws",
|
|
|
|
"ws_token": _TEST_WS_TOKEN
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"wait_timeout": 60,
|
|
|
|
"ping_timeout": 60,
|
|
|
|
"sleep_timeout": 60
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
mocker.patch('freqtrade.rpc.external_message_consumer.ExternalMessageConsumer.start',
|
|
|
|
MagicMock())
|
|
|
|
|
2022-09-11 06:50:18 +00:00
|
|
|
test_producer = default_conf['external_message_consumer']['producers'][0]
|
2022-09-11 05:57:17 +00:00
|
|
|
lock = asyncio.Lock()
|
2022-09-11 06:50:18 +00:00
|
|
|
|
2022-09-11 05:57:17 +00:00
|
|
|
dp = DataProvider(default_conf, None, None, None)
|
|
|
|
emc = ExternalMessageConsumer(default_conf, dp)
|
|
|
|
|
|
|
|
try:
|
|
|
|
# Test invalid URL
|
|
|
|
test_producer['url'] = "tcp://localhost:8080/api/v1/message/ws"
|
|
|
|
emc._running = True
|
|
|
|
await emc._create_connection(test_producer, lock)
|
|
|
|
emc._running = False
|
|
|
|
|
|
|
|
assert log_has_re(r".+is an invalid WebSocket URL.+", caplog)
|
|
|
|
finally:
|
|
|
|
emc.shutdown()
|
|
|
|
|
|
|
|
|
|
|
|
async def test_emc_create_connection_error(default_conf, caplog, mocker):
|
|
|
|
default_conf.update({
|
|
|
|
"external_message_consumer": {
|
|
|
|
"enabled": True,
|
|
|
|
"producers": [
|
|
|
|
{
|
|
|
|
"name": "default",
|
|
|
|
"url": "ws://localhost:8080/api/v1/message/ws",
|
|
|
|
"ws_token": _TEST_WS_TOKEN
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"wait_timeout": 60,
|
|
|
|
"ping_timeout": 60,
|
|
|
|
"sleep_timeout": 60
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
# Test unexpected error
|
|
|
|
mocker.patch('websockets.connect', side_effect=RuntimeError)
|
|
|
|
|
|
|
|
dp = DataProvider(default_conf, None, None, None)
|
|
|
|
emc = ExternalMessageConsumer(default_conf, dp)
|
|
|
|
|
|
|
|
try:
|
|
|
|
await asyncio.sleep(1)
|
|
|
|
assert log_has("Unexpected error has occurred:", caplog)
|
|
|
|
finally:
|
|
|
|
emc.shutdown()
|
|
|
|
|
|
|
|
|
2022-09-11 06:50:18 +00:00
|
|
|
async def test_emc_receive_messages_valid(default_conf, caplog, mocker):
|
|
|
|
default_conf.update({
|
|
|
|
"external_message_consumer": {
|
|
|
|
"enabled": True,
|
|
|
|
"producers": [
|
|
|
|
{
|
|
|
|
"name": "default",
|
|
|
|
"url": f"ws://{_TEST_WS_HOST}:{_TEST_WS_PORT}/api/v1/message/ws",
|
|
|
|
"ws_token": _TEST_WS_TOKEN
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"wait_timeout": 1,
|
|
|
|
"ping_timeout": 60,
|
|
|
|
"sleep_time": 60
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
mocker.patch('freqtrade.rpc.external_message_consumer.ExternalMessageConsumer.start',
|
|
|
|
MagicMock())
|
|
|
|
|
|
|
|
lock = asyncio.Lock()
|
|
|
|
test_producer = default_conf['external_message_consumer']['producers'][0]
|
|
|
|
|
|
|
|
dp = DataProvider(default_conf, None, None, None)
|
|
|
|
emc = ExternalMessageConsumer(default_conf, dp)
|
|
|
|
|
|
|
|
loop = asyncio.get_event_loop()
|
|
|
|
def change_running(emc): emc._running = not emc._running
|
2022-09-11 05:57:17 +00:00
|
|
|
|
2022-09-11 06:50:18 +00:00
|
|
|
class TestChannel:
|
|
|
|
async def recv(self, *args, **kwargs):
|
|
|
|
return {"type": "whitelist", "data": ["BTC/USDT"]}
|
|
|
|
|
|
|
|
async def ping(self, *args, **kwargs):
|
|
|
|
return asyncio.Future()
|
|
|
|
|
|
|
|
try:
|
|
|
|
change_running(emc)
|
|
|
|
loop.call_soon(functools.partial(change_running, emc=emc))
|
|
|
|
await emc._receive_messages(TestChannel(), test_producer, lock)
|
|
|
|
|
|
|
|
assert log_has_re(r"Received message of type `whitelist`.+", caplog)
|
|
|
|
finally:
|
|
|
|
emc.shutdown()
|
|
|
|
|
|
|
|
|
|
|
|
async def test_emc_receive_messages_invalid(default_conf, caplog, mocker):
|
2022-09-11 05:57:17 +00:00
|
|
|
default_conf.update({
|
|
|
|
"external_message_consumer": {
|
|
|
|
"enabled": True,
|
|
|
|
"producers": [
|
|
|
|
{
|
|
|
|
"name": "default",
|
|
|
|
"url": f"ws://{_TEST_WS_HOST}:{_TEST_WS_PORT}/api/v1/message/ws",
|
|
|
|
"ws_token": _TEST_WS_TOKEN
|
|
|
|
}
|
|
|
|
],
|
2022-09-11 06:50:18 +00:00
|
|
|
"wait_timeout": 1,
|
2022-09-11 05:57:17 +00:00
|
|
|
"ping_timeout": 60,
|
2022-09-11 06:50:18 +00:00
|
|
|
"sleep_time": 60
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
mocker.patch('freqtrade.rpc.external_message_consumer.ExternalMessageConsumer.start',
|
|
|
|
MagicMock())
|
|
|
|
|
|
|
|
lock = asyncio.Lock()
|
|
|
|
test_producer = default_conf['external_message_consumer']['producers'][0]
|
|
|
|
|
|
|
|
dp = DataProvider(default_conf, None, None, None)
|
|
|
|
emc = ExternalMessageConsumer(default_conf, dp)
|
|
|
|
|
|
|
|
loop = asyncio.get_event_loop()
|
|
|
|
def change_running(emc): emc._running = not emc._running
|
|
|
|
|
|
|
|
class TestChannel:
|
|
|
|
async def recv(self, *args, **kwargs):
|
|
|
|
return {"type": ["BTC/USDT"]}
|
|
|
|
|
|
|
|
async def ping(self, *args, **kwargs):
|
|
|
|
return asyncio.Future()
|
|
|
|
|
|
|
|
try:
|
|
|
|
change_running(emc)
|
|
|
|
loop.call_soon(functools.partial(change_running, emc=emc))
|
|
|
|
await emc._receive_messages(TestChannel(), test_producer, lock)
|
|
|
|
|
|
|
|
assert log_has_re(r"Invalid message from.+", caplog)
|
|
|
|
finally:
|
|
|
|
emc.shutdown()
|
|
|
|
|
|
|
|
|
|
|
|
async def test_emc_receive_messages_timeout(default_conf, caplog, mocker):
|
|
|
|
default_conf.update({
|
|
|
|
"external_message_consumer": {
|
|
|
|
"enabled": True,
|
|
|
|
"producers": [
|
|
|
|
{
|
|
|
|
"name": "default",
|
|
|
|
"url": f"ws://{_TEST_WS_HOST}:{_TEST_WS_PORT}/api/v1/message/ws",
|
|
|
|
"ws_token": _TEST_WS_TOKEN
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"wait_timeout": 1,
|
|
|
|
"ping_timeout": 1,
|
|
|
|
"sleep_time": 1
|
2022-09-11 05:57:17 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
mocker.patch('freqtrade.rpc.external_message_consumer.ExternalMessageConsumer.start',
|
|
|
|
MagicMock())
|
|
|
|
|
|
|
|
lock = asyncio.Lock()
|
|
|
|
test_producer = default_conf['external_message_consumer']['producers'][0]
|
|
|
|
|
|
|
|
dp = DataProvider(default_conf, None, None, None)
|
|
|
|
emc = ExternalMessageConsumer(default_conf, dp)
|
|
|
|
|
2022-09-11 06:50:18 +00:00
|
|
|
loop = asyncio.get_event_loop()
|
|
|
|
def change_running(emc): emc._running = not emc._running
|
|
|
|
|
|
|
|
class TestChannel:
|
|
|
|
async def recv(self, *args, **kwargs):
|
|
|
|
await asyncio.sleep(10)
|
|
|
|
|
|
|
|
async def ping(self, *args, **kwargs):
|
|
|
|
return asyncio.Future()
|
|
|
|
|
|
|
|
try:
|
|
|
|
change_running(emc)
|
|
|
|
loop.call_soon(functools.partial(change_running, emc=emc))
|
|
|
|
await emc._receive_messages(TestChannel(), test_producer, lock)
|
|
|
|
|
|
|
|
assert log_has_re(r"Ping error.+", caplog)
|
|
|
|
finally:
|
|
|
|
emc.shutdown()
|
|
|
|
|
|
|
|
|
|
|
|
async def test_emc_receive_messages_handle_error(default_conf, caplog, mocker):
|
|
|
|
default_conf.update({
|
|
|
|
"external_message_consumer": {
|
|
|
|
"enabled": True,
|
|
|
|
"producers": [
|
|
|
|
{
|
|
|
|
"name": "default",
|
|
|
|
"url": f"ws://{_TEST_WS_HOST}:{_TEST_WS_PORT}/api/v1/message/ws",
|
|
|
|
"ws_token": _TEST_WS_TOKEN
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"wait_timeout": 1,
|
|
|
|
"ping_timeout": 1,
|
|
|
|
"sleep_time": 1
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
mocker.patch('freqtrade.rpc.external_message_consumer.ExternalMessageConsumer.start',
|
|
|
|
MagicMock())
|
|
|
|
|
|
|
|
lock = asyncio.Lock()
|
|
|
|
test_producer = default_conf['external_message_consumer']['producers'][0]
|
|
|
|
|
|
|
|
dp = DataProvider(default_conf, None, None, None)
|
|
|
|
emc = ExternalMessageConsumer(default_conf, dp)
|
|
|
|
|
|
|
|
emc.handle_producer_message = MagicMock(side_effect=Exception)
|
2022-09-11 05:57:17 +00:00
|
|
|
|
|
|
|
loop = asyncio.get_event_loop()
|
|
|
|
def change_running(emc): emc._running = not emc._running
|
|
|
|
|
2022-09-11 06:50:18 +00:00
|
|
|
class TestChannel:
|
|
|
|
async def recv(self, *args, **kwargs):
|
|
|
|
return {"type": "whitelist", "data": ["BTC/USDT"]}
|
|
|
|
|
|
|
|
async def ping(self, *args, **kwargs):
|
|
|
|
return asyncio.Future()
|
|
|
|
|
2022-09-11 05:57:17 +00:00
|
|
|
try:
|
2022-09-11 06:50:18 +00:00
|
|
|
change_running(emc)
|
|
|
|
loop.call_soon(functools.partial(change_running, emc=emc))
|
|
|
|
await emc._receive_messages(TestChannel(), test_producer, lock)
|
2022-09-11 05:57:17 +00:00
|
|
|
|
2022-09-11 06:50:18 +00:00
|
|
|
assert log_has_re(r"Error handling producer message.+", caplog)
|
2022-09-11 05:57:17 +00:00
|
|
|
finally:
|
|
|
|
emc.shutdown()
|