|
| 1 | +/* |
| 2 | + * Copyright 2020 Google LLC |
| 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.google.cloud.pubsublite.internal.wire; |
| 17 | + |
| 18 | +import static org.junit.Assert.assertThrows; |
| 19 | +import static org.mockito.Mockito.*; |
| 20 | +import static org.mockito.MockitoAnnotations.initMocks; |
| 21 | + |
| 22 | +import com.google.api.core.ApiFutures; |
| 23 | +import com.google.api.gax.rpc.StatusCode; |
| 24 | +import com.google.cloud.pubsublite.*; |
| 25 | +import com.google.cloud.pubsublite.internal.ApiExceptionMatcher; |
| 26 | +import com.google.cloud.pubsublite.internal.CheckedApiException; |
| 27 | +import com.google.cloud.pubsublite.proto.Topic; |
| 28 | +import java.time.Duration; |
| 29 | +import java.util.function.Consumer; |
| 30 | +import org.junit.Before; |
| 31 | +import org.junit.Test; |
| 32 | +import org.junit.runner.RunWith; |
| 33 | +import org.junit.runners.JUnit4; |
| 34 | +import org.mockito.Mock; |
| 35 | +import org.mockito.Mockito; |
| 36 | + |
| 37 | +@RunWith(JUnit4.class) |
| 38 | +public class TopicConfigWatcherImplTest { |
| 39 | + private static final CloudRegion REGION = CloudRegion.of("us-east1"); |
| 40 | + |
| 41 | + private static TopicPath path() { |
| 42 | + return TopicPath.newBuilder() |
| 43 | + .setName(TopicName.of("a")) |
| 44 | + .setProject(ProjectNumber.of(4)) |
| 45 | + .setLocation(CloudZone.of(REGION, 'a')) |
| 46 | + .build(); |
| 47 | + } |
| 48 | + |
| 49 | + TopicConfigWatcher.Factory watcherFactory; |
| 50 | + @Mock AdminClient mockClient; |
| 51 | + @Mock Consumer<Topic> mockConsumer; |
| 52 | + |
| 53 | + @Before |
| 54 | + public void setUp() { |
| 55 | + initMocks(this); |
| 56 | + watcherFactory = new TopicConfigWatcherImpl.Factory(path(), mockClient); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + public void testFirstCallFails() { |
| 61 | + when(mockClient.getTopic(path())) |
| 62 | + .thenReturn( |
| 63 | + ApiFutures.immediateFailedFuture( |
| 64 | + new CheckedApiException(StatusCode.Code.FAILED_PRECONDITION).underlying)); |
| 65 | + TopicConfigWatcher watcher = watcherFactory.New(mockConsumer, Duration.ofMinutes(1)); |
| 66 | + watcher.startAsync(); |
| 67 | + assertThrows(IllegalStateException.class, watcher::awaitTerminated); |
| 68 | + ApiExceptionMatcher.assertThrowableMatches( |
| 69 | + watcher.failureCause(), StatusCode.Code.FAILED_PRECONDITION); |
| 70 | + verify(mockClient, times(1)).getTopic(path()); |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + public void testCallsHandlerOnStart() { |
| 75 | + Topic topic = Topic.getDefaultInstance(); |
| 76 | + when(mockClient.getTopic(path())).thenReturn(ApiFutures.immediateFuture(topic)); |
| 77 | + TopicConfigWatcher watcher = watcherFactory.New(mockConsumer, Duration.ofMinutes(1)); |
| 78 | + watcher.startAsync(); |
| 79 | + verify(mockConsumer, after(1000)).accept(topic); |
| 80 | + verifyNoMoreInteractions(mockConsumer); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + public void testHandlerCalledOnUpdates() { |
| 85 | + Topic topic1 = Topic.getDefaultInstance(); |
| 86 | + Topic topic2 = Topic.newBuilder().setName("foo").build(); |
| 87 | + when(mockClient.getTopic(path())) |
| 88 | + .thenReturn(ApiFutures.immediateFuture(topic1)) |
| 89 | + .thenReturn(ApiFutures.immediateFuture(topic1)) |
| 90 | + .thenReturn(ApiFutures.immediateFuture(topic2)); |
| 91 | + TopicConfigWatcher watcher = watcherFactory.New(mockConsumer, Duration.ofMillis(10)); |
| 92 | + watcher.startAsync(); |
| 93 | + verify(mockClient, after(1000).atLeast(3)).getTopic(path()); |
| 94 | + verify(mockConsumer, after(1000)).accept(topic1); |
| 95 | + verify(mockConsumer, after(1000)).accept(topic2); |
| 96 | + verifyNoMoreInteractions(mockConsumer); |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + public void testFailuresAfterFirstSuccessIgnored() { |
| 101 | + Topic topic1 = Topic.getDefaultInstance(); |
| 102 | + Topic topic2 = Topic.newBuilder().setName("foo").build(); |
| 103 | + when(mockClient.getTopic(path())) |
| 104 | + .thenReturn(ApiFutures.immediateFuture(topic1)) |
| 105 | + .thenReturn( |
| 106 | + ApiFutures.immediateFailedFuture( |
| 107 | + new CheckedApiException(StatusCode.Code.FAILED_PRECONDITION))) |
| 108 | + .thenReturn(ApiFutures.immediateFuture(topic2)); |
| 109 | + TopicConfigWatcher watcher = watcherFactory.New(mockConsumer, Duration.ofMillis(10)); |
| 110 | + watcher.startAsync(); |
| 111 | + verify(mockClient, after(1000).atLeast(3)).getTopic(path()); |
| 112 | + verify(mockConsumer, after(1000)).accept(topic1); |
| 113 | + verify(mockConsumer, after(1000)).accept(topic2); |
| 114 | + verifyNoMoreInteractions(mockConsumer); |
| 115 | + } |
| 116 | + |
| 117 | + @Test |
| 118 | + public void testStopPreventsFutureCalls() { |
| 119 | + Topic topic1 = Topic.getDefaultInstance(); |
| 120 | + when(mockClient.getTopic(path())).thenReturn(ApiFutures.immediateFuture(topic1)); |
| 121 | + TopicConfigWatcher watcher = watcherFactory.New(mockConsumer, Duration.ofMillis(10)); |
| 122 | + watcher.startAsync(); |
| 123 | + watcher.stopAsync(); |
| 124 | + watcher.awaitTerminated(); |
| 125 | + verify(mockClient, after(1000).atLeast(1)).getTopic(path()); |
| 126 | + Mockito.reset(mockClient); |
| 127 | + verify(mockClient, after(20).never()).getTopic(any()); |
| 128 | + } |
| 129 | +} |
0 commit comments