### Question:
LeaderOnlyLockManager implements Locks { @Override public Locks.Client newClient() { return new LeaderOnlyLockClient( localLocks.newClient() ); } LeaderOnlyLockManager( MemberId myself, Replicator replicator, LeaderLocator leaderLocator, Locks localLocks,
            ReplicatedLockTokenStateMachine lockTokenStateMachine ); @Override Locks.Client newClient(); @Override void accept( Visitor visitor ); @Override void close(); static final String LOCK_NOT_ON_LEADER_ERROR_MESSAGE; }### Answer:
@Test public void shouldIssueLocksOnLeader() throws Exception { MemberId me = member( 0 ); ReplicatedLockTokenStateMachine replicatedLockStateMachine = new ReplicatedLockTokenStateMachine( new InMemoryStateStorage( new ReplicatedLockTokenState() ) ); DirectReplicator replicator = new DirectReplicator( replicatedLockStateMachine ); LeaderLocator leaderLocator = mock( LeaderLocator.class ); when( leaderLocator.getLeader() ).thenReturn( me ); Locks locks = mock( Locks.class ); Locks.Client client = mock( Locks.Client.class ); when( locks.newClient() ).thenReturn( client ); LeaderOnlyLockManager lockManager = new LeaderOnlyLockManager( me, replicator, leaderLocator, locks, replicatedLockStateMachine ); lockManager.newClient().acquireExclusive( LockTracer.NONE, ResourceTypes.NODE, 0L ); }
@Test public void shouldNotIssueLocksOnNonLeader() throws Exception { MemberId me = member( 0 ); MemberId leader = member( 1 ); ReplicatedLockTokenStateMachine replicatedLockStateMachine = new ReplicatedLockTokenStateMachine( new InMemoryStateStorage( new ReplicatedLockTokenState() ) ); DirectReplicator replicator = new DirectReplicator( replicatedLockStateMachine ); LeaderLocator leaderLocator = mock( LeaderLocator.class ); when( leaderLocator.getLeader() ).thenReturn( leader ); Locks locks = mock( Locks.class ); Locks.Client client = mock( Locks.Client.class ); when( locks.newClient() ).thenReturn( client ); LeaderOnlyLockManager lockManager = new LeaderOnlyLockManager( me, replicator, leaderLocator, locks, replicatedLockStateMachine ); Locks.Client lockClient = lockManager.newClient(); try { lockClient.acquireExclusive( LockTracer.NONE, ResourceTypes.NODE, 0L ); fail( "Should have thrown exception" ); } catch ( AcquireLockTimeoutException e ) { } }