Skip to content

Commit 204438c

Browse files
committed
Adding documentation to the new added method in the
FakeScheduledExecutor that allow you to set expectations on scheduled work.
1 parent 6a21a54 commit 204438c

2 files changed

Lines changed: 17 additions & 2 deletions

File tree

google-cloud-pubsub/src/test/java/com/google/cloud/pubsub/spi/v1/FakeScheduledExecutorService.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,23 @@ public ScheduledFuture<?> scheduleWithFixedDelay(
8282
Duration.millis(unit.toMillis(initialDelay)), command, PendingCallableType.FIXED_DELAY));
8383
}
8484

85+
/**
86+
* This allows for adding expectations on future work to be scheduled (
87+
* {@link FakeScheduledExecutorService#schedule}
88+
* or {@link FakeScheduledExecutorService#scheduleAtFixedRate}
89+
* or {@link FakeScheduledExecutorService#scheduleWithFixedDelay}) based on its delay.
90+
*/
8591
public void setupScheduleExpectation(Duration delay) {
8692
synchronized (expectedWorkQueue) {
8793
expectedWorkQueue.add(delay);
8894
}
8995
}
9096

97+
/**
98+
* Blocks the current thread until all the work
99+
* {@link FakeScheduledExecutorService#setupScheduleExpectation(Duration) expected} has been
100+
* scheduled in the executor.
101+
*/
91102
public void waitForExpectedWork() {
92103
synchronized (expectedWorkQueue) {
93104
while (!expectedWorkQueue.isEmpty()) {
@@ -115,7 +126,8 @@ private void work() {
115126
for (;;) {
116127
PendingCallable<?> callable = null;
117128
synchronized (pendingCallables) {
118-
if (pendingCallables.isEmpty() || pendingCallables.peek().getScheduledTime().isAfter(cmpTime)) {
129+
if (pendingCallables.isEmpty()
130+
|| pendingCallables.peek().getScheduledTime().isAfter(cmpTime)) {
119131
break;
120132
}
121133
callable = pendingCallables.poll();
@@ -204,6 +216,9 @@ <V> ScheduledFuture<V> schedulePendingCallable(PendingCallable<V> callable) {
204216
}
205217
work();
206218
synchronized (expectedWorkQueue) {
219+
// We compare by the callable delay in order decide when to remove expectations from the
220+
// expected work queue, i.e. only the expected work that matches the delay of the scheduled
221+
// callable is removed from the queue.
207222
if (!expectedWorkQueue.isEmpty() && expectedWorkQueue.peek().equals(callable.delay)) {
208223
expectedWorkQueue.poll();
209224
}

google-cloud-pubsub/src/test/java/com/google/cloud/pubsub/spi/v1/SubscriberImplTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ public void testModifyAckDeadline() throws Exception {
284284
// Send messages to be acked
285285
List<String> testAckIdsBatch = ImmutableList.of("A", "B", "C");
286286
testReceiver.setExplicitAck(true);
287-
// A modify ack deadline should be schedule for the next 9s
287+
// A modify ack deadline should be scheduled for the next 9s
288288
fakeExecutor.setupScheduleExpectation(Duration.standardSeconds(9));
289289
sendMessages(testAckIdsBatch);
290290
// To ensure first modify ack deadline got scheduled

0 commit comments

Comments
 (0)