Skip to content

Commit 66b0934

Browse files
committed
Address Copilot comments (2)
Signed-off-by: Sergio Herrera <627709+seherv@users.noreply.github.com>
1 parent 197a974 commit 66b0934

3 files changed

Lines changed: 26 additions & 5 deletions

File tree

tests/integration/apps/pubsub_subscriber.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@
1717
@app.subscribe(pubsub_name='pubsub', topic='TOPIC_A')
1818
def handle_topic_a(event: v1.Event) -> TopicEventResponse:
1919
data = json.loads(event.Data())
20+
key = f'received-{data["run_id"]}-{data["id"]}'
2021
with DaprClient() as d:
21-
d.save_state('statestore', f'received-topic-a-{data["id"]}', event.Data())
22+
d.save_state('statestore', key, event.Data())
2223
return TopicEventResponse('success')
2324

2425

tests/integration/test_configuration.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def _redis_set(key: str, value: str, version: int = 1) -> None:
1616
Dapr's Redis configuration store encodes values as ``value||version``.
1717
"""
1818
subprocess.run(
19-
args=('docker', 'exec', REDIS_CONTAINER, 'redis-cli', 'SET', key, f'"{value}||{version}"'),
19+
args=('docker', 'exec', REDIS_CONTAINER, 'redis-cli', 'SET', key, f'{value}||{version}'),
2020
check=True,
2121
capture_output=True,
2222
)

tests/integration/test_pubsub.py

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,34 @@
11
import json
2+
import subprocess
23
import time
4+
import uuid
35

46
import pytest
57

68
STORE = 'statestore'
79
PUBSUB = 'pubsub'
810
TOPIC = 'TOPIC_A'
11+
REDIS_CONTAINER = 'dapr_redis'
12+
13+
14+
def _flush_redis() -> None:
15+
"""Flush the Dapr Redis instance to prevent state leaking between runs.
16+
17+
Both the state store and the pubsub component point at the same
18+
``dapr_redis`` container (see ``tests/integration/components/``), so a
19+
previous run's ``received-*`` keys could otherwise satisfy this test's
20+
assertions even if no new message was delivered.
21+
"""
22+
subprocess.run(
23+
args=('docker', 'exec', REDIS_CONTAINER, 'redis-cli', 'FLUSHDB'),
24+
check=True,
25+
capture_output=True,
26+
)
927

1028

1129
@pytest.fixture(scope='module')
1230
def client(dapr_env, apps_dir):
31+
_flush_redis()
1332
return dapr_env.start_sidecar(
1433
app_id='test-subscriber',
1534
grpc_port=50001,
@@ -20,19 +39,20 @@ def client(dapr_env, apps_dir):
2039

2140

2241
def test_published_messages_are_received_by_subscriber(client):
42+
run_id = uuid.uuid4().hex
2343
for n in range(1, 4):
2444
client.publish_event(
2545
pubsub_name=PUBSUB,
2646
topic_name=TOPIC,
27-
data=json.dumps({'id': n, 'message': 'hello world'}),
47+
data=json.dumps({'run_id': run_id, 'id': n, 'message': 'hello world'}),
2848
data_content_type='application/json',
2949
)
3050
time.sleep(1)
3151

3252
time.sleep(3)
3353

3454
for n in range(1, 4):
35-
state = client.get_state(store_name=STORE, key=f'received-topic-a-{n}')
55+
state = client.get_state(store_name=STORE, key=f'received-{run_id}-{n}')
3656
assert state.data != b'', f'Subscriber did not receive message {n}'
3757
msg = json.loads(state.data)
3858
assert msg['id'] == n
@@ -44,6 +64,6 @@ def test_publish_event_succeeds(client):
4464
client.publish_event(
4565
pubsub_name=PUBSUB,
4666
topic_name=TOPIC,
47-
data=json.dumps({'id': 99, 'message': 'smoke test'}),
67+
data=json.dumps({'run_id': uuid.uuid4().hex, 'id': 99, 'message': 'smoke test'}),
4868
data_content_type='application/json',
4969
)

0 commit comments

Comments
 (0)