Skip to content

Commit

Permalink
[MatrixManager] Add event clear
Browse files Browse the repository at this point in the history
  • Loading branch information
Herklos committed Apr 17, 2020
1 parent fbadaf0 commit a194bca
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 8 deletions.
14 changes: 13 additions & 1 deletion octobot_evaluators/data_manager/matrix_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ def get_tentacle_value_path(cryptocurrency=None, symbol=None, time_frame=None) -

async def get_nodes_event(matrix_id, nodes_path, timeout=None):
"""
Return the asyncio.wait
Return the asyncio.wait of nodes event
:param matrix_id: the matrix id
:param nodes_path: the tentacle node path
:param timeout: the event waiting timeout (when None no timeout)
Expand All @@ -166,6 +166,18 @@ async def get_nodes_event(matrix_id, nodes_path, timeout=None):
for node_path in nodes_path])


async def get_nodes_clear_event(matrix_id, nodes_path, timeout=None):
"""
Return the asyncio.wait of nodes clear event
:param matrix_id: the matrix id
:param nodes_path: the tentacle node path
:param timeout: the event waiting timeout (when None no timeout)
:return: the
"""
return asyncio.gather(*[asyncio.wait_for(get_tentacle_node(matrix_id, node_path).node_clear_event.wait(),
timeout=timeout) for node_path in nodes_path])


async def subscribe_nodes_event(matrix_id, nodes_path, callback, timeout=None):
"""
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ numpy==1.18.2

# Drakkar-Software requirements
OctoBot-Channels>=1.3.23, <1.4
OctoBot-Commons>=1.3.9, <1.4
OctoBot-Commons>=1.3.10, <1.4
OctoBot-Tentacles-Manager>=2.0.2, <2.1

# Cryptocurrency trading
Expand Down
78 changes: 72 additions & 6 deletions tests/data_manager/test_matrix_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from octobot_evaluators.data.matrix import Matrix
from octobot_evaluators.data_manager.matrix_manager import get_tentacle_path, get_tentacle_value_path, \
get_tentacle_nodes, get_tentacles_value_nodes, get_matrix_default_value_path, set_tentacle_value, \
get_tentacle_value, get_nodes_event
get_tentacle_value, get_nodes_event, get_nodes_clear_event, get_tentacle_node
from octobot_evaluators.matrices.matrices import Matrices


Expand Down Expand Up @@ -367,6 +367,7 @@ async def test_get_nodes_event():
set_tentacle_value(matrix.matrix_id, evaluator_4_path, "TA", None)
set_tentacle_value(matrix.matrix_id, evaluator_5_path, "TA", None)

# Asserts that a newly created event can't be successfuly awaited
with pytest.raises(asyncio.TimeoutError):
await asyncio.wait_for(await get_nodes_event(matrix.matrix_id, [evaluator_1_path, evaluator_3_path]), timeout=1)

Expand Down Expand Up @@ -400,11 +401,11 @@ async def test_get_nodes_event():
await wait_should_failed

wait_should_failed = asyncio.wait_for(await get_nodes_event(matrix.matrix_id,
[get_matrix_default_value_path(tentacle_type="TA",
tentacle_name="Test-TA",
cryptocurrency="BTC",
symbol="BTC/USD")]),
timeout=1)
[get_matrix_default_value_path(tentacle_type="TA",
tentacle_name="Test-TA",
cryptocurrency="BTC",
symbol="BTC/USD")]),
timeout=1)
wait_should_succeed = asyncio.wait_for(await get_nodes_event(matrix.matrix_id,
[get_matrix_default_value_path(tentacle_type="TA",
tentacle_name="Test-TA",
Expand All @@ -419,3 +420,68 @@ async def test_get_nodes_event():
await wait_should_failed

Matrices.instance().del_matrix(matrix.matrix_id)


@pytest.mark.asyncio
async def test_get_nodes_clear_event():
matrix = Matrix()
Matrices.instance().add_matrix(matrix)

evaluator_1_path = get_matrix_default_value_path(tentacle_type="TA", tentacle_name="Test-TA",
cryptocurrency="BTC",
symbol="BTC/USD",
time_frame="1m")
evaluator_2_path = get_matrix_default_value_path(tentacle_type="TA", tentacle_name="Test-TA",
cryptocurrency="BTC",
symbol="BTC/USD",
time_frame="5m")
evaluator_3_path = get_matrix_default_value_path(tentacle_type="TA", tentacle_name="Test-TA",
cryptocurrency="BTC",
symbol="BTC/USD",
time_frame="1h")
evaluator_4_path = get_matrix_default_value_path(tentacle_type="TA", tentacle_name="Test-TA",
cryptocurrency="BTC",
symbol="BTC/USD",
time_frame="4h")
evaluator_5_path = get_matrix_default_value_path(tentacle_type="TA", tentacle_name="Test-TA",
cryptocurrency="BTC",
symbol="BTC/USD",
time_frame="1d")

# simulate AbstractEvaluator.initialize()
set_tentacle_value(matrix.matrix_id, evaluator_1_path, "TA", None)
set_tentacle_value(matrix.matrix_id, evaluator_2_path, "TA", None)
set_tentacle_value(matrix.matrix_id, evaluator_3_path, "TA", None)
set_tentacle_value(matrix.matrix_id, evaluator_4_path, "TA", None)
set_tentacle_value(matrix.matrix_id, evaluator_5_path, "TA", None)

set_tentacle_value(matrix.matrix_id, evaluator_1_path, "TA", None)
set_tentacle_value(matrix.matrix_id, evaluator_2_path, "TA", None)
set_tentacle_value(matrix.matrix_id, evaluator_3_path, "TA", None)
await asyncio.wait_for(await get_nodes_clear_event(matrix.matrix_id,
[get_matrix_default_value_path(
tentacle_type="TA",
tentacle_name="Test-TA",
cryptocurrency="BTC")]),
timeout=1)
assert all([not get_tentacle_node(matrix.matrix_id, node_path).node_event.is_set()
for node_path in
[evaluator_1_path, evaluator_2_path, evaluator_3_path, evaluator_4_path, evaluator_5_path]])
set_tentacle_value(matrix.matrix_id, evaluator_4_path, "TA", None)
set_tentacle_value(matrix.matrix_id, evaluator_5_path, "TA", None)
assert not all([not get_tentacle_node(matrix.matrix_id, node_path).node_event.is_set()
for node_path in
[evaluator_1_path, evaluator_2_path, evaluator_3_path, evaluator_4_path, evaluator_5_path]])
assert all([get_tentacle_node(matrix.matrix_id, node_path).node_event.is_set()
for node_path in
[evaluator_4_path, evaluator_5_path]])
await asyncio.wait_for(await get_nodes_clear_event(matrix.matrix_id,
[get_matrix_default_value_path(
tentacle_type="TA",
tentacle_name="Test-TA",
cryptocurrency="BTC")]),
timeout=1)
assert all([not get_tentacle_node(matrix.matrix_id, node_path).node_event.is_set()
for node_path in
[evaluator_1_path, evaluator_2_path, evaluator_3_path, evaluator_4_path, evaluator_5_path]])
Matrices.instance().del_matrix(matrix.matrix_id)

0 comments on commit a194bca

Please sign in to comment.