7404c8c3843995cbd367e6ec6adc568f1855d650
[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 com.google.common.util.concurrent.ListenableFuture;
12 import com.google.common.util.concurrent.ListeningExecutorService;
13 import com.google.common.util.concurrent.Uninterruptibles;
14 import java.util.concurrent.Callable;
15 import java.util.concurrent.CountDownLatch;
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 = (executor, blockingLatch) -> executor.submit(new Callable<Void>() {
30         @Override
31         public Void call() throws Exception {
32             if (blockingLatch != null ) {
33                 Uninterruptibles.awaitUninterruptibly( blockingLatch );
34             }
35             return null;
36         }
37     } );
38
39     public static final Invoker SUBMIT_RUNNABLE = (executor, blockingLatch) -> executor.submit(() -> {
40         if (blockingLatch != null ) {
41             Uninterruptibles.awaitUninterruptibly( blockingLatch );
42         }
43     });
44
45     public static final Invoker SUBMIT_RUNNABLE_WITH_RESULT = (executor, blockingLatch) -> executor.submit(() -> {
46         if (blockingLatch != null ) {
47             Uninterruptibles.awaitUninterruptibly( blockingLatch );
48         }
49     }, "foo" );
50 }