Skip to content

Commit

Permalink
Fix NPE when trying to delete not existing queue
Browse files Browse the repository at this point in the history
Both `queue.delete` and `exchange.delete` methods are idempotent.
In the case no queue is deleted, they behave as if an empty
queue/exchange was.
  • Loading branch information
ledoyen committed Dec 12, 2019
1 parent 46e54f1 commit 9509c57
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/main/java/com/github/fridujo/rabbitmq/mock/MockNode.java
Expand Up @@ -86,9 +86,9 @@ public AMQP.Queue.DeclareOk queueDeclare(String queueName, boolean durable, bool
}

public AMQP.Queue.DeleteOk queueDelete(String queueName, boolean ifUnused, boolean ifEmpty) {
MockQueue queue = queues.remove(queueName);
queue.notifyDeleted();
return new AMQImpl.Queue.DeleteOk(queue != null ? queue.messageCount() : 0);
Optional<MockQueue> queue = Optional.ofNullable(queues.remove(queueName));
queue.ifPresent(MockQueue::notifyDeleted);
return new AMQImpl.Queue.DeleteOk(queue.map(MockQueue::messageCount).orElse(0));
}

public AMQP.Queue.BindOk queueBind(String queueName, String exchangeName, String routingKey, Map<String, Object> arguments) {
Expand Down
25 changes: 24 additions & 1 deletion src/test/java/com/github/fridujo/rabbitmq/mock/ChannelTest.java
Expand Up @@ -7,6 +7,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.UUID;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -168,6 +169,17 @@ void exchangeDeleteNoWait_removes_it() throws IOException, TimeoutException {
}
}

@Test
void exchangeDelete_does_nothing_when_not_existing() throws IOException, TimeoutException {
try (Connection conn = new MockConnectionFactory().newConnection()) {
try (Channel channel = conn.createChannel()) {
assertThat(channel.exchangeDelete(UUID.randomUUID().toString())).isNotNull();
assertThat(channel.exchangeDelete(UUID.randomUUID().toString(), false)).isNotNull();
channel.exchangeDeleteNoWait(UUID.randomUUID().toString(), false);
}
}
}

@Test
void exchangeBind_binds_two_exchanges() throws IOException, TimeoutException {
try (Connection conn = new MockConnectionFactory().newConnection()) {
Expand Down Expand Up @@ -292,6 +304,17 @@ void queueDeleteNoWait_deletes_it() throws IOException, TimeoutException {
}
}

@Test
void queueDelete_does_nothing_when_not_existing() throws IOException, TimeoutException {
try (Connection conn = new MockConnectionFactory().newConnection()) {
try (Channel channel = conn.createChannel()) {
assertThat(channel.queueDelete(UUID.randomUUID().toString())).isNotNull();
assertThat(channel.queueDelete(UUID.randomUUID().toString(), false, false)).isNotNull();
channel.queueDeleteNoWait(UUID.randomUUID().toString(), false, false);
}
}
}

@Test
void queuePurge_removes_all_messages() throws IOException, TimeoutException {
try (Connection conn = new MockConnectionFactory().newConnection()) {
Expand Down Expand Up @@ -601,7 +624,7 @@ void commit_or_rollback_can_be_called_multiple_times_after_a_single_select() thr
channel.txCommit();
assertThat(channel.basicGet(queue, true)).isNotNull();
// Channel contained only one message as transactions are cleared after commit
assertThat(channel.basicGet(queue, true)).isNull();
assertThat(channel.basicGet(queue, true)).isNull();

channel.basicPublish("", queue, null, "third message".getBytes());
assertThat(channel.basicGet(queue, true)).isNull();
Expand Down

0 comments on commit 9509c57

Please sign in to comment.