584c8674c300c0c6cd3f10d44d9625847afe0719
[yangtools.git] / common / util / src / test / java / org / opendaylight / yangtools / util / concurrent / CommonTestUtils.java
1 /*
2  * Copyright (c) 2014 Brocade Communications Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.yangtools.util.concurrent;
10
11 import java.util.concurrent.Callable;
12 import java.util.concurrent.CountDownLatch;
13 import com.google.common.util.concurrent.ListenableFuture;
14 import com.google.common.util.concurrent.ListeningExecutorService;
15 import com.google.common.util.concurrent.Uninterruptibles;
16
17 /**
18  * Some common test utilities.
19  *
20  * @author Thomas Pantelis
21  */
22 public class CommonTestUtils {
23
24     public interface Invoker {
25         ListenableFuture<?> invokeExecutor( ListeningExecutorService executor,
26                 CountDownLatch blockingLatch );
27     }
28
29     public static final Invoker SUBMIT_CALLABLE = new Invoker() {
30         @Override
31         public ListenableFuture<?> invokeExecutor( ListeningExecutorService executor,
32                 final CountDownLatch blockingLatch ) {
33             return executor.submit( new Callable<Void>() {
34                 @Override
35                 public Void call() throws Exception {
36                     if( blockingLatch != null ) {
37                         Uninterruptibles.awaitUninterruptibly( blockingLatch );
38                     }
39                     return null;
40                 }
41             } );
42         }
43     };
44
45     public static final Invoker SUBMIT_RUNNABLE =  new Invoker() {
46         @Override
47         public ListenableFuture<?> invokeExecutor( ListeningExecutorService executor,
48                 final CountDownLatch blockingLatch ) {
49             return executor.submit( new Runnable() {
50                 @Override
51                 public void run() {
52                     if( blockingLatch != null ) {
53                         Uninterruptibles.awaitUninterruptibly( blockingLatch );
54                     }
55                 }
56             } );
57         }
58     };
59
60     public static final Invoker SUBMIT_RUNNABLE_WITH_RESULT = new Invoker() {
61         @Override
62         public ListenableFuture<?> invokeExecutor( ListeningExecutorService executor,
63                 final CountDownLatch blockingLatch ) {
64             return executor.submit( new Runnable() {
65                 @Override
66                 public void run() {
67                     if( blockingLatch != null ) {
68                         Uninterruptibles.awaitUninterruptibly( blockingLatch );
69                     }
70                 }
71             }, "foo" );
72         }
73     };
74 }