### Question:
TimedRepository implements Runnable { public void begin( KEY key ) throws ConcurrentAccessException { VALUE instance = factory.newInstance(); Entry existing; if ( (existing = repo.putIfAbsent( key, new Entry( instance ) )) != null ) { reaper.accept( instance ); throw new ConcurrentAccessException( String.format( "Cannot begin '%s', because %s with that key already exists.", key, existing ) ); } } TimedRepository( Factory<VALUE> provider, Consumer<VALUE> reaper, long timeout, Clock clock ); void begin( KEY key ); VALUE end( KEY key ); VALUE acquire( KEY key ); void release( KEY key ); Set<KEY> keys(); @Override void run();  }### Answer:
@Test public void shouldNotAllowBeginningWithDuplicateKey() throws Exception { repo.begin( 1L ); try { repo.begin( 1L ); fail( "Should not have been able to begin." ); } catch ( ConcurrentAccessException e ) { assertThat( e.getMessage(), containsString( "Cannot begin '1', because Entry" ) ); assertThat( e.getMessage(), containsString( " with that key already exists." ) ); } }
@Test public void usingDuplicateKeysShouldDisposeOfPreemptiveAllocatedValue() throws Exception { repo.begin( 1L ); try { repo.begin( 1L ); fail( "Should not have been able to begin." ); } catch ( ConcurrentAccessException e ) { assertThat( e.getMessage(), containsString( "Cannot begin '1', because Entry" ) ); assertThat( e.getMessage(), containsString( " with that key already exists." ) ); } assertThat( reapedValues, equalTo( asList( 1L ) ) ); }