1 /* 2 * Copyright (C) 2024 The Android Open Source Project 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 package com.android.adservices.shared.testing.concurrency; 17 18 import java.util.Locale; 19 import java.util.Objects; 20 import java.util.concurrent.TimeUnit; 21 22 /** Custom exception to indicate a {@code SyncCallback} timed out. */ 23 public final class SyncCallbackTimeoutException extends IllegalStateException { 24 25 // TODO(b/341797803): @VisibleForTesting 26 static final String MSG_TEMPLATE = "%s: not called in %d %s"; 27 28 private final String mWhat; 29 private final long mTimeout; 30 private final TimeUnit mUnit; 31 32 /** 33 * Default constructor. 34 * 35 * @param what technically the {@code SyncCallback}, but could be any callback that timed out. 36 * @param timeout timeout duration 37 * @param unit timeout unit 38 */ SyncCallbackTimeoutException(String what, long timeout, TimeUnit unit)39 public SyncCallbackTimeoutException(String what, long timeout, TimeUnit unit) { 40 super( 41 String.format( 42 Locale.ENGLISH, 43 MSG_TEMPLATE, 44 Objects.requireNonNull(what), 45 timeout, 46 Objects.requireNonNull(unit))); 47 mWhat = what; 48 mTimeout = timeout; 49 mUnit = unit; 50 } 51 52 /** Gets the description of the callback that timed out. */ getWhat()53 public String getWhat() { 54 return mWhat; 55 } 56 57 /** Gets the timeout duration. */ getTimeout()58 public long getTimeout() { 59 return mTimeout; 60 } 61 62 /** Gets the timeout unit. */ getUnit()63 public TimeUnit getUnit() { 64 return mUnit; 65 } 66 } 67