Java Reference
In-Depth Information
12.1.1. Basic Unit Tests
The most basic unit tests for BoundedBuffer are similar to what we'd use in a sequential
context—create a bounded buffer, call its methods, and assert postconditions and invariants.
Some invariants that quickly come to mind are that a freshly created buffer should identify
itself as empty, and also as not full. A similar but slightly more complicated safety test is
to insert N elements into a buffer with capacity N (which should succeed without blocking),
and test that the buffer recognizes that it is full (and not empty). JUnit test methods for these
properties are shown in Listing 12.2 .
Listing 12.2. Basic Unit Tests for BoundedBuffer .
These simple test methods are entirely sequential. Including a set of sequential tests in your
test suite is often helpful, since they can disclose when a problem is not related to concur-
rency issues before you start looking for data races.
12.1.2. Testing Blocking Operations
Tests of essential concurrency properties require introducing more than one thread. Most
testing frameworks are not very concurrency-friendly: they rarely include facilities to create
threads or monitor them to ensure that they do not die unexpectedly. If a helper thread created
by a test case discovers a failure, the framework usually does not know with which test the
Search WWH ::




Custom Search