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

Fix NPE when trying to delete not existing queue #74

Merged
merged 1 commit into from Dec 12, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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