Synchronize through Common Lock

Existing explicit synchronization gives us one of the easiest seams to exploit. The most common form occurs when the code under test has a critical section that uses another class that is already thread-safe. In such cases, you can use the synchronization in the other class to suspend within the critical section. Consider the code in Listing 13-7.

Listing 13-7: An example for discussion on common lock synchronization

public final class HostInfoService { // Singleton  private static HostInfoService instance;  private HostInfoService() {   ...  }  public static synchronized HostInfoService getInstance() {    if (instance == null ) {      instance = new HostInfoService();    }    return ...

Get Quality Code: Software Testing Principles, Practices, and Patterns now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.