• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 Google Inc.
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.squareup.okhttp.mockwebserver;
17 
18 import java.util.concurrent.CountDownLatch;
19 import java.util.function.Consumer;
20 
21 /**
22  * Utility class for registering as a listener to {@link MockResponse#setSocketShutdownListener}.
23  *
24  * An instance will wait for a single shutdown callback. Once triggered, the listener will stay in a
25  * "shutdown occurred" state.
26  *
27  * @see SocketPolicy
28  */
29 public class SocketShutdownListener implements Consumer<SocketPolicy> {
30 
31   final CountDownLatch shutdownLatch = new CountDownLatch(1);
32 
33   @Override
accept(SocketPolicy reason)34   public void accept(SocketPolicy reason) {
35     shutdownLatch.countDown();
36   }
37 
waitForSocketShutdown()38   public void waitForSocketShutdown() {
39     try {
40       shutdownLatch.await();
41     } catch (InterruptedException e) {
42     }
43   }
44 }
45