1 /* 2 * Copyright 2018 Google LLC 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions are 6 * met: 7 * 8 * * Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * * Redistributions in binary form must reproduce the above 11 * copyright notice, this list of conditions and the following disclaimer 12 * in the documentation and/or other materials provided with the 13 * distribution. 14 * * Neither the name of Google LLC nor the names of its 15 * contributors may be used to endorse or promote products derived from 16 * this software without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 */ 30 package com.google.api.gax.rpc; 31 32 import static com.google.common.truth.Truth.assertThat; 33 34 import com.google.api.core.ApiClock; 35 import java.util.concurrent.ScheduledExecutorService; 36 import java.util.concurrent.TimeUnit; 37 import org.junit.Test; 38 import org.junit.runner.RunWith; 39 import org.mockito.Mock; 40 import org.mockito.Mockito; 41 import org.mockito.junit.MockitoJUnitRunner; 42 import org.threeten.bp.Duration; 43 44 @RunWith(MockitoJUnitRunner.class) 45 public class InstantiatingWatchdogProviderTest { 46 @Mock private ScheduledExecutorService executor; 47 @Mock private ApiClock clock; 48 private Duration checkInterval = Duration.ofSeconds(11); 49 50 @Test happyPath()51 public void happyPath() { 52 WatchdogProvider provider = InstantiatingWatchdogProvider.create(); 53 54 assertThat(provider.needsExecutor()).isTrue(); 55 provider = provider.withExecutor(executor); 56 57 assertThat(provider.needsClock()).isTrue(); 58 provider = provider.withClock(clock); 59 60 assertThat(provider.needsCheckInterval()).isTrue(); 61 provider = provider.withCheckInterval(checkInterval); 62 63 assertThat(provider.shouldAutoClose()).isTrue(); 64 65 Watchdog watchdog = provider.getWatchdog(); 66 Mockito.verify(executor) 67 .scheduleAtFixedRate( 68 watchdog, checkInterval.toMillis(), checkInterval.toMillis(), TimeUnit.MILLISECONDS); 69 } 70 71 @Test requiresExecutor()72 public void requiresExecutor() { 73 WatchdogProvider provider = 74 InstantiatingWatchdogProvider.create().withCheckInterval(checkInterval).withClock(clock); 75 76 Throwable actualError = null; 77 try { 78 provider.getWatchdog(); 79 } catch (Throwable t) { 80 actualError = t; 81 } 82 assertThat(actualError).isInstanceOf(IllegalStateException.class); 83 } 84 85 @Test requiresCheckInterval()86 public void requiresCheckInterval() { 87 WatchdogProvider provider = 88 InstantiatingWatchdogProvider.create().withExecutor(executor).withClock(clock); 89 90 Throwable actualError = null; 91 try { 92 provider.getWatchdog(); 93 } catch (Throwable t) { 94 actualError = t; 95 } 96 assertThat(actualError).isInstanceOf(IllegalStateException.class); 97 } 98 99 @Test requiresClock()100 public void requiresClock() { 101 WatchdogProvider provider = 102 InstantiatingWatchdogProvider.create() 103 .withExecutor(executor) 104 .withCheckInterval(checkInterval); 105 106 Throwable actualError = null; 107 try { 108 provider.getWatchdog(); 109 } catch (Throwable t) { 110 actualError = t; 111 } 112 assertThat(actualError).isInstanceOf(IllegalStateException.class); 113 } 114 } 115