Omar Mainegra
06/11/2020, 4:06 PMTestObservable
freezes? I'm been forced to use atomics instead of regular `var`s (i.e. to capture values, etc.) otherwise I get InvalidMutabilityException
, if I remove it everything works fine. At least could it be optional? like TestObservableObserver(autoFreeze)
Arkadii Ivanov
06/11/2020, 5:46 PMmyObservable.test(autoFreeze = false).
Omar Mainegra
06/11/2020, 5:48 PMTestScheduler
Arkadii Ivanov
06/11/2020, 5:49 PMTestScheduler
freezes by design. Because in real app schedulers freeze as well.Arkadii Ivanov
06/11/2020, 5:49 PMArkadii Ivanov
06/11/2020, 5:51 PMOmar Mainegra
06/11/2020, 5:51 PMArkadii Ivanov
06/11/2020, 5:53 PMOmar Mainegra
06/11/2020, 5:55 PMcapturedAdd
, so I need to use an AtomicXXX
instead, and that slower, since I use property checksArkadii Ivanov
06/11/2020, 5:56 PMLogCacheBaseMock
? It's not clear what's insideOmar Mainegra
06/11/2020, 5:57 PMOmar Mainegra
06/11/2020, 5:58 PMArkadii Ivanov
06/11/2020, 5:59 PMTestScheduler
used?Omar Mainegra
06/11/2020, 6:04 PMArkadii Ivanov
06/11/2020, 6:36 PMLogCacheBaseMock
so it captures the outer state (the variable). And then it gets frozen so everything captured is frozen as well. If you will do this in production you will have troubles.Arkadii Ivanov
06/11/2020, 6:38 PMTestScheduler
is designed to behave as real as possible.Arkadii Ivanov
06/11/2020, 6:42 PMLogCacheBaseMock
to TestLogCache
and implement "fake" logging inside. E.g. accumulate events into AtomicReference<List<LogData>>
. You can then expose a handy property like val data: List<LogData> get() = ref.value
Omar Mainegra
06/11/2020, 6:43 PMAtomicReference
with my code works, but then it is much slower, since every test case is run 500 times with random dataArkadii Ivanov
06/11/2020, 6:45 PMOmar Mainegra
06/11/2020, 6:46 PMList
since all ops happens in the same thread, the problem is with the testArkadii Ivanov
06/11/2020, 6:46 PMArkadii Ivanov
06/11/2020, 6:47 PMOmar Mainegra
06/11/2020, 6:47 PMArkadii Ivanov
06/11/2020, 6:47 PMArkadii Ivanov
06/11/2020, 6:48 PMOmar Mainegra
06/11/2020, 6:48 PMOmar Mainegra
06/11/2020, 6:48 PMArkadii Ivanov
06/11/2020, 6:48 PMlogCache
list will be frozenOmar Mainegra
06/11/2020, 6:49 PMArkadii Ivanov
06/11/2020, 6:50 PMOmar Mainegra
06/11/2020, 7:19 PMMainScheduler
directly (MainScheduler()
instead of using mainScheduler
) I had to copied locally because it's internal (I double check and is the same, no changes on my side)Omar Mainegra
06/11/2020, 7:24 PMclass LongStore {
private var _value: Long = 0
fun apply(value: Long) {
_value = value
}
fun get(): Long = _value
}
class UseCase(store: LongStore, scheduler: Scheduler) {
private val disposable = CompositeDisposable()
init {
disposable += observableInterval(300, scheduler)
.subscribe { store.apply(it) }
}
}
This doesn't work
val store = LongStore()
val useCase = UseCase(store, mainScheduler)
But this does
val store = LongStore()
val useCase = UseCase(store, MainScheduler())
Omar Mainegra
06/11/2020, 7:25 PMOmar Mainegra
06/11/2020, 7:28 PMOmar Mainegra
06/11/2020, 7:34 PMArkadii Ivanov
06/11/2020, 8:20 PMOperation
is added to the list, which is stored in the Executor
. Executor
itself is stored in the MainScheduler
. The MainScheduler
(as well as all other default schedulers) is frozen, so all Executors
are frozen as well, and so all their data is frozen. Operation
gets frozen anyway.
Your log
method schedules a callback to the main thread. So I assume that the log
method may be called from any thread. If this statement is true, then you callback will be frozen anyway (regardless of the scheduler being used, and if it is frozen or not). If this statement is false (the log
method is called only from the main thread), then you don't need scheduler at all.Arkadii Ivanov
06/11/2020, 8:21 PMmainScheduler
, no need to create a new scheduler every time.Arkadii Ivanov
06/11/2020, 8:23 PMOmar Mainegra
06/11/2020, 8:25 PMOmar Mainegra
06/11/2020, 8:27 PMOmar Mainegra
06/11/2020, 8:28 PMArkadii Ivanov
06/11/2020, 8:48 PMIsolate
thing (https://github.com/touchlab/Stately#stately-isolate) . It may give better performance. It creates a dedicated thread for the mutable state and communicates via events.Omar Mainegra
06/11/2020, 8:48 PM