-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Using fake clock in AckDeadlineRenewerTest #1413
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
garrettjonesgoogle
merged 2 commits into
googleapis:master
from
garrettjonesgoogle:master
Nov 21, 2016
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
google-cloud-pubsub/src/test/java/com/google/cloud/pubsub/FakeClock.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| /* | ||
| * Copyright 2016 Google Inc. All Rights Reserved. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.google.cloud.pubsub; | ||
|
|
||
| import com.google.cloud.Clock; | ||
| import java.util.concurrent.TimeUnit; | ||
| import java.util.concurrent.atomic.AtomicLong; | ||
|
|
||
| /** | ||
| * A Clock to help with testing time-based logic. | ||
| */ | ||
| class FakeClock extends Clock { | ||
|
|
||
| private final AtomicLong millis = new AtomicLong(); | ||
|
|
||
| // Advances the clock value by {@code time} in {@code timeUnit}. | ||
| void advance(long time, TimeUnit timeUnit) { | ||
| millis.addAndGet(timeUnit.toMillis(time)); | ||
| } | ||
|
|
||
| @Override | ||
| public long millis() { | ||
| return millis.get(); | ||
| } | ||
| } |
182 changes: 182 additions & 0 deletions
182
google-cloud-pubsub/src/test/java/com/google/cloud/pubsub/FakeScheduledExecutorService.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,182 @@ | ||
| /* | ||
| * Copyright 2016 Google Inc. All Rights Reserved. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.google.cloud.pubsub; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Iterator; | ||
| import java.util.List; | ||
| import java.util.concurrent.Callable; | ||
| import java.util.concurrent.Delayed; | ||
| import java.util.concurrent.ExecutionException; | ||
| import java.util.concurrent.Executors; | ||
| import java.util.concurrent.ScheduledFuture; | ||
| import java.util.concurrent.ScheduledThreadPoolExecutor; | ||
| import java.util.concurrent.TimeUnit; | ||
| import java.util.concurrent.TimeoutException; | ||
|
|
||
| /** | ||
| * A ScheduledExecutorService to help with testing. | ||
| */ | ||
| class FakeScheduledExecutorService extends ScheduledThreadPoolExecutor { | ||
| private final FakeClock clock; | ||
| private final List<FakeScheduledFuture> futures = new ArrayList<>(); | ||
|
|
||
| public FakeScheduledExecutorService(int corePoolSize, FakeClock clock) { | ||
| super(corePoolSize); | ||
| this.clock = clock; | ||
| } | ||
|
|
||
| public ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit) { | ||
| synchronized (this) { | ||
| long runAtMillis = clock.millis() + unit.toMillis(delay); | ||
| FakeScheduledFuture scheduledFuture = | ||
| new FakeScheduledFuture(command, delay, unit, runAtMillis); | ||
| futures.add(scheduledFuture); | ||
| return scheduledFuture; | ||
| } | ||
| } | ||
|
|
||
| public <V> ScheduledFuture<V> schedule(Callable<V> callable, long delay, TimeUnit unit) { | ||
| throw new UnsupportedOperationException( | ||
| "FakeScheduledExecutorService.schedule(Callable, long, TimeUnit) not supported"); | ||
| } | ||
|
|
||
| public ScheduledFuture<?> scheduleAtFixedRate(Runnable command, long initialDelay, long period, | ||
| TimeUnit unit) { | ||
| throw new UnsupportedOperationException( | ||
| "FakeScheduledExecutorService.scheduleAtFixedRate not supported"); | ||
| } | ||
|
|
||
| public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, | ||
| TimeUnit unit) { | ||
| throw new UnsupportedOperationException( | ||
| "FakeScheduledExecutorService.scheduleAtFixedRate not supported"); | ||
| } | ||
|
|
||
| public void tick(long time, TimeUnit unit) { | ||
| List<FakeScheduledFuture> runnablesToGo = new ArrayList<>(); | ||
| synchronized (this) { | ||
| clock.advance(time, unit); | ||
| Iterator<FakeScheduledFuture> iter = futures.iterator(); | ||
| while (iter.hasNext()) { | ||
| FakeScheduledFuture scheduledFuture = iter.next(); | ||
| if (scheduledFuture.runAtMillis <= clock.millis()) { | ||
| runnablesToGo.add(scheduledFuture); | ||
| iter.remove(); | ||
| } | ||
| } | ||
| } | ||
| for (FakeScheduledFuture scheduledFuture : runnablesToGo) { | ||
| scheduledFuture.run(); | ||
| } | ||
| } | ||
|
|
||
| private boolean cancel(FakeScheduledFuture toCancel) { | ||
| synchronized (this) { | ||
| Iterator<FakeScheduledFuture> iter = futures.iterator(); | ||
| while (iter.hasNext()) { | ||
| FakeScheduledFuture scheduledFuture = iter.next(); | ||
| if (scheduledFuture == toCancel) { | ||
| iter.remove(); | ||
| return true; | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| private class FakeScheduledFuture implements ScheduledFuture<Object> { | ||
| final Callable<Object> callable; | ||
| final long delay; | ||
| final TimeUnit unit; | ||
| final long runAtMillis; | ||
|
|
||
| volatile boolean isDone; | ||
| volatile boolean isCancelled; | ||
| volatile Exception exception; | ||
| volatile Object result; | ||
|
|
||
| FakeScheduledFuture(Runnable runnable, long delay, TimeUnit unit, long runAtMillis) { | ||
| this.callable = Executors.callable(runnable); | ||
| this.delay = delay; | ||
| this.unit = unit; | ||
| this.runAtMillis = runAtMillis; | ||
| } | ||
|
|
||
| @Override | ||
| public long getDelay(TimeUnit requestedUnit) { | ||
| return unit.convert(delay, requestedUnit); | ||
| } | ||
|
|
||
| @Override | ||
| public int compareTo(Delayed other) { | ||
| return Long.compare(getDelay(TimeUnit.MILLISECONDS), other.getDelay(TimeUnit.MILLISECONDS)); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean cancel(boolean var1) { | ||
| if (isCancelled) { | ||
| return isCancelled; | ||
| } | ||
| isCancelled = FakeScheduledExecutorService.this.cancel(this); | ||
| return isCancelled; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean isCancelled() { | ||
| return isCancelled; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean isDone() { | ||
| return isDone; | ||
| } | ||
|
|
||
| @Override | ||
| public Object get() throws InterruptedException, ExecutionException { | ||
| if (!isDone()) { | ||
| throw new UnsupportedOperationException("FakeScheduledFuture: blocking get not supported"); | ||
| } | ||
|
|
||
| if (exception != null) { | ||
| throw new ExecutionException(exception); | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| @Override | ||
| public Object get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, | ||
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong. |
||
| TimeoutException { | ||
| return get(); | ||
| } | ||
|
|
||
| public void run() { | ||
| if (isDone()) { | ||
| throw new UnsupportedOperationException("FakeScheduledFuture already done."); | ||
| } | ||
|
|
||
| try { | ||
| result = callable.call(); | ||
| } catch (Exception e) { | ||
| exception = e; | ||
| } | ||
|
|
||
| isDone = true; | ||
| } | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.