Is it somehow possible to "emulate" the multithrea...
# kotlin-native
m
Is it somehow possible to "emulate" the multithreading behavior of K/N on the JVM ? Have the runtime throw on multithreaded accesses on non-freezed objects ? My dev experience is way better on the JVM (debugger & compile time mainly) so if I could catch the freeze exceptions there, that would save me a lot of time.
a
I don't think so. I prefer to write unit tests for this and run them for all targets.
Not sure about coroutines, but this is how I do it with Reaktive:
Copy code
class MyClass {
    private var value = 0

    fun foo() {
        completableFromFunction { value = 10 } // Updating value from a background thread
            .subscribeOn(ioScheduler)
            .subscribe()
    }
}

class MyClassTest {
    @BeforeTest
    fun before() {
        overrideSchedulers(main = { TestScheduler() }, io = { TestScheduler() })
    }

    @Test
    fun test_foo() {
        MyClass().foo() // <-- InvalidMutabilityException
    }
}
k
yes, write common unit tests, and then perhaps a couple that are platform specific to test concurrency
i haven't been able to debug on either platform, so println has become my best friend
d
jw wrote some things about this I didn't understand https://jakewharton.com/litmus-testing-kotlins-many-memory-models/
👍 1
m
Thanks for the article, that was exactly my problem, which I solved by removing top level singletons: https://github.com/HearthSim/Arcane-Tracker/commit/65c0c3672fb1f87f1e40d474e1fb2966f1c5257a It works and writing test will help catching these issues. Having some kind of "strict threading mode" that I could enforce at runtime would also be useful
❤️ 1
Or maybe a warning on top level objects without an annotation ?