-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Add way to convert CoroutineDispatcher to Scheduler #1923
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
Closed
Closed
Changes from all commits
Commits
Show all changes
76 commits
Select commit
Hold shift + click to select a range
abc2158
created DispatcherScheduler and added first test
f016ca8
some refactoring to better handle cancellation
f7f318e
added test for shutdown
cc423ea
remove redundant isActive check
7bb4530
remove try finally
81e65c7
remove comment
39df4ed
add test for scheduler with SchedulerCoroutineDispatcher
7d0d475
remove formatting changes
99f3e6c
add missing expectUnreached call in one test
136260c
add a small kdoc
23085cb
improve scheduleDirect with delay.
0e936fe
optimize imports
a9b99eb
formatting changes
3a27d4b
using job from launch for worker
2733fd8
remove spaces
6fa3ad1
simplify job handling, thus fix OOM
9c9e867
remove back ticks from test names
56919ff
call into overload
695d553
now handling non positive delays
566e2f4
formatting
7b8c5cf
using supervisorscope
f5306e2
using if expressions for return
56d09c9
create test that verifies sequential ordering
dd61f82
make rx scheduler worker sequential
af07533
trying out using channel to schedule sequentially
eb18274
made queue processing easier. also fixed unit tests
a3d02f3
return DispatcherScheduler dispatcher in asCoroutineDispatcher
04a1fef
add tests to verify conversions between dispatcher and scheduler
6beb046
small comment
a086d62
moving stress tests to another class. ignoring for now
debaa09
checking in current state of tests
e346b93
fix sequential ordering if we're using a dispatcher with thread pool
621388c
simplify isActive check
0c62da3
let channel suspend while empty
ef71887
change test name
9bd16a7
now running blocks directly in scheduleDirect
2e9f1f1
test with observables
09e990b
running blocks more directly inside DispatcherScheduler. also fixing …
de113d3
make asDisposable private
89eceae
fix dispose logic
ebdaf8e
move queue job processing into constructor
bbe3edb
making schedule block defer to override with delay param
9cf2c13
reduce if nesting
7686e2f
call RxJavaPlugins before delay
435faee
rx plugins for worker too
e40083b
add top level CoroutineExceptionHandler
9ab3762
adding if check to schedule direct
fda0e7a
now using coroutines test to assert rxjavaplugins call
9d8b4ae
remove queue processing job
4d5dd17
improving how jobs are added to the channel
37bff66
adding comment about keepMe function
8b90b5b
use run blocking test for tests that call delay
44c9ebe
removing redundant thread tests
537de18
separate plugin tests
eb3da9b
adding more tests for worker tasks
a100fb0
make worker job private
554ef38
check for job cancelled
33d5c67
remove println
9e100c2
refactor tests to be more generic for worker tests in next commit
446c787
wrote tests for scheduler worker and made fixes based off tests
631110b
updating scheduler stress tests to include worker tests
e0d70d4
make sure we're waiting for delayJob properly
cb506a6
refactor delay logic (again :)) to delay first before enqueuing work
0477dfd
remove redundant expect
53e0122
remove redundant channel close
7b361b3
using one job for both non-delayed and delayed. also fix redundant jo…
26c9e1e
remove redundant modifier
e5e86f1
api dump
64b3743
use start function and reuse JobDisposable
78457b3
remove addTaskToQueue function
5f5e637
fix typo in tests
b634a8c
unit test for multiple workers
8f204bf
maintain binary compat. also formatting
a829462
api dump
15f96b3
change if statements
2aab409
add asScheduler extension for rx3
recheej 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
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
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
114 changes: 114 additions & 0 deletions
114
reactive/kotlinx-coroutines-rx2/test/SchedulerStressTest.kt
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,114 @@ | ||
/* | ||
* Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package kotlinx.coroutines.rx2 | ||
|
||
import kotlinx.coroutines.* | ||
import kotlinx.coroutines.test.* | ||
import org.junit.* | ||
import java.util.concurrent.* | ||
|
||
class SchedulerStressTest : TestBase() { | ||
@Before | ||
fun setup() { | ||
ignoreLostThreads("RxCachedThreadScheduler-", "RxCachedWorkerPoolEvictor-", "RxSchedulerPurge-") | ||
} | ||
|
||
/** | ||
* Test that we don't get an OOM if we schedule many jobs at once. It's expected that if you don't dispose that you'd | ||
* see a OOM error. | ||
*/ | ||
@Test | ||
fun testSchedulerDisposed(): Unit = runTest { | ||
val dispatcher = currentDispatcher() as CoroutineDispatcher | ||
val scheduler = dispatcher.asScheduler() | ||
testRunnableDisposed(scheduler::scheduleDirect) | ||
} | ||
|
||
@Test | ||
fun testSchedulerWorkerDisposed(): Unit = runTest { | ||
val dispatcher = currentDispatcher() as CoroutineDispatcher | ||
val scheduler = dispatcher.asScheduler() | ||
testRunnableDisposed(scheduler.createWorker()::schedule) | ||
} | ||
|
||
private suspend fun testRunnableDisposed(block: RxSchedulerBlockNoDelay) { | ||
expect(1) | ||
|
||
val dispatcher = currentDispatcher() as CoroutineDispatcher | ||
val scheduler = dispatcher.asScheduler() | ||
|
||
val n = 2000 * stressTestMultiplier | ||
coroutineScope { | ||
repeat(n) { i -> | ||
launch { | ||
val a = ByteArray(1000000) //1MB | ||
val disposable = block(Runnable { | ||
expectUnreached() | ||
runBlocking { | ||
keepMe(a) | ||
} | ||
recheej marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}) | ||
disposable.dispose() | ||
expect(i + 2) | ||
} | ||
yield() | ||
} | ||
} | ||
|
||
scheduler.shutdown() | ||
finish(n + 2) | ||
} | ||
|
||
/** | ||
* Test function that holds a reference. Used for testing OOM situations | ||
*/ | ||
private suspend fun keepMe(a: ByteArray) { | ||
delay(10) | ||
} | ||
|
||
/** | ||
* Test that we don't get an OOM if we schedule many delayed jobs at once. It's expected that if you don't dispose that you'd | ||
* see a OOM error. | ||
*/ | ||
@Test | ||
fun testSchedulerDisposedDuringDelay(): Unit = runBlockingTest { | ||
val dispatcher = currentDispatcher() as CoroutineDispatcher | ||
val scheduler = dispatcher.asScheduler() | ||
testRunnableDisposedDuringDelay(scheduler::scheduleDirect) | ||
} | ||
|
||
@Test | ||
fun testSchedulerWorkerDisposedDuringDelay(): Unit = runBlockingTest { | ||
val dispatcher = currentDispatcher() as CoroutineDispatcher | ||
val scheduler = dispatcher.asScheduler() | ||
testRunnableDisposedDuringDelay(scheduler.createWorker()::schedule) | ||
} | ||
|
||
private suspend fun TestCoroutineScope.testRunnableDisposedDuringDelay(block: RxSchedulerBlockWithDelay) { | ||
expect(1) | ||
|
||
val dispatcher = currentDispatcher() as CoroutineDispatcher | ||
val scheduler = dispatcher.asScheduler() | ||
|
||
val n = 2000 * stressTestMultiplier | ||
coroutineScope { | ||
repeat(n) { i -> | ||
val a = ByteArray(1000000) //1MB | ||
val delayMillis: Long = 10 | ||
val disposable = block(Runnable { | ||
runBlocking { | ||
keepMe(a) | ||
} | ||
}, delayMillis, TimeUnit.MILLISECONDS) | ||
disposable.dispose() | ||
advanceTimeBy(delayMillis) | ||
yield() | ||
} | ||
} | ||
|
||
scheduler.shutdown() | ||
finish(2) | ||
} | ||
} |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@elizarov is this "synthetic" ok?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. That is because it is HIDDEN. Old code that was compiled against the previous version of the library will still work.