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

Do not confirmSelect more than once per channel #1057

Merged
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
11 changes: 10 additions & 1 deletion src/main/java/com/rabbitmq/client/impl/ChannelN.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ public class ChannelN extends AMQChannel implements com.rabbitmq.client.Channel
private final SortedSet<Long> unconfirmedSet =
Collections.synchronizedSortedSet(new TreeSet<Long>());

/** Whether the confirm select method has been successfully activated */
private boolean confirmSelectActivated = false;

/** Whether any nacks have been received since the last waitForConfirms(). */
private volatile boolean onlyAcksReceived = true;

Expand Down Expand Up @@ -1553,10 +1556,16 @@ public Tx.RollbackOk txRollback()
public Confirm.SelectOk confirmSelect()
throws IOException
{
if (confirmSelectActivated) {
return new Confirm.SelectOk();
}

if (nextPublishSeqNo == 0) nextPublishSeqNo = 1;
return (Confirm.SelectOk)
Confirm.SelectOk result = (Confirm.SelectOk)
exnWrappingRpc(new Confirm.Select(false)).getMethod();

confirmSelectActivated = true;
return result;
}

/** Public API - {@inheritDoc} */
Expand Down
26 changes: 26 additions & 0 deletions src/test/java/com/rabbitmq/client/test/ChannelNTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@

package com.rabbitmq.client.test;

import com.rabbitmq.client.Command;
import com.rabbitmq.client.Method;
import com.rabbitmq.client.TrafficListener;
import com.rabbitmq.client.impl.*;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
Expand All @@ -26,6 +28,7 @@
import java.util.concurrent.Executors;
import java.util.stream.Stream;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.jupiter.api.Assertions.assertNotNull;

public class ChannelNTest {

Expand Down Expand Up @@ -81,6 +84,29 @@ public TestConfig(int value, Consumer call) {
.forEach(config -> assertThatThrownBy(() -> config.call.apply(config.value)).isInstanceOf(IllegalArgumentException.class));
}

@Test
public void confirmSelectOnlySendsRPCCallOnce() throws Exception {
AMQConnection connection = Mockito.mock(AMQConnection.class);
TrafficListener trafficListener = Mockito.mock(TrafficListener.class);

Mockito.when(connection.getTrafficListener()).thenReturn(trafficListener);

ChannelN channel = new ChannelN(connection, 1, consumerWorkService);

new Thread(() -> {
try {
Thread.sleep(15);
channel.handleCompleteInboundCommand(new AMQCommand(new AMQImpl.Confirm.SelectOk()));
} catch (Exception e) {
throw new RuntimeException(e);
}
}).start();

assertNotNull(channel.confirmSelect());
assertNotNull(channel.confirmSelect());
Mockito.verify(trafficListener, Mockito.times(1)).write(Mockito.any(Command.class));
}

interface Consumer {

void apply(int value) throws Exception;
Expand Down