Spek: Memoized is not thread safe.

Created on 6 Jul 2019  路  4Comments  路  Source: spekframework/spek

If a memoized value is accessed concurrently by many threads in the same test, then it might create the value many times.

Example:

describe("concurrent access to memoized") {
    val obj by memoized {
        println("create")
    }

    it("test") {
        runBlocking {
            val barrier = CompletableDeferred<Unit>()

            repeat(100) {
                launch(Dispatchers.Default) {
                    barrier.await()
                    obj
                }
            }

            barrier.complete(Unit)
        }
    }
}

on my computer prints:

create
create
create
create
create
create
create
create
create
create
create
bug

Most helpful comment

In 2.1.0 memoized will be eagerly evaluated (see #835), I don't think it's a good idea to include it to in the 2.0.x line since it is a big change. I might re-scope the 2.1.0 release to just include a better IJ plugin and coroutines support, before, I wanted to add Kotlin\Native support but unfortunately the compiler plugin api is not stable enough so I guess I'll push that back.

All 4 comments

This might become obselete once I make memoized eagerly evaluated.

Encountered same problem, when will eagerly evaluated memoized be available?

As the PR for 'eager evaluation of memoized ' has been closed (#661). Maybe memoized could be made thread safe in the mean time?

In 2.1.0 memoized will be eagerly evaluated (see #835), I don't think it's a good idea to include it to in the 2.0.x line since it is a big change. I might re-scope the 2.1.0 release to just include a better IJ plugin and coroutines support, before, I wanted to add Kotlin\Native support but unfortunately the compiler plugin api is not stable enough so I guess I'll push that back.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Benbyday picture Benbyday  路  4Comments

raosuj picture raosuj  路  3Comments

windyboy picture windyboy  路  3Comments

mattbdean picture mattbdean  路  7Comments

snowe2010 picture snowe2010  路  5Comments