Package io.thekraken.grok.api

Source Code of io.thekraken.grok.api.ConcurrencyTest

package io.thekraken.grok.api;

import static org.junit.Assert.assertEquals;

import java.util.Collections;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

import oi.thekraken.grok.api.Match;

import static org.hamcrest.core.IsEqual.*;
import static org.hamcrest.core.IsNot.*;
import org.junit.Assert;
import org.junit.Test;

public class ConcurrencyTest {

  /**
   * We will test this by setting up two threads, asserting on the hash values
   * the instances generate for each thread
   * @throws InterruptedException
   * @throws ExecutionException
   */
  @Test
  public void test_001_concurrent_match() throws InterruptedException, ExecutionException {

    // Setup callable method that reports the hashcode for the thread
    int threadCount = 2;
    Callable<Integer> task = new Callable<Integer>() {
      @Override
      public Integer call() {
        return Match.getInstance().hashCode();
      }
    };

    // Create n tasks to execute
    List<Callable<Integer>> tasks = Collections.nCopies(threadCount, task);

    // Execute the task for both tasks
    ExecutorService es = Executors.newFixedThreadPool(threadCount);
    List<Future<Integer>> futures = es.invokeAll(tasks);
    int hash1 = futures.get(0).get();
    int hash2 = futures.get(1).get();

    // The two hashcodes must NOT be equal
    Assert.assertThat(hash1, not(equalTo(hash2)));
  }

  @Test
  public void test_002_match_within_instance() {
    // Verify that the instances are equal for the same thread
    Match m1 = Match.getInstance();
    Match m2 = Match.getInstance();
    assertEquals(m1, m2);
  }

}
TOP

Related Classes of io.thekraken.grok.api.ConcurrencyTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.