|
| 1 | +/* |
| 2 | + * Copyright 2025 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 | + |
| 17 | +package com.google.cloud.pubsub.v1; |
| 18 | + |
| 19 | +import static org.junit.Assert.assertEquals; |
| 20 | +import static org.junit.Assert.assertNotNull; |
| 21 | +import static org.junit.Assert.assertTrue; |
| 22 | + |
| 23 | +import com.google.cloud.pubsub.v1.SubscriberShutdownSettings.ShutdownMode; |
| 24 | +import java.time.Duration; |
| 25 | +import org.junit.Test; |
| 26 | + |
| 27 | +public class SubscriberShutdownSettingsTest { |
| 28 | + |
| 29 | + @Test |
| 30 | + public void testDefaultSettings() { |
| 31 | + SubscriberShutdownSettings settings = SubscriberShutdownSettings.newBuilder().build(); |
| 32 | + |
| 33 | + assertNotNull(settings); |
| 34 | + assertEquals(ShutdownMode.WAIT_FOR_PROCESSING, settings.getMode()); |
| 35 | + assertTrue(settings.getTimeout().isNegative()); // Indefinite timeout |
| 36 | + } |
| 37 | + |
| 38 | + @Test |
| 39 | + public void testWaitForProcessingWithCustomTimeout() { |
| 40 | + Duration customTimeout = Duration.ofSeconds(30); |
| 41 | + SubscriberShutdownSettings settings = |
| 42 | + SubscriberShutdownSettings.newBuilder() |
| 43 | + .setMode(ShutdownMode.WAIT_FOR_PROCESSING) |
| 44 | + .setTimeout(customTimeout) |
| 45 | + .build(); |
| 46 | + |
| 47 | + assertNotNull(settings); |
| 48 | + assertEquals(ShutdownMode.WAIT_FOR_PROCESSING, settings.getMode()); |
| 49 | + assertEquals(customTimeout, settings.getTimeout()); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + public void testNackImmediatelyWithDefaultTimeout() { |
| 54 | + SubscriberShutdownSettings settings = |
| 55 | + SubscriberShutdownSettings.newBuilder().setMode(ShutdownMode.NACK_IMMEDIATELY).build(); |
| 56 | + |
| 57 | + assertNotNull(settings); |
| 58 | + assertEquals(ShutdownMode.NACK_IMMEDIATELY, settings.getMode()); |
| 59 | + assertTrue(settings.getTimeout().isNegative()); // Indefinite timeout |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + public void testNackImmediatelyWithCustomTimeout() { |
| 64 | + Duration customTimeout = Duration.ofSeconds(10); |
| 65 | + SubscriberShutdownSettings settings = |
| 66 | + SubscriberShutdownSettings.newBuilder() |
| 67 | + .setMode(ShutdownMode.NACK_IMMEDIATELY) |
| 68 | + .setTimeout(customTimeout) |
| 69 | + .build(); |
| 70 | + |
| 71 | + assertNotNull(settings); |
| 72 | + assertEquals(ShutdownMode.NACK_IMMEDIATELY, settings.getMode()); |
| 73 | + assertEquals(customTimeout, settings.getTimeout()); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + public void testZeroTimeout() { |
| 78 | + Duration zeroTimeout = Duration.ZERO; |
| 79 | + SubscriberShutdownSettings settings = |
| 80 | + SubscriberShutdownSettings.newBuilder() |
| 81 | + .setMode(ShutdownMode.WAIT_FOR_PROCESSING) |
| 82 | + .setTimeout(zeroTimeout) |
| 83 | + .build(); |
| 84 | + |
| 85 | + assertNotNull(settings); |
| 86 | + assertEquals(ShutdownMode.WAIT_FOR_PROCESSING, settings.getMode()); |
| 87 | + assertEquals(zeroTimeout, settings.getTimeout()); |
| 88 | + assertTrue(settings.getTimeout().isZero()); |
| 89 | + } |
| 90 | + |
| 91 | + @Test(expected = NullPointerException.class) |
| 92 | + public void testNullModeThrowsException() { |
| 93 | + SubscriberShutdownSettings.newBuilder().setMode(null).build(); |
| 94 | + } |
| 95 | + |
| 96 | + @Test(expected = NullPointerException.class) |
| 97 | + public void testNullTimeoutThrowsException() { |
| 98 | + SubscriberShutdownSettings.newBuilder().setTimeout(null).build(); |
| 99 | + } |
| 100 | +} |
0 commit comments