Skip to content

Commit 375ea3e

Browse files
Merge branch 'main' of github.com:ag2ai/faststream into fix/fastapi-body-none-for-empty-message
# Conflicts: # faststream/_internal/kafka/keys.py
2 parents da60e12 + 3fed896 commit 375ea3e

6 files changed

Lines changed: 153 additions & 27 deletions

File tree

‎faststream/nats/helpers/bucket_declarer.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ def connect(self, connection: "JetStreamContext") -> None:
2828

2929
def disconnect(self) -> None:
3030
self.__state = EmptyConnectionState()
31+
self.buckets.clear()
3132

3233
async def create_key_value(
3334
self,

‎faststream/nats/helpers/obj_storage_declarer.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ def connect(self, connection: "JetStreamContext") -> None:
2828

2929
def disconnect(self) -> None:
3030
self.__state = EmptyConnectionState()
31+
self.buckets.clear()
3132

3233
async def create_object_store(
3334
self,

‎tests/brokers/confluent/test_publish_command.py‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,35 @@
22

33
from faststream.confluent.response import KafkaPublishCommand, KafkaResponse
44
from faststream.response import ensure_response
5+
from faststream.response.publish_type import PublishType
56
from tests.brokers.base.publish_command import BatchPublishCommandTestcase
67

78

89
@pytest.mark.confluent()
910
class TestPublishCommand(BatchPublishCommandTestcase):
1011
publish_command_cls = KafkaPublishCommand
1112

13+
@pytest.mark.parametrize("body", (None, "duplicate"))
14+
def test_duplicate_bodies_keep_distinct_keys_after_reassignment(
15+
self, body: str | None
16+
) -> None:
17+
"""Fixes https://github.com/ag2ai/faststream/issues/3359."""
18+
cmd = self.publish_command_cls(
19+
"present",
20+
KafkaResponse(body, key=b"key-a"),
21+
KafkaResponse(body, key=b"key-b"),
22+
topic="events",
23+
key=b"key-default",
24+
_publish_type=PublishType.PUBLISH,
25+
)
26+
expected_keys = (b"key-default", b"key-a", b"key-b")
27+
assert tuple(cmd.key_for(i) for i in range(3)) == expected_keys
28+
29+
cmd.batch_bodies = cmd.batch_bodies
30+
31+
assert cmd.batch_bodies == ("present", body, body)
32+
assert tuple(cmd.key_for(i) for i in range(3)) == expected_keys
33+
1234
def test_kafka_response_class(self) -> None:
1335
response = ensure_response(KafkaResponse(body=1, headers={"1": 1}, key=b"1"))
1436
cmd = self.publish_command_cls.from_cmd(response.as_publish_command())

‎tests/brokers/kafka/test_publish_command.py‎

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,35 @@
11
import pytest
22

33
from faststream.kafka.response import KafkaPublishCommand, KafkaResponse
4-
from faststream.response import ensure_response
4+
from faststream.response import PublishType, ensure_response
55
from tests.brokers.base.publish_command import BatchPublishCommandTestcase
66

77

88
@pytest.mark.kafka()
99
class TestPublishCommand(BatchPublishCommandTestcase):
1010
publish_command_cls = KafkaPublishCommand
1111

12+
@pytest.mark.parametrize("body", (None, "duplicate"))
13+
def test_duplicate_bodies_keep_distinct_keys_after_reassignment(
14+
self, body: str | None
15+
) -> None:
16+
"""Fixes https://github.com/ag2ai/faststream/issues/3359."""
17+
cmd = self.publish_command_cls(
18+
"present",
19+
KafkaResponse(body, key=b"key-a"),
20+
KafkaResponse(body, key=b"key-b"),
21+
topic="events",
22+
key=b"key-default",
23+
_publish_type=PublishType.PUBLISH,
24+
)
25+
expected_keys = (b"key-default", b"key-a", b"key-b")
26+
assert tuple(cmd.key_for(i) for i in range(3)) == expected_keys
27+
28+
cmd.batch_bodies = cmd.batch_bodies
29+
30+
assert cmd.batch_bodies == ("present", body, body)
31+
assert tuple(cmd.key_for(i) for i in range(3)) == expected_keys
32+
1233
def test_kafka_response_class(self) -> None:
1334
response = ensure_response(KafkaResponse(body=1, headers={"1": 1}, key=b"1"))
1435
cmd = self.publish_command_cls.from_cmd(response.as_publish_command())

‎tests/brokers/nats/test_kv_declarer_cache.py‎

Lines changed: 52 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,61 @@
33
import pytest
44
from nats.js import JetStreamContext
55

6-
from faststream.nats import NatsBroker
6+
from faststream.exceptions import IncorrectState
77
from tests.tools import spy_decorator
88

9+
from .basic import NatsTestcaseConfig
10+
911

1012
@pytest.mark.connected()
1113
@pytest.mark.asyncio()
1214
@pytest.mark.nats()
13-
async def test_kv_storage_cache() -> None:
14-
broker = NatsBroker()
15-
async with broker:
16-
with patch.object(
17-
JetStreamContext,
18-
"create_key_value",
19-
spy_decorator(JetStreamContext.create_key_value),
20-
) as m:
21-
await broker.key_value("test")
22-
await broker.key_value("test")
23-
assert broker.config.kv_declarer.buckets["test"]
24-
m.mock.assert_called_once()
15+
class TestKVStorageCache(NatsTestcaseConfig):
16+
async def test_kv_storage_cache(self, queue: str) -> None:
17+
broker = self.get_broker()
18+
async with self.patch_broker(broker) as br:
19+
with patch.object(
20+
JetStreamContext,
21+
"create_key_value",
22+
spy_decorator(JetStreamContext.create_key_value),
23+
) as m:
24+
await br.key_value(queue)
25+
await br.key_value(queue)
26+
assert br.config.kv_declarer.buckets[queue]
27+
m.mock.assert_called_once()
28+
29+
async def test_kv_storage_reconnect(self, queue: str) -> None:
30+
"""Fixes https://github.com/ag2ai/faststream/issues/3331."""
31+
broker = self.get_broker()
32+
async with self.patch_broker(broker) as br:
33+
bucket = await br.key_value(queue)
34+
await bucket.put("key", b"original")
35+
36+
async with self.patch_broker(broker) as br:
37+
bucket = await br.key_value(queue)
38+
assert (await bucket.get("key")).value == b"original"
39+
await bucket.put("key", b"updated")
40+
assert (await bucket.get("key")).value == b"updated"
41+
42+
async def test_kv_storage_reconnect_without_declaration(
43+
self,
44+
queue: str,
45+
) -> None:
46+
"""Fixes https://github.com/ag2ai/faststream/issues/3331."""
47+
broker = self.get_broker()
48+
async with self.patch_broker(broker) as br:
49+
await br.key_value(queue)
50+
51+
async with self.patch_broker(broker) as br:
52+
bucket = await br.key_value(queue, declare=False)
53+
await bucket.put("key", b"reconnected")
54+
assert (await bucket.get("key")).value == b"reconnected"
55+
56+
async def test_kv_storage_access_after_disconnect(self, queue: str) -> None:
57+
"""Fixes https://github.com/ag2ai/faststream/issues/3331."""
58+
broker = self.get_broker()
59+
async with self.patch_broker(broker) as br:
60+
await br.key_value(queue)
61+
62+
with pytest.raises(IncorrectState):
63+
await broker.key_value(queue)

‎tests/brokers/nats/test_os_declarer_cache.py‎

Lines changed: 55 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,64 @@
33
import pytest
44
from nats.js import JetStreamContext
55

6-
from faststream.nats import NatsBroker
6+
from faststream.exceptions import IncorrectState
77
from tests.tools import spy_decorator
88

9+
from .basic import NatsTestcaseConfig
10+
911

1012
@pytest.mark.connected()
1113
@pytest.mark.asyncio()
1214
@pytest.mark.nats()
13-
async def test_object_storage_cache() -> None:
14-
broker = NatsBroker()
15-
async with broker:
16-
with patch.object(
17-
JetStreamContext,
18-
"create_object_store",
19-
spy_decorator(JetStreamContext.create_object_store),
20-
) as m:
21-
await broker.object_storage("test")
22-
await broker.object_storage("test")
23-
assert broker.config.os_declarer.buckets["test"]
24-
m.mock.assert_called_once()
15+
class TestObjectStorageCache(NatsTestcaseConfig):
16+
async def test_object_storage_cache(self, queue: str) -> None:
17+
broker = self.get_broker()
18+
async with self.patch_broker(broker) as br:
19+
with patch.object(
20+
JetStreamContext,
21+
"create_object_store",
22+
spy_decorator(JetStreamContext.create_object_store),
23+
) as m:
24+
await br.object_storage(queue)
25+
await br.object_storage(queue)
26+
assert br.config.os_declarer.buckets[queue]
27+
m.mock.assert_called_once()
28+
29+
async def test_object_storage_reconnect(self, queue: str) -> None:
30+
"""Fixes https://github.com/ag2ai/faststream/issues/3331."""
31+
broker = self.get_broker()
32+
async with self.patch_broker(broker) as br:
33+
bucket = await br.object_storage(queue)
34+
await bucket.put("key", b"original")
35+
36+
async with self.patch_broker(broker) as br:
37+
bucket = await br.object_storage(queue)
38+
assert (await bucket.get("key")).data == b"original"
39+
await bucket.put("key", b"updated")
40+
assert (await bucket.get("key")).data == b"updated"
41+
42+
async def test_object_storage_reconnect_without_declaration(
43+
self,
44+
queue: str,
45+
) -> None:
46+
"""Fixes https://github.com/ag2ai/faststream/issues/3331."""
47+
broker = self.get_broker()
48+
async with self.patch_broker(broker) as br:
49+
await br.object_storage(queue)
50+
51+
async with self.patch_broker(broker) as br:
52+
bucket = await br.object_storage(queue, declare=False)
53+
await bucket.put("key", b"reconnected")
54+
assert (await bucket.get("key")).data == b"reconnected"
55+
56+
async def test_object_storage_access_after_disconnect(
57+
self,
58+
queue: str,
59+
) -> None:
60+
"""Fixes https://github.com/ag2ai/faststream/issues/3331."""
61+
broker = self.get_broker()
62+
async with self.patch_broker(broker) as br:
63+
await br.object_storage(queue)
64+
65+
with pytest.raises(IncorrectState):
66+
await broker.object_storage(queue)

0 commit comments

Comments
 (0)