by skyde · 2y ago · 0 comments
But assuming you do not want to change the default isolation for your transaction or for the whole DB. How do you write unit-test that check your code will work correctly because it's using "select balance from accounts FOR UPDATE"?
It seem to me the only way to really validate this is using tool like TLA+. Or something like https://github.com/microsoft/coyote
I haven't heard of Coyote (it's still amazing how many tools are out there). It's good that it operates on the actual code level, but I'm still not sure that would reproduce concurrency non-determinism at the database level.
The problem is your mock need to implement the same isolation level as your real DB and support transaction ...
You could use SQLITE in memory DB to run your test but that would make your test a lot slower I assume.
At that point—why bother with the mock at all? Cutting out the intermediary and running integration tests of your application against a real database will be faster.
You can’t use SQLite here either because all SQLite isolation levels are serializable anyway, which is _very_ different from how PostgreSQL works. Testing against SQLite could end up giving you a false sense of security that your code is safe against race conditions, whereas in reality it’s vulnerable when connected to a PostgreSQL server because of the difference in default isolation levels
At this level of testing detail the only real option is to test against a real database that matches what you’re running in production. Otherwise you’re just testing a mock.
Except in the case of shared cache database connections with PRAGMA read_uncommitted turned on, all transactions in SQLite show "serializable" isolation
If your app contain 10 distinct transactions.
you have to test what happen if 2 instances of tx #1 run concurrently.
But also if tx #1 and tx #2 run concurrently.
And also what if tx #1 and tx #3 run concurrently ....