1- from typing import Callable , Dict
1+ import asyncio
2+ from typing import Tuple
23
34import pytest
4- from aleph_message .models import PostMessage
5+ from aleph_message .models import ItemHash
56
67from aleph .sdk .client import AuthenticatedAlephClient
78from aleph .sdk .models .message import MessageFilter
89from aleph .sdk .types import Account
910
1011from .config import REFERENCE_NODE , TARGET_NODE , TEST_CHANNEL
11- from .toolkit import try_until
12+ from .toolkit import has_messages , has_no_messages , try_until
1213
1314
1415async def create_and_forget_post (
1516 account : Account , emitter_node : str , receiver_node : str , channel = TEST_CHANNEL
16- ) -> str :
17- async def wait_matching_posts (
18- item_hash : str ,
19- condition : Callable [[Dict ], bool ],
20- timeout : int = 5 ,
21- ):
22- async with AuthenticatedAlephClient (
23- account = account , api_server = receiver_node
24- ) as rx_session :
25- return await try_until (
26- rx_session .get_posts ,
27- condition ,
28- timeout = timeout ,
29- hashes = [item_hash ],
30- )
31-
17+ ) -> Tuple [ItemHash , ItemHash ]:
3218 async with AuthenticatedAlephClient (
3319 account = account , api_server = emitter_node
3420 ) as tx_session :
@@ -38,13 +24,17 @@ async def wait_matching_posts(
3824 channel = "INTEGRATION_TESTS" ,
3925 )
4026
41- # Wait for the message to appear on the receiver. We don't check the values,
42- # they're checked in other integration tests.
43- get_post_response = await wait_matching_posts (
44- post_message .item_hash ,
45- lambda response : len (response ["posts" ]) > 0 ,
46- )
47- print (get_post_response )
27+ async with AuthenticatedAlephClient (
28+ account = account , api_server = receiver_node
29+ ) as rx_session :
30+ await try_until (
31+ rx_session .get_messages ,
32+ has_messages ,
33+ timeout = 5 ,
34+ message_filter = MessageFilter (
35+ hashes = [post_message .item_hash ],
36+ ),
37+ )
4838
4939 post_hash = post_message .item_hash
5040 reason = "This well thought-out content offends me!"
@@ -56,27 +46,34 @@ async def wait_matching_posts(
5646 reason = reason ,
5747 channel = channel ,
5848 )
59-
6049 assert forget_message .sender == account .get_address ()
6150 assert forget_message .content .reason == reason
6251 assert forget_message .content .hashes == [post_hash ]
63-
64- print (forget_message )
52+ forget_hash = forget_message .item_hash
6553
6654 # Wait until the message is forgotten
67- forgotten_posts = await wait_matching_posts (
68- post_hash ,
69- lambda response : "forgotten_by" in response ["posts" ][0 ],
70- timeout = 15 ,
71- )
55+ async with AuthenticatedAlephClient (
56+ account = account , api_server = receiver_node
57+ ) as rx_session :
58+ await try_until (
59+ rx_session .get_messages ,
60+ has_messages ,
61+ timeout = 5 ,
62+ message_filter = MessageFilter (
63+ hashes = [forget_hash ],
64+ ),
65+ )
7266
73- assert len (forgotten_posts ["posts" ]) == 1
74- forgotten_post = forgotten_posts ["posts" ][0 ]
75- assert forgotten_post ["forgotten_by" ] == [forget_message .item_hash ]
76- assert forgotten_post ["item_content" ] is None
77- print (forgotten_post )
67+ await try_until (
68+ rx_session .get_messages ,
69+ has_no_messages ,
70+ timeout = 5 ,
71+ message_filter = MessageFilter (
72+ hashes = [post_hash ],
73+ ),
74+ )
7875
79- return post_hash
76+ return post_hash , forget_hash
8077
8178
8279@pytest .mark .asyncio
@@ -85,7 +82,7 @@ async def test_create_and_forget_post_on_target(fixture_account):
8582 Create a post on the target node, then forget it and check that the change is propagated
8683 to the reference node.
8784 """
88- _ = await create_and_forget_post (fixture_account , TARGET_NODE , REFERENCE_NODE )
85+ _ , _ = await create_and_forget_post (fixture_account , TARGET_NODE , REFERENCE_NODE )
8986
9087
9188@pytest .mark .asyncio
@@ -94,7 +91,7 @@ async def test_create_and_forget_post_on_reference(fixture_account):
9491 Create a post on the reference node, then forget it and check that the change is propagated
9592 to the target node.
9693 """
97- _ = await create_and_forget_post (fixture_account , REFERENCE_NODE , TARGET_NODE )
94+ _ , _ = await create_and_forget_post (fixture_account , REFERENCE_NODE , TARGET_NODE )
9895
9996
10097@pytest .mark .asyncio
@@ -104,26 +101,33 @@ async def test_forget_a_forget_message(fixture_account):
104101 """
105102
106103 # TODO: this test should be moved to the PyAleph API tests, once a framework is in place.
107- post_hash = await create_and_forget_post (fixture_account , TARGET_NODE , TARGET_NODE )
104+ post_hash , forget_hash = await create_and_forget_post (
105+ fixture_account , TARGET_NODE , REFERENCE_NODE
106+ )
108107 async with AuthenticatedAlephClient (
109108 account = fixture_account , api_server = TARGET_NODE
110- ) as session :
111- get_post_message : PostMessage = await session .get_message (post_hash )
112-
113- forget_message_hash = get_post_message .forgotten_by [0 ]
114- forget_message , forget_status = await session .forget (
115- hashes = [forget_message_hash ],
109+ ) as tx_session :
110+ forget_message , forget_status = await tx_session .forget (
111+ hashes = [forget_hash ],
116112 reason = "I want to remember this post. Maybe I can forget I forgot it?" ,
117113 channel = TEST_CHANNEL ,
118114 )
119115
120116 print (forget_message )
121117
122- get_forget_message_response = await session .get_messages (
118+ # wait 5 seconds
119+ await asyncio .sleep (5 )
120+
121+ async with AuthenticatedAlephClient (
122+ account = fixture_account , api_server = REFERENCE_NODE
123+ ) as rx_session :
124+ get_forget_message_response = await try_until (
125+ rx_session .get_messages ,
126+ has_messages ,
127+ timeout = 5 ,
123128 message_filter = MessageFilter (
124- hashes = [forget_message_hash ],
125- channels = [TEST_CHANNEL ],
126- )
129+ hashes = [forget_hash ],
130+ ),
127131 )
128132 assert len (get_forget_message_response .messages ) == 1
129133 forget_message = get_forget_message_response .messages [0 ]
0 commit comments