/*
 * Copyright 2012 to the original author or authors.
 *
 * 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 org.rioproject.resolver;

import junit.framework.Assert;
import org.junit.Test;
import org.rioproject.resolver.aether.AetherResolver;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;

/**
 * @author Dennis Reedy
 */
public class ITResolverConcurrencyTest {

    @Test
    public void testConcurrentAccess() throws ExecutionException, InterruptedException {
        Resolver r = new AetherResolver();
        ExecutorService resolverExecutor = Executors.newCachedThreadPool();
        System.out.println("Create 1000 threads");
        long t0 = System.currentTimeMillis();
        List<Future<String[]>> futures = new ArrayList<Future<String[]>>();
        for(int i=0; i<1000; i++) {
            futures.add(resolverExecutor.submit(new Request(r)));
        }
        Assert.assertTrue(futures.size() == 1000);
        for(Future<String[]> future : futures) {
            String[] classPath = future.get();
            Assert.assertTrue("Expected 7 jars, got " + classPath.length, classPath.length == 7);
        }
        long t1 = System.currentTimeMillis();
        System.out.println("Complete, took "+(t1-t0)+" millis");
    }

    @Test
    public void testConcurrentAccess2() throws ExecutionException, InterruptedException {
        Resolver r = new AetherResolver();
        ExecutorService resolverExecutor = Executors.newCachedThreadPool();
        System.out.println("Create 1000 threads");
        long t0 = System.currentTimeMillis();
        List<Future<String[]>> futures = new ArrayList<Future<String[]>>();
        RemoteRepository repository = new RemoteRepository();
        repository.setUrl("http://repo1.maven.org/maven2/");
        repository.setId("central");
        for(int i=0; i<1000; i++) {
            futures.add(resolverExecutor.submit(new Request(r, new RemoteRepository[]{repository})));
        }
        Assert.assertTrue(futures.size() == 1000);
        for(Future<String[]> future : futures) {
            String[] classPath = future.get();
            Assert.assertTrue("Expected 7 jars, got "+classPath.length, classPath.length==7);
        }
        long t1 = System.currentTimeMillis();
        System.out.println("Complete, took "+(t1-t0)+" millis");
    }

    class Request implements Callable<String[]> {
        Resolver resolver;
        RemoteRepository[] repositories;

        Request(Resolver resolver) {
            this.resolver = resolver;
        }

        Request(Resolver resolver, RemoteRepository[] repositories) {
            this.resolver = resolver;
            this.repositories = repositories;
        }

        public String[] call() throws ResolverException {
            String[] classPath;
            if(repositories==null)
                classPath = resolver.getClassPathFor("org.apache.maven:maven-settings-builder:3.0.3");
            else
                classPath = resolver.getClassPathFor("org.apache.maven:maven-settings-builder:3.0.3", repositories);
            return classPath;

        }
    }
}

@@ -1160,33 +1160,32 @@
 ntln(%22Create 100
-0
  threads%22);%0A    
@@ -1172,32 +1172,32 @@
  100 threads%22);%0A
+
         long t0 
@@ -1320,33 +1320,32 @@
 r(int i=0; i%3C100
-0
 ; i++) %7B%0A       
@@ -1452,33 +1452,32 @@
 es.size() == 100
-0
 );%0A        for(F
@@ -2042,17 +2042,16 @@
 eate 100
-0
  threads
@@ -2362,17 +2362,16 @@
 0; i%3C100
-0
 ; i++) %7B
@@ -2475,32 +2475,32 @@
 %7D)));%0A        %7D%0A
+
         Assert.a
@@ -2530,17 +2530,16 @@
 ) == 100
-0
 );%0A     

Create 100 threads instead of 1000 to verify concurrency