Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* De-flake PicassoTest.loading_slot * Remove coroutines-test from Picasso * De-flake CoilTest.loading_slot
- Loading branch information
1 parent
260341e
commit f6d27a9
Showing
4 changed files
with
117 additions
and
55 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
64 changes: 64 additions & 0 deletions
64
.../src/androidTest/java/dev/chrisbanes/accompanist/picasso/SingleThreadPauseableExecutor.kt
This file contains 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,64 @@ | ||
/* | ||
* Copyright 2020 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 | ||
* | ||
* https://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 dev.chrisbanes.accompanist.picasso | ||
|
||
import java.util.concurrent.LinkedBlockingQueue | ||
import java.util.concurrent.ThreadPoolExecutor | ||
import java.util.concurrent.TimeUnit | ||
import java.util.concurrent.locks.ReentrantLock | ||
|
||
/** | ||
* This is copied from the javadoc on [ThreadPoolExecutor]. | ||
*/ | ||
internal class SingleThreadPausableExecutor(paused: Boolean = false) : ThreadPoolExecutor( | ||
1, 1, 0L, TimeUnit.MILLISECONDS, LinkedBlockingQueue() | ||
) { | ||
private var isPaused = paused | ||
private val pauseLock = ReentrantLock() | ||
private val unpaused = pauseLock.newCondition() | ||
|
||
override fun beforeExecute(t: Thread, r: Runnable) { | ||
super.beforeExecute(t, r) | ||
pauseLock.lock() | ||
try { | ||
while (isPaused) unpaused.await() | ||
} catch (ie: InterruptedException) { | ||
t.interrupt() | ||
} finally { | ||
pauseLock.unlock() | ||
} | ||
} | ||
|
||
fun pause() { | ||
pauseLock.lock() | ||
try { | ||
isPaused = true | ||
} finally { | ||
pauseLock.unlock() | ||
} | ||
} | ||
|
||
fun resume() { | ||
pauseLock.lock() | ||
try { | ||
isPaused = false | ||
unpaused.signalAll() | ||
} finally { | ||
pauseLock.unlock() | ||
} | ||
} | ||
} |