### Question:
PrepareStoreCopyRequestHandler extends SimpleChannelInboundHandler<PrepareStoreCopyRequest> { @Override protected void channelRead0( ChannelHandlerContext channelHandlerContext, PrepareStoreCopyRequest prepareStoreCopyRequest ) throws IOException { CloseablesListener closeablesListener = new CloseablesListener(); PrepareStoreCopyResponse response = PrepareStoreCopyResponse.error( PrepareStoreCopyResponse.Status.E_LISTING_STORE ); try { NeoStoreDataSource neoStoreDataSource = dataSourceSupplier.get(); if ( !hasSameStoreId( prepareStoreCopyRequest.getStoreId(), neoStoreDataSource ) ) { channelHandlerContext.write( ResponseMessageType.PREPARE_STORE_COPY_RESPONSE ); response = PrepareStoreCopyResponse.error( PrepareStoreCopyResponse.Status.E_STORE_ID_MISMATCH ); } else { CheckPointer checkPointer = neoStoreDataSource.getDependencyResolver().resolveDependency( CheckPointer.class ); closeablesListener.add( tryCheckpointAndAcquireMutex( checkPointer ) ); PrepareStoreCopyFiles prepareStoreCopyFiles = closeablesListener.add( prepareStoreCopyFilesProvider.prepareStoreCopyFiles( neoStoreDataSource ) ); StoreResource[] nonReplayable = prepareStoreCopyFiles.getAtomicFilesSnapshot(); for ( StoreResource storeResource : nonReplayable ) { streamingProtocol.stream( channelHandlerContext, storeResource ); } channelHandlerContext.write( ResponseMessageType.PREPARE_STORE_COPY_RESPONSE ); response = createSuccessfulResponse( checkPointer, prepareStoreCopyFiles ); } } finally { channelHandlerContext.writeAndFlush( response ).addListener( closeablesListener ); protocol.expect( CatchupServerProtocol.State.MESSAGE_TYPE ); } } PrepareStoreCopyRequestHandler( CatchupServerProtocol catchupServerProtocol, Supplier<NeoStoreDataSource> dataSourceSupplier,
            PrepareStoreCopyFilesProvider prepareStoreCopyFilesProvider );   }### Answer:
@Test public void shouldRetainLockWhileStreaming() throws Exception { ChannelPromise channelPromise = embeddedChannel.newPromise(); ChannelHandlerContext channelHandlerContext = mock( ChannelHandlerContext.class ); when( channelHandlerContext.writeAndFlush( any( PrepareStoreCopyResponse.class ) ) ).thenReturn( channelPromise ); ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); when( neoStoreDataSource.getStoreCopyCheckPointMutex() ).thenReturn( new StoreCopyCheckPointMutex( lock ) ); PrepareStoreCopyRequestHandler subjectHandler = createHandler(); LongSet indexIds = LongSets.immutable.of( 42 ); File[] files = new File[]{new File( "file" )}; long lastCheckpoint = 1; configureProvidedStoreCopyFiles( new StoreResource[0], files, indexIds, lastCheckpoint ); subjectHandler.channelRead0( channelHandlerContext, new PrepareStoreCopyRequest( STORE_ID_MATCHING ) ); assertEquals( 1, lock.getReadLockCount() ); channelPromise.setSuccess(); assertEquals( 0, lock.getReadLockCount() ); }