| 1 | import logging |
| 2 | import time |
| 3 | from unittest.mock import MagicMock, PropertyMock |
| 4 | |
| 5 | from freqtrade.data.dataprovider import DataProvider |
| 6 | from freqtrade.state import State |
| 7 | from freqtrade.worker import Worker |
| 8 | from tests.conftest import get_patched_worker, log_has, log_has_re |
| 9 | |
| 10 | |
| 11 | def test_worker_state(mocker, default_conf, markets) -> None: |
| 12 | mocker.patch('freqtrade.exchange.Exchange.markets', PropertyMock(return_value=markets)) |
| 13 | worker = get_patched_worker(mocker, default_conf) |
| 14 | assert worker.freqtrade.state is State.RUNNING |
| 15 | |
| 16 | default_conf.pop('initial_state') |
| 17 | worker = Worker(args=None, config=default_conf) |
| 18 | assert worker.freqtrade.state is State.STOPPED |
| 19 | |
| 20 | |
| 21 | def test_worker_running(mocker, default_conf, caplog) -> None: |
| 22 | mock_throttle = MagicMock() |
| 23 | mocker.patch('freqtrade.worker.Worker._throttle', mock_throttle) |
| 24 | mocker.patch('freqtrade.persistence.Trade.stoploss_reinitialization', MagicMock()) |
| 25 | |
| 26 | worker = get_patched_worker(mocker, default_conf) |
| 27 | |
| 28 | state = worker._worker(old_state=None) |
| 29 | assert state is State.RUNNING |
| 30 | assert log_has('Changing state to: RUNNING', caplog) |
| 31 | assert mock_throttle.call_count == 1 |
| 32 | # Check strategy is loaded, and received a dataprovider object |
| 33 | assert worker.freqtrade.strategy |
| 34 | assert worker.freqtrade.strategy.dp |
| 35 | assert isinstance(worker.freqtrade.strategy.dp, DataProvider) |
| 36 | |
| 37 | |
| 38 | def test_worker_stopped(mocker, default_conf, caplog) -> None: |
| 39 | mock_throttle = MagicMock() |
| 40 | mocker.patch('freqtrade.worker.Worker._throttle', mock_throttle) |
| 41 | |
| 42 | worker = get_patched_worker(mocker, default_conf) |
| 43 | worker.freqtrade.state = State.STOPPED |
| 44 | state = worker._worker(old_state=State.RUNNING) |
| 45 | assert state is State.STOPPED |
| 46 | assert log_has('Changing state to: STOPPED', caplog) |
| 47 | assert mock_throttle.call_count == 1 |
| 48 | |
| 49 | |
| 50 | def test_throttle(mocker, default_conf, caplog) -> None: |
| 51 | def throttled_func(): |
| 52 | return 42 |
| 53 | |
| 54 | caplog.set_level(logging.DEBUG) |
| 55 | worker = get_patched_worker(mocker, default_conf) |
| 56 | |
| 57 | start = time.time() |
| 58 | result = worker._throttle(throttled_func, throttle_secs=0.1) |
| 59 | end = time.time() |
| 60 | |
| 61 | assert result == 42 |
| 62 | assert end - start > 0.1 |
| 63 | assert log_has_re(r"Throttling with 'throttled_func\(\)': sleep for \d\.\d{2} s.*", caplog) |
| 64 | |
| 65 | result = worker._throttle(throttled_func, throttle_secs=-1) |
| 66 | assert result == 42 |
| 67 | |
| 68 | |
| 69 | def test_throttle_with_assets(mocker, default_conf) -> None: |
| 70 | def throttled_func(nb_assets=-1): |
| 71 | return nb_assets |
| 72 | |
| 73 | worker = get_patched_worker(mocker, default_conf) |
| 74 | |
| 75 | result = worker._throttle(throttled_func, throttle_secs=0.1, nb_assets=666) |
| 76 | assert result == 666 |
| 77 | |
| 78 | result = worker._throttle(throttled_func, throttle_secs=0.1) |
| 79 | assert result == -1 |
| 80 | |
| 81 | |
| 82 | def test_worker_heartbeat_running(default_conf, mocker, caplog): |
| 83 | message = r"Bot heartbeat\. PID=.*state='RUNNING'" |
| 84 | |
| 85 | mock_throttle = MagicMock() |
| 86 | mocker.patch('freqtrade.worker.Worker._throttle', mock_throttle) |
| 87 | worker = get_patched_worker(mocker, default_conf) |
| 88 | |
| 89 | worker.freqtrade.state = State.RUNNING |
| 90 | worker._worker(old_state=State.STOPPED) |
| 91 | assert log_has_re(message, caplog) |
| 92 | |
| 93 | caplog.clear() |
| 94 | # Message is not shown before interval is up |
| 95 | worker._worker(old_state=State.RUNNING) |
| 96 | assert not log_has_re(message, caplog) |
| 97 | |
| 98 | caplog.clear() |
| 99 | # Set clock - 70 seconds |
| 100 | worker._heartbeat_msg -= 70 |
| 101 | worker._worker(old_state=State.RUNNING) |
| 102 | assert log_has_re(message, caplog) |
| 103 | |
| 104 | |
| 105 | def test_worker_heartbeat_stopped(default_conf, mocker, caplog): |
| 106 | message = r"Bot heartbeat\. PID=.*state='STOPPED'" |
| 107 | |
| 108 | mock_throttle = MagicMock() |
| 109 | mocker.patch('freqtrade.worker.Worker._throttle', mock_throttle) |
| 110 | worker = get_patched_worker(mocker, default_conf) |
| 111 | |
| 112 | worker.freqtrade.state = State.STOPPED |
| 113 | worker._worker(old_state=State.RUNNING) |
| 114 | assert log_has_re(message, caplog) |
| 115 | |
| 116 | caplog.clear() |
| 117 | # Message is not shown before interval is up |
| 118 | worker._worker(old_state=State.STOPPED) |
| 119 | assert not log_has_re(message, caplog) |
| 120 | |
| 121 | caplog.clear() |
| 122 | # Set clock - 70 seconds |
| 123 | worker._heartbeat_msg -= 70 |
| 124 | worker._worker(old_state=State.STOPPED) |
| 125 | assert log_has_re(message, caplog) |