Home | History | Annotate | Download | only in concurrent
      1 /*
      2  * Copyright (C) 2009 The Guava Authors
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  * http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.google.common.util.concurrent;
     18 
     19 import com.google.common.util.concurrent.Service.State;
     20 
     21 import junit.framework.TestCase;
     22 
     23 import java.lang.Thread.UncaughtExceptionHandler;
     24 import java.util.concurrent.Executor;
     25 import java.util.concurrent.TimeUnit;
     26 import java.util.concurrent.TimeoutException;
     27 
     28 /**
     29  * Unit test for {@link AbstractIdleService}.
     30  *
     31  * @author Chris Nokleberg
     32  */
     33 public class AbstractIdleServiceTest extends TestCase {
     34   private Thread executorThread;
     35   private Throwable thrownByExecutorThread;
     36   private final Executor executor = new Executor() {
     37     @Override
     38     public void execute(Runnable command) {
     39       executorThread = new Thread(command);
     40       executorThread.setUncaughtExceptionHandler(
     41           new UncaughtExceptionHandler() {
     42             @Override
     43             public void uncaughtException(Thread thread, Throwable e) {
     44               thrownByExecutorThread = e;
     45             }
     46           });
     47       executorThread.start();
     48     }
     49   };
     50 
     51   public void testServiceStartStop() throws Exception {
     52     NullService service = new NullService();
     53     assertFalse(service.startUpCalled);
     54 
     55     service.start().get();
     56     assertTrue(service.startUpCalled);
     57     assertEquals(Service.State.RUNNING, service.state());
     58 
     59     service.stop().get();
     60     assertTrue(service.shutDownCalled);
     61     assertEquals(Service.State.TERMINATED, service.state());
     62     executorThread.join();
     63     assertNull(thrownByExecutorThread);
     64   }
     65 
     66   public void testServiceToString() throws Exception {
     67     NullService service = new NullService();
     68     assertEquals("NullService [" + Service.State.NEW + "]", service.toString());
     69     service.start().get();
     70     assertEquals("NullService [" + Service.State.RUNNING + "]", service.toString());
     71     service.stop().get();
     72     assertEquals("NullService [" + Service.State.TERMINATED + "]", service.toString());
     73   }
     74 
     75   public void testTimeout() throws Exception {
     76     // Create a service whose executor will never run its commands
     77     Service service = new NullService() {
     78       @Override protected Executor executor(Service.State state) {
     79         return new Executor() {
     80           @Override public void execute(Runnable command) {
     81           }
     82         };
     83       }
     84     };
     85 
     86     try {
     87       service.start().get(1, TimeUnit.MILLISECONDS);
     88       fail("Expected timeout");
     89     } catch (TimeoutException e) {
     90       assertTrue(e.getMessage().contains(State.STARTING.toString()));
     91     }
     92   }
     93 
     94   private class NullService extends AbstractIdleService {
     95     boolean startUpCalled = false;
     96     boolean shutDownCalled = false;
     97     State expectedShutdownState = State.STOPPING;
     98 
     99     @Override protected void startUp() {
    100       assertFalse(startUpCalled);
    101       assertFalse(shutDownCalled);
    102       startUpCalled = true;
    103       assertEquals(State.STARTING, state());
    104     }
    105 
    106     @Override protected void shutDown() {
    107       assertTrue(startUpCalled);
    108       assertFalse(shutDownCalled);
    109       shutDownCalled = true;
    110       assertEquals(expectedShutdownState, state());
    111     }
    112 
    113     @Override protected Executor executor(Service.State state) {
    114       switch (state) {
    115         case STARTING:
    116           assertFalse(startUpCalled);
    117           return executor;
    118         case STOPPING:
    119           assertTrue(startUpCalled);
    120           assertFalse(shutDownCalled);
    121           return executor;
    122         default:
    123           throw new IllegalStateException("unexpected state " + state);
    124       }
    125     }
    126   }
    127 }
    128