Testing a Basic Concurrent Cache

To use stateful tests, we’ll first need a stateful system to validate. In this section, we’ll use a cache implemented as an OTP gen_server. A common optimization pattern in Erlang is to use an ETS table for reads, and to make the writes sequential through calls to the gen_server, which ensures they’re safe. This creates a bit of contention on the write operations, so instead, we’ll try to write a cache that only uses ETS for all operations, and the gen_server’s job is just to keep the ETS table alive. The simple conceptual model—a cache handling data like a key-value store—along with an implementation dangerously accessing ETS tables concurrently makes this is a great candidate to demonstrate stateful property ...

Get Property-Based Testing with PropEr, Erlang, and Elixir now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.