|
45 | 45 | import java.util.ArrayList; |
46 | 46 | import java.util.Arrays; |
47 | 47 | import java.util.Collection; |
48 | | -import java.util.Deque; |
49 | 48 | import java.util.List; |
50 | | -import java.util.concurrent.ConcurrentLinkedDeque; |
51 | 49 | import java.util.concurrent.CountDownLatch; |
| 50 | +import java.util.concurrent.LinkedBlockingQueue; |
52 | 51 | import org.joda.time.Duration; |
53 | 52 | import org.junit.After; |
54 | 53 | import org.junit.Before; |
@@ -87,8 +86,8 @@ public static Collection<Object[]> data() { |
87 | 86 | private TestReceiver testReceiver; |
88 | 87 |
|
89 | 88 | static class TestReceiver implements MessageReceiver { |
90 | | - private final Deque<SettableFuture<AckReply>> outstandingMessageReplies = |
91 | | - new ConcurrentLinkedDeque<>(); |
| 89 | + private final LinkedBlockingQueue<SettableFuture<AckReply>> outstandingMessageReplies = |
| 90 | + new LinkedBlockingQueue<>(); |
92 | 91 | private AckReply ackReply = AckReply.ACK; |
93 | 92 | private Optional<CountDownLatch> messageCountLatch = Optional.absent(); |
94 | 93 | private Optional<Throwable> error = Optional.absent(); |
@@ -124,34 +123,40 @@ public ListenableFuture<AckReply> receiveMessage(PubsubMessage message) { |
124 | 123 | SettableFuture<AckReply> reply = SettableFuture.create(); |
125 | 124 |
|
126 | 125 | if (explicitAckReplies) { |
127 | | - outstandingMessageReplies.add(reply); |
128 | | - } else { |
129 | | - if (error.isPresent()) { |
130 | | - reply.setException(error.get()); |
131 | | - } else { |
132 | | - reply.set(ackReply); |
| 126 | + try { |
| 127 | + outstandingMessageReplies.put(reply); |
| 128 | + } catch (InterruptedException e) { |
| 129 | + throw new IllegalStateException(e); |
133 | 130 | } |
| 131 | + } else { |
| 132 | + replyTo(reply); |
134 | 133 | } |
135 | 134 |
|
136 | 135 | return reply; |
137 | 136 | } |
138 | 137 |
|
139 | 138 | public void replyNextOutstandingMessage() { |
140 | 139 | Preconditions.checkState(explicitAckReplies); |
141 | | - |
142 | | - SettableFuture<AckReply> reply = outstandingMessageReplies.poll(); |
143 | | - if (error.isPresent()) { |
144 | | - reply.setException(error.get()); |
145 | | - } else { |
146 | | - reply.set(ackReply); |
| 140 | + try { |
| 141 | + replyTo(outstandingMessageReplies.take()); |
| 142 | + } catch (InterruptedException e) { |
| 143 | + throw new IllegalStateException(e); |
147 | 144 | } |
148 | 145 | } |
149 | 146 |
|
150 | 147 | public void replyAllOutstandingMessage() { |
151 | 148 | Preconditions.checkState(explicitAckReplies); |
| 149 | + SettableFuture<AckReply> reply; |
| 150 | + while ((reply = outstandingMessageReplies.poll()) != null) { |
| 151 | + replyTo(reply); |
| 152 | + } |
| 153 | + } |
152 | 154 |
|
153 | | - while (!outstandingMessageReplies.isEmpty()) { |
154 | | - replyNextOutstandingMessage(); |
| 155 | + private void replyTo(SettableFuture<AckReply> reply) { |
| 156 | + if (error.isPresent()) { |
| 157 | + reply.setException(error.get()); |
| 158 | + } else { |
| 159 | + reply.set(ackReply); |
155 | 160 | } |
156 | 161 | } |
157 | 162 | } |
|
0 commit comments