Ray Milkey | 2eb9167 | 2018-04-24 10:07:52 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 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 org.onosproject.store.primitives; |
| 18 | |
| 19 | import java.util.concurrent.CompletableFuture; |
| 20 | import java.util.concurrent.ExecutionException; |
| 21 | import java.util.concurrent.TimeUnit; |
| 22 | import java.util.concurrent.TimeoutException; |
| 23 | |
| 24 | /** |
| 25 | * Completable Futures with known error states used for testing. |
| 26 | */ |
| 27 | public final class TestingCompletableFutures { |
| 28 | |
| 29 | private TestingCompletableFutures() { |
| 30 | } |
| 31 | |
| 32 | /** Indicates which kind of error to produce. */ |
| 33 | public enum ErrorState { NONE, INTERRUPTED_EXCEPTION, TIMEOUT_EXCEPTION, EXECUTION_EXCEPTION } |
| 34 | |
| 35 | private static class TimeoutExceptionFuture<T> extends CompletableFuture<T> { |
| 36 | @Override |
| 37 | public T get(long timeout, TimeUnit unit) |
| 38 | throws TimeoutException { |
| 39 | throw new TimeoutException(); |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | private static class InterruptedFuture<T> extends CompletableFuture<T> { |
| 44 | @Override |
| 45 | public T get(long timeout, TimeUnit unit) |
| 46 | throws InterruptedException { |
| 47 | throw new InterruptedException(); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | private static class ExecutionExceptionFuture<T> extends CompletableFuture<T> { |
| 52 | @Override |
| 53 | public T get(long timeout, TimeUnit unit) |
| 54 | throws ExecutionException { |
| 55 | throw new ExecutionException("", new Exception()); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Creates a Long Future for a given error type. |
| 61 | * |
| 62 | * @param errorState wht kind of error to produce |
| 63 | * @return new future that will generate the requested error |
| 64 | */ |
| 65 | public static CompletableFuture<Long> createFuture(ErrorState errorState) { |
| 66 | switch (errorState) { |
| 67 | case TIMEOUT_EXCEPTION: |
| 68 | return new TimeoutExceptionFuture<>(); |
| 69 | case INTERRUPTED_EXCEPTION: |
| 70 | return new InterruptedFuture<>(); |
| 71 | case EXECUTION_EXCEPTION: |
| 72 | return new ExecutionExceptionFuture<>(); |
| 73 | default: |
| 74 | return new CompletableFuture<>(); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Creates a Long Future for a given error type. |
| 80 | * |
| 81 | * @param errorState wht kind of error to produce |
| 82 | * @return new future that will generate the requested error |
| 83 | */ |
| 84 | public static CompletableFuture<String> createStringFuture(ErrorState errorState) { |
| 85 | switch (errorState) { |
| 86 | case TIMEOUT_EXCEPTION: |
| 87 | return new TimeoutExceptionFuture<>(); |
| 88 | case INTERRUPTED_EXCEPTION: |
| 89 | return new InterruptedFuture<>(); |
| 90 | case EXECUTION_EXCEPTION: |
| 91 | return new ExecutionExceptionFuture<>(); |
| 92 | default: |
| 93 | return new CompletableFuture<>(); |
| 94 | } |
| 95 | } |
| 96 | } |