home | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Java example source code file (SingleServiceIntegrationTest.java)

This example Java source code file (SingleServiceIntegrationTest.java) is included in the alvinalexander.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Learn more about this Java project at its project page.

Java - Java tags/keywords

asyncservice, atomicinteger, countdownlatch, executionexception, executorservice, future, interruptedexception, override, singleserviceintegrationtest, testcase, threading, threads, timeoutexception

The SingleServiceIntegrationTest.java Java example source code

package com.google.inject.service;

import junit.framework.TestCase;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicInteger;

 * Tests using Async Service.

public class SingleServiceIntegrationTest extends TestCase {

  public final void testAsyncServiceLifecycle() throws Exception {
    ExecutorService executor = Executors.newSingleThreadExecutor();

    final CountDownLatch startLatch = new CountDownLatch(1);
    final CountDownLatch stopLatch = new CountDownLatch(1);
    AsyncService service = new AsyncService(executor) {
      @Override protected void onStart() {
        assertEquals(1, startLatch.getCount());
        assertEquals(1, stopLatch.getCount());


      @Override protected void onStop() {
        assertEquals(0, startLatch.getCount());
        assertEquals(1, stopLatch.getCount());


    Future<?> future = service.start();
    // This should not pass!  TODO(sameb): Why?  Looks like it should to me
    assertTrue(startLatch.await(2, TimeUnit.SECONDS));
    // onStart() is called before the state is set to STARTED, so we need
    // to wait until the Future finishes to guarantee it really was started.
    // This still manages to test what we want because the startLatch check
    // is before this.
    future.get(1, TimeUnit.SECONDS);

    assertTrue(stopLatch.await(2, TimeUnit.SECONDS));

    assertEquals(0, startLatch.getCount());
    assertEquals(0, stopLatch.getCount());

  public final void testAsyncServiceBlockingLifecycle()
      throws InterruptedException, ExecutionException, TimeoutException {
    ExecutorService executor = Executors.newSingleThreadExecutor();

    final AtomicInteger integer = new AtomicInteger(2);
    AsyncService service = new AsyncService(executor) {
      @Override protected void onStart() {
        assertEquals(2, integer.getAndDecrement());

      @Override protected void onStop() {
        assertEquals(1, integer.getAndDecrement());

    service.start().get(2, TimeUnit.SECONDS);
    service.stop().get(2, TimeUnit.SECONDS);

    assertEquals(0, integer.get());

Other Java examples (source code examples)

Here is a short list of links related to this Java SingleServiceIntegrationTest.java source code file:

my book on functional programming


new blog posts


Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.