Skip to content

Commit c0f9a4f

Browse files
committed
chore: Restore deleted files
1 parent a59970e commit c0f9a4f

File tree

2 files changed

+202
-0
lines changed

2 files changed

+202
-0
lines changed
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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+
* https://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 com.google.common.base.Preconditions;
20+
import java.time.Duration;
21+
22+
/**
23+
* Settings for configuring the shutdown behavior of a {@link Subscriber}.
24+
*
25+
* <p>This class allows customization of how the subscriber handles outstanding messages during
26+
* shutdown, including whether to wait for processing to complete or to immediately nack messages,
27+
* and an optional timeout for the shutdown process.
28+
*/
29+
public final class SubscriberShutdownSettings {
30+
31+
/** Defines the behavior for handling outstanding messages during subscriber shutdown. */
32+
public enum ShutdownMode {
33+
/**
34+
* The subscriber will wait for all outstanding messages to be processed (acked or nacked by the
35+
* user's message receiver) before completing the shutdown.
36+
*/
37+
WAIT_FOR_PROCESSING,
38+
/**
39+
* The subscriber will immediately nack all outstanding messages and attempt to shut down as
40+
* quickly as possible. Messages delivered to the user callback but not yet acked/nacked will
41+
* also be nacked.
42+
*/
43+
NACK_IMMEDIATELY
44+
}
45+
46+
private static final Duration DEFAULT_TIMEOUT = Duration.ofSeconds(-1); // Indicates no timeout
47+
private static final ShutdownMode DEFAULT_MODE = ShutdownMode.WAIT_FOR_PROCESSING;
48+
49+
private final ShutdownMode mode;
50+
private final Duration timeout;
51+
52+
private SubscriberShutdownSettings(Builder builder) {
53+
this.mode = builder.mode;
54+
this.timeout = builder.timeout;
55+
}
56+
57+
/** Returns the configured shutdown mode. */
58+
public ShutdownMode getMode() {
59+
return mode;
60+
}
61+
62+
/** Returns the configured shutdown timeout. A negative duration indicates no timeout. */
63+
public Duration getTimeout() {
64+
return timeout;
65+
}
66+
67+
/** Returns a new builder for {@code SubscriberShutdownSettings}. */
68+
public static Builder newBuilder() {
69+
return new Builder();
70+
}
71+
72+
/** Builder for {@code SubscriberShutdownSettings}. */
73+
public static final class Builder {
74+
private ShutdownMode mode = DEFAULT_MODE;
75+
private Duration timeout = DEFAULT_TIMEOUT;
76+
77+
private Builder() {}
78+
79+
/** Sets the shutdown mode. Defaults to {@link ShutdownMode#WAIT_FOR_PROCESSING}. */
80+
public Builder setMode(ShutdownMode mode) {
81+
this.mode = Preconditions.checkNotNull(mode);
82+
return this;
83+
}
84+
85+
/**
86+
* Sets the shutdown timeout. Defaults to a negative duration, indicating no timeout.
87+
*
88+
* <p>A positive duration specifies the maximum time to wait for shutdown to complete. A
89+
* duration of zero indicates an immediate, forceful shutdown. A negative duration indicates an
90+
* indefinite wait.
91+
*/
92+
public Builder setTimeout(Duration timeout) {
93+
this.timeout = Preconditions.checkNotNull(timeout);
94+
return this;
95+
}
96+
97+
/** Builds an instance of {@code SubscriberShutdownSettings}. */
98+
public SubscriberShutdownSettings build() {
99+
return new SubscriberShutdownSettings(this);
100+
}
101+
}
102+
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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

Comments
 (0)