diff options
author | Brandon Maxwell <maxwelb@google.com> | 2016-02-12 20:08:38 +0000 |
---|---|---|
committer | android-build-merger <android-build-merger@google.com> | 2016-02-12 20:08:38 +0000 |
commit | 71579e23fddbd18d095515854b1a88894e1ed5ba (patch) | |
tree | 9dee122fc7ed1407dd479c37452fc7cdd3a07162 | |
parent | f4b1095b2d52bf07b5f61900c940ef95bb86772b (diff) | |
parent | 4c6d68244beab9c9855594ab88168eb855496ac0 (diff) |
Added executor framework to sync prod and tests am: 7ba4e96fdc
am: 4c6d68244b
* commit '4c6d68244beab9c9855594ab88168eb855496ac0':
Added executor framework to sync prod and tests
3 files changed, 153 insertions, 0 deletions
diff --git a/InCallUI/src/com/android/incallui/async/PausableExecutor.java b/InCallUI/src/com/android/incallui/async/PausableExecutor.java new file mode 100644 index 000000000..fdeef360c --- /dev/null +++ b/InCallUI/src/com/android/incallui/async/PausableExecutor.java @@ -0,0 +1,53 @@ +/* + * Copyright (C) 2016 The Android Open Source Project + * + * 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.android.incallui.async; + +import com.android.contacts.common.testing.NeededForTesting; + +import java.util.concurrent.Executor; + +/** + * Executor that can be used to easily synchronize testing and production code. Production code + * should call {@link #milestone()} at points in the code where the state of the system is worthy of + * testing. In a test scenario, this method will pause execution until the test acknowledges the + * milestone through the use of {@link #ackMilestoneForTesting()}. + */ +public interface PausableExecutor extends Executor { + + /** + * Method called from asynchronous production code to inform this executor that it has + * reached a point that puts the system into a state worth testing. TestableExecutors intended + * for use in a testing environment should cause the calling thread to block. In the production + * environment this should be a no-op. + */ + void milestone(); + + /** + * Method called from the test code to inform this executor that the state of the production + * system at the current milestone has been sufficiently tested. Every milestone must be + * acknowledged. + */ + @NeededForTesting + void ackMilestoneForTesting(); + + /** + * Method called from the test code to block until a milestone has been reached in the + * production code. + */ + @NeededForTesting + void awaitMilestoneForTesting() throws InterruptedException; +} diff --git a/InCallUI/src/com/android/incallui/async/PausableExecutorImpl.java b/InCallUI/src/com/android/incallui/async/PausableExecutorImpl.java new file mode 100644 index 000000000..e493feb3d --- /dev/null +++ b/InCallUI/src/com/android/incallui/async/PausableExecutorImpl.java @@ -0,0 +1,39 @@ +/* + * Copyright (C) 2016 The Android Open Source Project + * + * 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.android.incallui.async; + +import java.util.concurrent.Executors; + +/** + * {@link PausableExecutor} intended for use in production environments. + */ +public class PausableExecutorImpl implements PausableExecutor { + + @Override + public void milestone() {} + + @Override + public void ackMilestoneForTesting() {} + + @Override + public void awaitMilestoneForTesting() {} + + @Override + public void execute(Runnable command) { + Executors.newSingleThreadExecutor().execute(command); + } +} diff --git a/InCallUI/tests/src/com/android/incallui/async/SingleProdThreadExecutor.java b/InCallUI/tests/src/com/android/incallui/async/SingleProdThreadExecutor.java new file mode 100644 index 000000000..ee27862b3 --- /dev/null +++ b/InCallUI/tests/src/com/android/incallui/async/SingleProdThreadExecutor.java @@ -0,0 +1,61 @@ +/* + * Copyright (C) 2016 The Android Open Source Project + * + * 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.android.incallui.async; + +import java.util.concurrent.Executors; + +import javax.annotation.concurrent.ThreadSafe; + +/** + * {@link PausableExecutor} for use in tests. It is intended to be used between one test thread + * and one prod thread. + */ +@ThreadSafe +public final class SingleProdThreadExecutor implements PausableExecutor { + + private int mMilestonesReached; + private int mMilestonesAcked; + + @Override + public synchronized void milestone() { + ++mMilestonesReached; + notify(); + while (mMilestonesReached > mMilestonesAcked) { + try { + wait(); + } catch (InterruptedException e) {} + } + } + + @Override + public synchronized void ackMilestoneForTesting() { + ++mMilestonesAcked; + notify(); + } + + @Override + public synchronized void awaitMilestoneForTesting() throws InterruptedException { + while (mMilestonesReached <= mMilestonesAcked) { + wait(); + } + } + + @Override + public synchronized void execute(Runnable command) { + Executors.newSingleThreadExecutor().execute(command); + } +} |