Java Code Examples for java.util.concurrent.ExecutorCompletionService#submit()

The following examples show how to use java.util.concurrent.ExecutorCompletionService#submit() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TStreamTest.java    From quarks with Apache License 2.0 6 votes vote down vote up
/**
 * Submit multiple jobs concurrently using PeriodicSource.
 */
@Test
public void testMultiTopologyPollWithError() throws Exception {

    int executions = 4;
    ExecutorCompletionService<Boolean> completer = new ExecutorCompletionService<>(
            Executors.newFixedThreadPool(executions));
    for (int i = 0; i < executions; i++) {
        completer.submit(() -> {
            Topology t = newTopology();
            AtomicLong n = new AtomicLong(0);
            TStream<Long> s = t.poll(() -> n.incrementAndGet(), 10, TimeUnit.MILLISECONDS);
            // Throw on the 8th tuple
            s.sink((tuple) -> { if (8 == n.get()) throw new RuntimeException("Expected Test Exception");});
            // Expect 7 tuples out of 8
            Condition<Long> tc = t.getTester().tupleCount(s, 7);
            complete(t, tc);
            return true;
        });
    }
    waitForCompletion(completer, executions);
}
 
Example 2
Source File: TestIdLock.java    From hbase with Apache License 2.0 6 votes vote down vote up
@Test
public void testMultipleClients() throws Exception {
  ExecutorService exec = Executors.newFixedThreadPool(NUM_THREADS);
  try {
    ExecutorCompletionService<Boolean> ecs = new ExecutorCompletionService<>(exec);
    for (int i = 0; i < NUM_THREADS; ++i)
      ecs.submit(new IdLockTestThread("client_" + i));
    for (int i = 0; i < NUM_THREADS; ++i) {
      Future<Boolean> result = ecs.take();
      assertTrue(result.get());
    }
    idLock.assertMapEmpty();
  } finally {
    exec.shutdown();
    exec.awaitTermination(5000, TimeUnit.MILLISECONDS);
  }
}
 
Example 3
Source File: ExecutorCompletionServiceTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * Submitting a null callable throws NPE
 */
public void testSubmitNPE() {
    final ExecutorService e = Executors.newCachedThreadPool();
    final ExecutorCompletionService ecs = new ExecutorCompletionService(e);
    try (PoolCleaner cleaner = cleaner(e)) {
        Callable c = null;
        try {
            ecs.submit(c);
            shouldThrow();
        } catch (NullPointerException success) {}
    }
}
 
Example 4
Source File: ExecutorCompletionServiceTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * Submitting a null runnable throws NPE
 */
public void testSubmitNPE2() {
    final ExecutorService e = Executors.newCachedThreadPool();
    final ExecutorCompletionService ecs = new ExecutorCompletionService(e);
    try (PoolCleaner cleaner = cleaner(e)) {
        Runnable r = null;
        try {
            ecs.submit(r, Boolean.TRUE);
            shouldThrow();
        } catch (NullPointerException success) {}
    }
}
 
Example 5
Source File: TestIdReadWriteLockWithObjectPool.java    From hbase with Apache License 2.0 5 votes vote down vote up
@Test
public void testMultipleClients() throws Exception {
  ExecutorService exec = Executors.newFixedThreadPool(NUM_THREADS);
  try {
    ExecutorCompletionService<Boolean> ecs = new ExecutorCompletionService<>(exec);
    for (int i = 0; i < NUM_THREADS; ++i)
      ecs.submit(new IdLockTestThread("client_" + i));
    for (int i = 0; i < NUM_THREADS; ++i) {
      Future<Boolean> result = ecs.take();
      assertTrue(result.get());
    }
    int entryPoolSize = idLock.purgeAndGetEntryPoolSize();
    LOG.debug("Size of entry pool after gc and purge: " + entryPoolSize);
    ReferenceType refType = idLock.getReferenceType();
    switch (refType) {
    case WEAK:
      // make sure the entry pool will be cleared after GC and purge call
      assertEquals(0, entryPoolSize);
      break;
    case SOFT:
      // make sure the entry pool won't be cleared when JVM memory is enough
      // even after GC and purge call
      assertEquals(NUM_IDS, entryPoolSize);
      break;
    default:
      break;
    }
  } finally {
    exec.shutdown();
    exec.awaitTermination(5000, TimeUnit.MILLISECONDS);
  }
}
 
Example 6
Source File: UpdateLog.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/** Returns the Future to wait on, or null if no replay was needed */
public Future<RecoveryInfo> applyBufferedUpdates() {
  // recovery trips this assert under some race - even when
  // it checks the state first
  // assert state == State.BUFFERING;

  // block all updates to eliminate race conditions
  // reading state and acting on it in the update processor
  versionInfo.blockUpdates();
  try {
    cancelApplyBufferUpdate = false;
    if (state != State.BUFFERING) return null;

    synchronized (this) {
      // handle case when no updates were received.
      if (bufferTlog == null) {
        state = State.ACTIVE;
        return null;
      }
      bufferTlog.incref();
    }

    state = State.APPLYING_BUFFERED;
  } finally {
    versionInfo.unblockUpdates();
  }

  if (recoveryExecutor.isShutdown()) {
    throw new RuntimeException("executor is not running...");
  }
  ExecutorCompletionService<RecoveryInfo> cs = new ExecutorCompletionService<>(recoveryExecutor);
  LogReplayer replayer = new LogReplayer(Collections.singletonList(bufferTlog), true);
  return cs.submit(() -> {
    replayer.run();
    dropBufferTlog();
  }, recoveryInfo);
}
 
Example 7
Source File: ControlDataSet.java    From spliceengine with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public DataSet<V> union(List<DataSet<V>> dataSetList, OperationContext operationContext) {
    try {
        ExecutorService es = SIDriver.driver().getExecutorService();
        ExecutorCompletionService<Iterator<V>> completionService = new ExecutorCompletionService<>(es);
        NonOrderPreservingFutureIterator<V> futureIterator = new NonOrderPreservingFutureIterator<>(completionService, dataSetList.size());
        for (DataSet<V> aSet: dataSetList) {
            completionService.submit(new NonLazy(((DataSet<V>)aSet).toLocalIterator()));
        }
        return new ControlDataSet<>(futureIterator);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
 
Example 8
Source File: Test_ThreadsManager.java    From ats-framework with Apache License 2.0 5 votes vote down vote up
@Test
public void positiveTest() {

    // create the threads manager
    this.threadsManager = new ThreadsManager();

    // create all the threads
    ThreadPoolExecutor executor = ( ThreadPoolExecutor ) Executors.newCachedThreadPool();
    executor.setKeepAliveTime( 0, TimeUnit.SECONDS );
    ExecutorCompletionService<Object> executionService = new ExecutorCompletionService<Object>( executor );

    IterationListener listener = new IterationListener();

    msg( log, "Ask Java to create all threads" );
    for( int j = 0; j < N_THREADS; j++ ) {
        executionService.submit( new TestWorker( N_ITERATIONS, threadsManager, listener ), null );
    }

    // run all iterations
    for( int i = 0; i < N_ITERATIONS; i++ ) {
        msg( log, "ITERATION " + i + "\n\n" );
        runThisIteration();

        waitForIterationCompletion();
    }

    // it may take a little while all threads are gone
    try {
        Thread.sleep( 1000 );
    } catch( InterruptedException e ) {}

    // check if there are any remaining threads started by this test
    checkAllRemainingThreads();
}
 
Example 9
Source File: ExecutorCompletionServiceTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * Take returns the same future object returned by submit
 */
public void testTake2() throws InterruptedException {
    final ExecutorService e = Executors.newCachedThreadPool();
    final ExecutorCompletionService ecs = new ExecutorCompletionService(e);
    try (PoolCleaner cleaner = cleaner(e)) {
        Callable c = new StringTask();
        Future f1 = ecs.submit(c);
        Future f2 = ecs.take();
        assertSame(f1, f2);
    }
}
 
Example 10
Source File: ExecutorCompletionServiceTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * A taken submitted task is completed
 */
public void testTake() throws InterruptedException {
    final ExecutorService e = Executors.newCachedThreadPool();
    final ExecutorCompletionService ecs = new ExecutorCompletionService(e);
    try (PoolCleaner cleaner = cleaner(e)) {
        Callable c = new StringTask();
        ecs.submit(c);
        Future f = ecs.take();
        assertTrue(f.isDone());
    }
}
 
Example 11
Source File: ControlDataSet.java    From spliceengine with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public DataSet<V> union(DataSet<V> dataSet, OperationContext operationContext) {
    try {
        ExecutorService es = SIDriver.driver().getExecutorService();
        ExecutorCompletionService<Iterator<V>> completionService = new ExecutorCompletionService<>(es);
        NonOrderPreservingFutureIterator<V> futureIterator = new NonOrderPreservingFutureIterator<>(completionService, 2);
        completionService.submit(new NonLazy(iterator));
        completionService.submit(new NonLazy(((ControlDataSet<V>) dataSet).iterator));
        return new ControlDataSet<>(futureIterator);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
 
Example 12
Source File: BlobOutputStreamTests.java    From azure-storage-android with Apache License 2.0 4 votes vote down vote up
@Test
public void testWritesDoubleConcurrency() throws URISyntaxException, StorageException, IOException,
        InterruptedException {
    String blobName = BlobTestHelper.generateRandomBlobNameWithPrefix("concurrency");
    CloudBlockBlob blockBlob = this.container.getBlockBlobReference(blobName);

    // setup the blob output stream with a concurrency of 5
    BlobRequestOptions options = new BlobRequestOptions();
    options.setConcurrentRequestCount(5);
    BlobOutputStream blobOutputStream = blockBlob.openOutputStream(null, options, null);

    // set up the execution completion service
    ExecutorService threadExecutor = Executors.newFixedThreadPool(5);
    ExecutorCompletionService<Void> completion = new ExecutorCompletionService<Void>(threadExecutor);
    
    int tasks = 10;
    int writes = 10;
    int length = 512;
    
    // submit tasks to write and flush many blocks
    for (int i = 0; i < tasks; i++) {
        completion.submit(new WriteTask(blobOutputStream, length, writes, 4 /*flush period*/));
    }

    // wait for all tasks to complete
    for (int i = 0; i < tasks; i++) {
        completion.take();
    }

    // shut down the thread executor for this method
    threadExecutor.shutdown();

    // check that blocks were committed
    ArrayList<BlockEntry> blocks = blockBlob.downloadBlockList(BlockListingFilter.UNCOMMITTED, null, null, null);
    assertTrue(blocks.size() != 0);
    
    // close the stream and check that the blob is the expected length
    blobOutputStream.close();
    blockBlob.downloadAttributes();
    assertTrue(blockBlob.getProperties().getLength() == length*writes*tasks);
}
 
Example 13
Source File: TestInterProcessSemaphore.java    From curator with Apache License 2.0 4 votes vote down vote up
@Test
public void testReleaseInChunks() throws Exception
{
    final Timing timing = new Timing();
    final int MAX_LEASES = 11;
    final int THREADS = 100;

    final CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));
    client.start();
    try
    {
        final Stepper latch = new Stepper();
        final Random random = new Random();
        final Counter counter = new Counter();
        ExecutorService service = Executors.newCachedThreadPool();
        ExecutorCompletionService<Object> completionService = new ExecutorCompletionService<Object>(service);
        for ( int i = 0; i < THREADS; ++i )
        {
            completionService.submit
                (
                    new Callable<Object>()
                    {
                        @Override
                        public Object call() throws Exception
                        {
                            InterProcessSemaphoreV2 semaphore = new InterProcessSemaphoreV2(client, "/test", MAX_LEASES);
                            Lease lease = semaphore.acquire(timing.forWaiting().seconds(), TimeUnit.SECONDS);
                            if ( lease == null )
                            {
                                throw new Exception("timed out");
                            }
                            try
                            {
                                synchronized(counter)
                                {
                                    ++counter.currentCount;
                                    if ( counter.currentCount > counter.maxCount )
                                    {
                                        counter.maxCount = counter.currentCount;
                                    }
                                    counter.notifyAll();
                                }

                                latch.await();
                            }
                            finally
                            {
                                synchronized(counter)
                                {
                                    --counter.currentCount;
                                }
                                semaphore.returnLease(lease);
                            }
                            return null;
                        }
                    }
                );
        }

        int remaining = THREADS;
        while ( remaining > 0 )
        {
            int times = Math.min(random.nextInt(5) + 1, remaining);
            latch.countDown(times);
            remaining -= times;
            Thread.sleep(random.nextInt(100) + 1);
        }

        for ( int i = 0; i < THREADS; ++i )
        {
            completionService.take();
        }

        timing.sleepABit();
        synchronized(counter)
        {
            Assert.assertTrue(counter.currentCount == 0);
            Assert.assertTrue(counter.maxCount > 0);
            Assert.assertTrue(counter.maxCount <= MAX_LEASES);
            System.out.println(counter.maxCount);
        }
    }
    finally
    {
        TestCleanState.closeAndTestClean(client);
    }
}
 
Example 14
Source File: TestS3ADeleteManyFiles.java    From big-c with Apache License 2.0 4 votes vote down vote up
@Test
public void testBulkRenameAndDelete() throws Throwable {
  final Path scaleTestDir = getTestPath();
  final Path srcDir = new Path(scaleTestDir, "src");
  final Path finalDir = new Path(scaleTestDir, "final");
  final long count = getOperationCount();
  ContractTestUtils.rm(fs, scaleTestDir, true, false);

  fs.mkdirs(srcDir);
  fs.mkdirs(finalDir);

  int testBufferSize = fs.getConf()
      .getInt(ContractTestUtils.IO_CHUNK_BUFFER_SIZE,
          ContractTestUtils.DEFAULT_IO_CHUNK_BUFFER_SIZE);
  // use Executor to speed up file creation
  ExecutorService exec = Executors.newFixedThreadPool(16);
  final ExecutorCompletionService<Boolean> completionService =
      new ExecutorCompletionService<Boolean>(exec);
  try {
    final byte[] data = ContractTestUtils.dataset(testBufferSize, 'a', 'z');

    for (int i = 0; i < count; ++i) {
      final String fileName = "foo-" + i;
      completionService.submit(new Callable<Boolean>() {
        @Override
        public Boolean call() throws IOException {
          ContractTestUtils.createFile(fs, new Path(srcDir, fileName),
              false, data);
          return fs.exists(new Path(srcDir, fileName));
        }
      });
    }
    for (int i = 0; i < count; ++i) {
      final Future<Boolean> future = completionService.take();
      try {
        if (!future.get()) {
          LOG.warn("cannot create file");
        }
      } catch (ExecutionException e) {
        LOG.warn("Error while uploading file", e.getCause());
        throw e;
      }
    }
  } finally {
    exec.shutdown();
  }

  int nSrcFiles = fs.listStatus(srcDir).length;
  fs.rename(srcDir, finalDir);
  assertEquals(nSrcFiles, fs.listStatus(finalDir).length);
  ContractTestUtils.assertPathDoesNotExist(fs, "not deleted after rename",
      new Path(srcDir, "foo-" + 0));
  ContractTestUtils.assertPathDoesNotExist(fs, "not deleted after rename",
      new Path(srcDir, "foo-" + count / 2));
  ContractTestUtils.assertPathDoesNotExist(fs, "not deleted after rename",
      new Path(srcDir, "foo-" + (count - 1)));
  ContractTestUtils.assertPathExists(fs, "not renamed to dest dir",
      new Path(finalDir, "foo-" + 0));
  ContractTestUtils.assertPathExists(fs, "not renamed to dest dir",
      new Path(finalDir, "foo-" + count/2));
  ContractTestUtils.assertPathExists(fs, "not renamed to dest dir",
      new Path(finalDir, "foo-" + (count-1)));

  ContractTestUtils.assertDeleted(fs, finalDir, true, false);
}
 
Example 15
Source File: TestInterProcessSemaphore.java    From xian with Apache License 2.0 4 votes vote down vote up
@Test
public void testReleaseInChunks() throws Exception
{
    final Timing timing = new Timing();
    final int MAX_LEASES = 11;
    final int THREADS = 100;

    final CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));
    client.start();
    try
    {
        final Stepper latch = new Stepper();
        final Random random = new Random();
        final Counter counter = new Counter();
        ExecutorService service = Executors.newCachedThreadPool();
        ExecutorCompletionService<Object> completionService = new ExecutorCompletionService<Object>(service);
        for ( int i = 0; i < THREADS; ++i )
        {
            completionService.submit
                (
                    new Callable<Object>()
                    {
                        @Override
                        public Object call() throws Exception
                        {
                            InterProcessSemaphoreV2 semaphore = new InterProcessSemaphoreV2(client, "/test", MAX_LEASES);
                            Lease lease = semaphore.acquire(timing.forWaiting().seconds(), TimeUnit.SECONDS);
                            if ( lease == null )
                            {
                                throw new Exception("timed out");
                            }
                            try
                            {
                                synchronized(counter)
                                {
                                    ++counter.currentCount;
                                    if ( counter.currentCount > counter.maxCount )
                                    {
                                        counter.maxCount = counter.currentCount;
                                    }
                                    counter.notifyAll();
                                }

                                latch.await();
                            }
                            finally
                            {
                                synchronized(counter)
                                {
                                    --counter.currentCount;
                                }
                                semaphore.returnLease(lease);
                            }
                            return null;
                        }
                    }
                );
        }

        int remaining = THREADS;
        while ( remaining > 0 )
        {
            int times = Math.min(random.nextInt(5) + 1, remaining);
            latch.countDown(times);
            remaining -= times;
            Thread.sleep(random.nextInt(100) + 1);
        }

        for ( int i = 0; i < THREADS; ++i )
        {
            completionService.take();
        }

        timing.sleepABit();
        synchronized(counter)
        {
            Assert.assertTrue(counter.currentCount == 0);
            Assert.assertTrue(counter.maxCount > 0);
            Assert.assertTrue(counter.maxCount <= MAX_LEASES);
            System.out.println(counter.maxCount);
        }
    }
    finally
    {
        client.close();
    }
}
 
Example 16
Source File: TestLogFile.java    From mt-flume with Apache License 2.0 4 votes vote down vote up
@Test
public void testGroupCommit() throws Exception {
  final FlumeEvent eventIn = TestUtils.newPersistableEvent(250);
  final CyclicBarrier barrier = new CyclicBarrier(20);
  ExecutorService executorService = Executors.newFixedThreadPool(20);
  ExecutorCompletionService<Void> completionService = new
    ExecutorCompletionService<Void>(executorService);
  final LogFile.Writer writer = logFileWriter;
  final AtomicLong txnId = new AtomicLong(++transactionID);
  for (int i = 0; i < 20; i++) {
    completionService.submit(new Callable<Void>() {
      @Override
      public Void call() {
        try {
          Put put = new Put(txnId.incrementAndGet(),
            WriteOrderOracle.next(), eventIn);
          ByteBuffer bytes = TransactionEventRecord.toByteBuffer(put);
          writer.put(bytes);
          writer.commit(TransactionEventRecord.toByteBuffer(
            new Commit(txnId.get(), WriteOrderOracle.next())));
          barrier.await();
          writer.sync();
        } catch (Exception ex) {
          Throwables.propagate(ex);
        }
        return null;
      }
    });
  }

  for(int i = 0; i < 20; i++) {
    completionService.take().get();
  }

  //At least 250*20, but can be higher due to serialization overhead
  Assert.assertTrue(logFileWriter.position() >= 5000);
  Assert.assertEquals(1, writer.getSyncCount());
  Assert.assertTrue(logFileWriter.getLastCommitPosition() ==
    logFileWriter.getLastSyncPosition());

  executorService.shutdown();

}
 
Example 17
Source File: TestInterProcessMutexBase.java    From xian with Apache License 2.0 4 votes vote down vote up
@Test
public void testKilledSession() throws Exception
{
    final Timing timing = new Timing();

    CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new ExponentialBackoffRetry(100, 3));
    client.start();
    try
    {
        final InterProcessLock mutex1 = makeLock(client);
        final InterProcessLock mutex2 = makeLock(client);

        final Semaphore semaphore = new Semaphore(0);
        ExecutorCompletionService<Object> service = new ExecutorCompletionService<Object>(Executors.newFixedThreadPool(2));
        service.submit
            (
                new Callable<Object>()
                {
                    @Override
                    public Object call() throws Exception
                    {
                        mutex1.acquire();
                        semaphore.release();
                        Thread.sleep(1000000);
                        return null;
                    }
                }
            );

        service.submit
            (
                new Callable<Object>()
                {
                    @Override
                    public Object call() throws Exception
                    {
                        mutex2.acquire();
                        semaphore.release();
                        Thread.sleep(1000000);
                        return null;
                    }
                }
            );

        Assert.assertTrue(timing.acquireSemaphore(semaphore, 1));
        KillSession.kill(client.getZookeeperClient().getZooKeeper(), server.getConnectString());
        Assert.assertTrue(timing.acquireSemaphore(semaphore, 1));
    }
    finally
    {
        client.close();
    }
}
 
Example 18
Source File: DynamicTMThreading.java    From phrasal with GNU General Public License v3.0 4 votes vote down vote up
public static void main(String[] args) throws IOException {
  if (args.length != 3) {
    System.err.printf("Usage: java %s tm_file source_file n%n", DynamicTMThreading.class.getName());
    System.exit(-1);
  }
  String fileName = args[0];
  String inputFile = args[1];
  int numThreads = Integer.parseInt(args[2]);
  TimeKeeper timer = TimingUtils.start();
  DynamicTranslationModel<String> tm = DynamicTranslationModel.load(fileName, true, "benchmark");
  tm.setReorderingScores(false);
  timer.mark("Load");
  tm.createQueryCache(FeatureTemplate.DENSE_EXT);
  timer.mark("Cache creation");

  // Read the source at once for accurate timing of queries
  List<Sequence<IString>> sourceFile = IStrings.tokenizeFile(inputFile);
  timer.mark("Source file loading");
  
  final ThreadPoolExecutor threadPool = (ThreadPoolExecutor) 
      Executors.newFixedThreadPool(numThreads);
  final ExecutorCompletionService<List<ConcreteRule<IString,String>>> workQueue = 
      new ExecutorCompletionService<>(threadPool);
  
  long startTime = TimingUtils.startTime();
  int numRules = 0;
  final InputProperties inProps = new InputProperties();
  for (final Sequence<IString> source : sourceFile) {
    workQueue.submit(new Callable<List<ConcreteRule<IString,String>>>() {
      @Override
      public List<ConcreteRule<IString, String>> call() throws Exception {
        return tm.getRules(source, inProps, 0, null);
      }   
    });
  }
  try {
    for (int k = 0; k < sourceFile.size(); ++k) {
      List<ConcreteRule<IString,String>> result = workQueue.take().get();
      numRules += result.size();
    }
  } catch (InterruptedException | ExecutionException e) {
    e.printStackTrace();
    throw new RuntimeException("Could not read results");
  } finally {
    threadPool.shutdown();
  }
  double queryTimeMillis = TimingUtils.elapsedMillis(startTime);
  timer.mark("Query");

  threadPool.shutdown();
  
  System.out.printf("TM src cardinality: %d%n", tm.maxLengthSource());
  System.out.printf("TM tgt cardinality: %d%n", tm.maxLengthTarget());
  System.out.println("===========");
  System.out.printf("#source segments:   %d%n", sourceFile.size());
  System.out.printf("Timing: %s%n", timer);
  System.out.printf("Time/segment: %.2fms%n", queryTimeMillis / (double) sourceFile.size());
  System.out.printf("#rules: %d%n", numRules);
  System.out.printf("#segments: %d%n", sourceFile.size());
}
 
Example 19
Source File: RoomManagerTest.java    From kurento-room with Apache License 2.0 4 votes vote down vote up
@Test
public void addMediaFilterInParallel() throws InterruptedException, ExecutionException {
  joinManyUsersOneRoom();

  final FaceOverlayFilter filter = new FaceOverlayFilter.Builder(pipeline).build();
  assertNotNull("FaceOverlayFiler is null", filter);
  assertThat("Filter returned by the builder is not the same as the mocked one", filter,
      is(faceFilter));

  final String participantId0 = usersParticipantIds.get(users[0]);

  ExecutorService threadPool = Executors.newFixedThreadPool(1);
  ExecutorCompletionService<Void> exec = new ExecutorCompletionService<>(threadPool);
  exec.submit(new Callable<Void>() {
    @Override
    public Void call() throws Exception {
      System.out.println("Starting execution of addMediaElement");
      manager.addMediaElement(participantId0, filter);
      return null;
    }
  });

  Thread.sleep(10);

  assertEquals("SDP answer doesn't match", SDP_WEB_ANSWER,
      manager.publishMedia(participantId0, true, SDP_WEB_OFFER, false));

  assertThat(manager.getPublishers(roomx).size(), is(1));

  boolean firstSubscriber = true;
  for (String pid : usersParticipantIds.values()) {
    if (pid.equals(participantId0)) {
      continue;
    }
    assertEquals("SDP answer doesn't match", SDP_WEB_ANSWER,
        manager.subscribe(users[0], SDP_WEB_OFFER, pid));
    if (firstSubscriber) {
      firstSubscriber = false;
      try {
        exec.take().get();
        System.out
        .println("Execution of addMediaElement ended (just after first peer subscribed)");
      } finally {
        threadPool.shutdownNow();
      }
    }
  }
  assertThat(manager.getSubscribers(roomx).size(), is(users.length - 1));

  verify(faceFilter, times(1)).connect(passThru, faceFilterConnectCaptor.getValue());
  verify(endpoint, times(1)).connect(faceFilter, webRtcConnectCaptor.getValue());

  Set<UserParticipant> remainingUsers = manager.leaveRoom(participantId0);
  Set<UserParticipant> roomParticipants = manager.getParticipants(roomx);
  assertEquals(roomParticipants, remainingUsers);
  assertThat(roomParticipants, not(hasItem(usersParticipants.get(users[0]))));
  assertThat(manager.getPublishers(roomx).size(), is(0));

  // peers are automatically unsubscribed
  assertThat(manager.getSubscribers(roomx).size(), is(0));
}
 
Example 20
Source File: AbstractExperimentRunner.java    From quaerite with Apache License 2.0 4 votes vote down vote up
void runExperiment(Experiment experiment, List<Scorer> scorers,
                   int maxRows, ExperimentDB experimentDB, JudgmentList judgmentList,
                   String judgmentListId, boolean logResults)
        throws SQLException, IOException, SearchClientException {
    if (experimentDB.hasScores(experiment.getName())) {
        LOG.info("Already has scores for " + experiment.getName() + "; skipping.  " +
                "Use the -freshStart commandline option to clear all scores");
        return;
    }
    experimentDB.initScoreTable(scorers);
    SearchClient searchClient = SearchClientFactory.getClient(experiment.getSearchServerUrl());

    if (StringUtils.isBlank(experimentConfig.getIdField())) {
        LOG.info("default document 'idField' not set in experiment config. " +
                "Will use default: '"
                + searchClient.getDefaultIdField() + "'");
        experimentConfig.setIdField(searchClient.getDefaultIdField());
    }

    JudgmentList validated = searchServerValidatedMap.get(
            experiment.getSearchServerUrl() +
                    "_" + judgmentListId);
    if (validated == null) {
        validated = validate(searchClient, judgmentList);
        searchServerValidatedMap.put(experiment.getSearchServerUrl()
                + "_" + judgmentListId, validated);
    }
    ExecutorService executorService = Executors.newFixedThreadPool(
            experimentConfig.getNumThreads());
    ExecutorCompletionService<Integer> executorCompletionService =
            new ExecutorCompletionService<>(executorService);
    ArrayBlockingQueue<Judgments> queue = new ArrayBlockingQueue<>(
            validated.getJudgmentsList().size() +
                    experimentConfig.getNumThreads());

    queue.addAll(validated.getJudgmentsList());
    for (int i = 0; i < experimentConfig.getNumThreads(); i++) {
        queue.add(POISON);
    }

    for (int i = 0; i < experimentConfig.getNumThreads(); i++) {
        executorCompletionService.submit(
                new QueryRunner(experimentConfig.getIdField(), maxRows,
                        queue, experiment, experimentDB, scorers));
    }

    int completed = 0;
    while (completed < experimentConfig.getNumThreads()) {
        try {
            Future<Integer> future = executorCompletionService.take();
            future.get();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            completed++;
        }
    }
    executorService.shutdown();
    executorService.shutdownNow();
    //insertScores(experimentDB, experimentName, scoreAggregators);
    experimentDB.insertScoresAggregated(experiment.getName(), scorers);
    if (logResults) {
        logResults(experiment.getName(), scorers);
    }
}