Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

publish message without exchange declaration #116

Merged
merged 3 commits into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/src/client/channel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,14 @@ abstract class Channel {
/// returned future will fail with a [ExchangeNotFoundException] if the exchange does not exist.
///
/// The [durable] flag will enable the exchange to persist across server restarts.
///
/// The [declare] flag can be set to false to skip the exchange declaration step
/// for clients with read-only access to the broker.
Future<Exchange> exchange(String name, ExchangeType type,
{bool passive = false,
Jafarili marked this conversation as resolved.
Show resolved Hide resolved
bool durable = false,
bool noWait = false,
bool declare = true,
Map<String, Object> arguments});

/// Setup the [prefetchSize] and [prefetchCount] QoS parameters. The value
Expand Down
7 changes: 7 additions & 0 deletions lib/src/client/impl/channel_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,7 @@ class _ChannelImpl implements Channel {
{bool passive = false,
bool durable = false,
bool noWait = false,
bool declare = true,
Map<String, Object>? arguments}) {
if (name.isEmpty) {
throw ArgumentError("The name of the exchange cannot be empty");
Expand All @@ -579,6 +580,12 @@ class _ChannelImpl implements Channel {
..arguments = arguments;

Completer<Exchange> opCompleter = Completer<Exchange>();

if (!declare) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please add a test case similar to this in the exchange test suite?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done :)

opCompleter.complete(_ExchangeImpl(this, name, type));
return opCompleter.future;
}

writeMessage(exchangeRequest,
completer: opCompleter,
futurePayload: _ExchangeImpl(this, name, type),
Expand Down
32 changes: 32 additions & 0 deletions test/lib/exchange_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,38 @@ main({bool enableLogger = true}) {
await boundQueue.unbind(exchange, "");
});

test("publish to exchange without exchange declaration", () async {
Completer testCompleter = Completer();

// Use the second client to define the queue and the exchange in advance
Channel channel2 = await client2.channel();
Queue queue = await channel2.queue("q_test_ro");
Exchange exchange = await channel2.exchange("exc_test_ro", ExchangeType.FANOUT);
queue.bind(exchange, '');

// Pretend we are a RO consumer that cannot declare the exchange but
// should still be able to publish to it.
Channel channel = await client.channel();
Exchange exchangeRo = await channel
.exchange("exc_test_ro", ExchangeType.FANOUT, declare: false);

expect(exchangeRo.channel, const TypeMatcher<Channel>());
expect(exchangeRo.type, equals(ExchangeType.FANOUT));
expect(exchangeRo.name, "exc_test_ro");

exchangeRo.publish("Test payload", "");

Consumer consumer = await queue.consume();
consumer.listen((AmqpMessage reply) {
expect(reply.payloadAsString, equals("Test payload"));

// Pass!
testCompleter.complete();
});

return testCompleter.future;
});

group("exceptions", () {
test("missing exchange name", () async {
Channel channel = await client.channel();
Expand Down