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