Skip to content

fix race in TestReceiver #1558

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

Merged
merged 3 commits into from
Jan 24, 2017
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,9 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Deque;
import java.util.List;
import java.util.concurrent.ConcurrentLinkedDeque;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.LinkedBlockingQueue;
import org.joda.time.Duration;
import org.junit.After;
import org.junit.Before;
Expand Down Expand Up @@ -87,8 +86,8 @@ public static Collection<Object[]> data() {
private TestReceiver testReceiver;

static class TestReceiver implements MessageReceiver {
private final Deque<SettableFuture<AckReply>> outstandingMessageReplies =
new ConcurrentLinkedDeque<>();
private final LinkedBlockingQueue<SettableFuture<AckReply>> outstandingMessageReplies =
new LinkedBlockingQueue<>();
private AckReply ackReply = AckReply.ACK;
private Optional<CountDownLatch> messageCountLatch = Optional.absent();
private Optional<Throwable> error = Optional.absent();
Expand Down Expand Up @@ -124,34 +123,40 @@ public ListenableFuture<AckReply> receiveMessage(PubsubMessage message) {
SettableFuture<AckReply> reply = SettableFuture.create();

if (explicitAckReplies) {
outstandingMessageReplies.add(reply);
} else {
if (error.isPresent()) {
reply.setException(error.get());
} else {
reply.set(ackReply);
try {
outstandingMessageReplies.put(reply);
} catch (InterruptedException e) {
throw new IllegalStateException(e);
}
} else {
replyTo(reply);
}

return reply;
}

public void replyNextOutstandingMessage() {
Preconditions.checkState(explicitAckReplies);

SettableFuture<AckReply> reply = outstandingMessageReplies.poll();
if (error.isPresent()) {
reply.setException(error.get());
} else {
reply.set(ackReply);
try {
replyTo(outstandingMessageReplies.take());
} catch (InterruptedException e) {
throw new IllegalStateException(e);
}
}

public void replyAllOutstandingMessage() {
Preconditions.checkState(explicitAckReplies);
SettableFuture<AckReply> reply;
while ((reply = outstandingMessageReplies.poll()) != null) {
replyTo(reply);
}

This comment was marked as spam.

This comment was marked as spam.

}

while (!outstandingMessageReplies.isEmpty()) {
replyNextOutstandingMessage();
private void replyTo(SettableFuture<AckReply> reply) {
if (error.isPresent()) {
reply.setException(error.get());
} else {
reply.set(ackReply);
}
}
}
Expand Down