/*
 * Copyright 2014-2019 Real Logic Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.aeron.archive;

import io.aeron.archive.client.AeronArchive;
import io.aeron.driver.MediaDriver;
import org.junit.Test;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;

public class ArchiveTest
{
    @Test
    public void shouldGenerateRecordingName()
    {
        final long recordingId = 1L;
        final int segmentIndex = 2;
        final String expected = "1-2.rec";

        final String actual = Archive.segmentFileName(recordingId, segmentIndex);

        assertThat(actual, is(expected));
    }

    @Test
    public void shouldAllowMultipleConnectionsInParallel() throws InterruptedException
    {
        final int numberOfArchiveClients = 5;
        final CountDownLatch latch = new CountDownLatch(numberOfArchiveClients);
        final ExecutorService executorService = Executors.newFixedThreadPool(numberOfArchiveClients);

        final MediaDriver.Context driverCtx = new MediaDriver.Context();
        final Archive.Context archiveCtx = new Archive.Context();

        try (ArchivingMediaDriver driver = ArchivingMediaDriver.launch(driverCtx, archiveCtx))
        {
            for (int i = 0; i < numberOfArchiveClients; i++)
            {
                executorService.execute(() ->
                {
                    AeronArchive.connect();
                    latch.countDown();
                });
            }

            latch.await(10, TimeUnit.SECONDS);

            assertThat(latch.getCount(), is(0L));
        }
        finally
        {
            archiveCtx.deleteArchiveDirectory();
            driverCtx.deleteAeronDirectory();
        }
    }
}

@@ -716,148 +716,81 @@
 org.
-junit.Test;%0A%0Aimport java.util.concurrent.CountDownLatch;%0Aimport java.util.concurrent.ExecutorService;%0Aimport java.util.concurrent.Executors;
+agrona.concurrent.ManyToOneConcurrentLinkedQueue;%0Aimport org.junit.Test;%0A
 %0Aimp
@@ -814,24 +814,17 @@
 current.
-TimeUnit
+*
 ;%0A%0Aimpor
@@ -1571,16 +1571,135 @@
 ients);%0A
+        final ManyToOneConcurrentLinkedQueue%3CAeronArchive%3E archiveClientQueue = new ManyToOneConcurrentLinkedQueue%3C%3E();
 %0A       
@@ -1865,22 +1865,22 @@
 aDriver 
-driver
+ignore
  = Archi
@@ -2051,16 +2051,37 @@
 execute(
+%0A                    
 () -%3E%0A  
@@ -2086,34 +2086,42 @@
                 
-%7B%0A
+    %7B%0A    
                 
@@ -2124,16 +2124,39 @@
         
+archiveClientQueue.add(
 AeronArc
@@ -2169,16 +2169,17 @@
 onnect()
+)
 ;%0A      
@@ -2188,24 +2188,28 @@
             
+    
 latch.countD
@@ -2211,24 +2211,28 @@
 ountDown();%0A
+    
             
@@ -2380,32 +2380,256 @@
 nally%0A        %7B%0A
+            executorService.shutdownNow();%0A%0A            AeronArchive archiveClient;%0A            while (null != (archiveClient = archiveClientQueue.poll()))%0A            %7B%0A                archiveClient.close();%0A            %7D%0A%0A
             arch

Tidy up test after merge of PR #634.