liveBook · Manning

Chapter 7: Chapter 7. Test-driving the unpredictable

What you will learn in chapter 7 of Test Driven: Practical TDD and Acceptance TDD for Java Developers

This introduction previews the challenges of test-driving time-dependent and multithreaded Java code.

After this chapter you will be able to

What it builds on

Section by section

Introduction

This introduction previews the challenges of test-driving time-dependent and multithreaded Java code.

7.1 Test-driving time-based functionality

This section introduces the testing challenge posed by code whose behavior depends on the current time.

Key terms: current time, time-based functionality

7.1.1 Example: logs and timestamps

The section uses a logging example to show why system time makes a naïve test unreliable and why a reusable way to fake time is preferable to local workarounds.

Key terms: timestamp, system clock, current system time, method parameter, overridden method, generic solution, system-time access

7.1.2 Abstracting system time

This section develops a configurable system-time abstraction that lets tests substitute controlled time sources for the real clock.

Key terms: SystemTime, System#currentTimeMillis, Calendar#getInstance, TimeSource, Strategy design pattern, teardown, default TimeSource, fixed time

7.1.3 Testing log output with faked system time

The section makes a log-formatter test time-independent using a fake time source and examines how to verify both the timestamp’s value and its format.

Key terms: SystemTime, TimeSource, SystemTime#asDate, fake system time, DateFormat, common log format, regular expression, timestamp

7.2 Test-driving multithreaded code

This section explains how concurrency relates to sequential execution and recommends test-driving sequential logic before verifying concurrent behavior.

Key terms: concurrent programming, thread scheduler, sequential programs, sequential logic, concurrent behavior, synchronization

7.2.1 What are we testing for?

This section explains what concurrency tests should target and why nondeterministic execution makes exhaustive testing impractical.

Key terms: concurrent behavior, error-situation space, repeated, long test runs, Heisenbugs, asynchronous execution, deadlock, starvation, liveness, thread scheduler

7.2.2 Thread-safety

This section explains thread-safety testing limitations and uses a shared counter to demonstrate how synchronized test starts and finishes can make concurrency defects easier to expose.

Key terms: thread-safe, concurrently, byte-code instructions, properties, invariants, intelligent brute-force test, false negatives, thread scheduling, Counter, increment

7.2.3 Blocking operations

This section explains how to test a blocking method and why thread state is unreliable evidence of blocking.

Key terms: blocking method, commit, BlackMarket#buyTicket, sellTicket, InterruptedException, Thread#sleep, Thread#getState, waiting state, spurious wakeups, Object#wait

7.2.4 Starting and stopping threads

This section shows how to test that a server starts and stops its background thread using an injected ThreadFactory and observable thread lifecycle signals.

Key terms: ThreadFactory, dependency injection, Thread, latches, run, waitForStarted, waitForStopped, Server, start latch, stop latch

7.2.5 Asynchronous execution

This section explains ways to test work performed asynchronously, including isolating typed Runnables and waiting or polling for results.

Key terms: typed Runnable, ThreadFactory, asynchronous service, wait time, RetriedAssert, polling, assertion, timeout

7.2.6 Synchronization between threads

This section explains thread synchronization and a divide-and-conquer approach to testing collaboration between threads.

Key terms: worker threads, synchronization point, invariants, divide-and-conquer, test doubles, synchronization problems

7.3 Standard synchronization objects

This section recommends standard Java concurrency abstractions for necessary interthread synchronization instead of constructing solutions from low-level primitives.

Key terms: shared-nothing, interthread communication, java.util.concurrent, low-level locks, wait/notify

7.3.1 Semaphores

This section explains how semaphore permits limit concurrent access to work or shared resources.

Key terms: semaphore, permit, acquire, release, counting semaphore, single permit, synchronization lock, shared resource

7.3.2 Latches

This section explains latches as one-use synchronization points and describes how CountDownLatch releases waiting threads.

Key terms: latch, CountDownLatch, countdown calls, synchronization points

7.3.3 Barriers

This section explains reusable barriers for coordinating threads and the Exchanger for swapping data between two threads.

Key terms: barrier, CyclicBarrier, latch, await, Exchanger, buffers

7.3.4 Futures

This section explains how a Future lets a calling thread obtain the result of a cancellable asynchronous task.

Key terms: Future, FutureTask, get, asynchronous tasks, timeout

7.4 Summary

Concepts explained in this chapter

← Chapter 6: Chapter 6. Test-driving data access Chapter 8: Chapter 8. Test-driving Swing →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.