/
SubscriberTest.java
243 lines (203 loc) · 8.43 KB
/
SubscriberTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*
* Copyright 2016 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.cloud.pubsub.v1;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import com.google.api.gax.core.ExecutorProvider;
import com.google.api.gax.core.FixedExecutorProvider;
import com.google.api.gax.core.InstantiatingExecutorProvider;
import com.google.api.gax.core.NoCredentialsProvider;
import com.google.api.gax.grpc.GrpcStatusCode;
import com.google.api.gax.grpc.GrpcTransportChannel;
import com.google.api.gax.rpc.ApiException;
import com.google.api.gax.rpc.FixedTransportChannelProvider;
import com.google.api.gax.rpc.StatusCode;
import com.google.cloud.pubsub.v1.Subscriber.Builder;
import com.google.pubsub.v1.ProjectSubscriptionName;
import com.google.pubsub.v1.PubsubMessage;
import io.grpc.ManagedChannel;
import io.grpc.Server;
import io.grpc.Status;
import io.grpc.StatusException;
import io.grpc.inprocess.InProcessChannelBuilder;
import io.grpc.inprocess.InProcessServerBuilder;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestName;
import org.threeten.bp.Duration;
/** Tests for {@link Subscriber}. */
public class SubscriberTest {
private static final ProjectSubscriptionName TEST_SUBSCRIPTION =
ProjectSubscriptionName.of("test-project", "test-subscription");
private ManagedChannel testChannel;
private FakeScheduledExecutorService fakeExecutor;
private FakeSubscriberServiceImpl fakeSubscriberServiceImpl;
private Server testServer;
private final MessageReceiver testReceiver =
new MessageReceiver() {
@Override
public void receiveMessage(PubsubMessage message, AckReplyConsumer consumer) {
consumer.ack();
}
};
@Rule public TestName testName = new TestName();
@Before
public void setUp() throws Exception {
InProcessServerBuilder serverBuilder = InProcessServerBuilder.forName(testName.getMethodName());
fakeSubscriberServiceImpl = new FakeSubscriberServiceImpl();
fakeExecutor = new FakeScheduledExecutorService();
testChannel = InProcessChannelBuilder.forName(testName.getMethodName()).build();
serverBuilder.addService(fakeSubscriberServiceImpl);
testServer = serverBuilder.build();
testServer.start();
}
@After
public void tearDown() throws Exception {
testServer.shutdownNow().awaitTermination();
testChannel.shutdown();
}
@Test
public void testDeliveryAttemptHelper() {
Integer deliveryAttempt = 3;
PubsubMessage message =
PubsubMessage.newBuilder()
.putAttributes("googclient_deliveryattempt", Integer.toString(deliveryAttempt))
.build();
assertEquals(Subscriber.getDeliveryAttempt(message), deliveryAttempt);
// In the case where delivery attempt attribute is not populated, expect null
PubsubMessage emptyMessage = PubsubMessage.newBuilder().build();
assertEquals(Subscriber.getDeliveryAttempt(emptyMessage), null);
}
@Test
public void testOpenedChannels() throws Exception {
int expectedChannelCount = 1;
Subscriber subscriber = startSubscriber(getTestSubscriberBuilder(testReceiver));
assertEquals(
expectedChannelCount, fakeSubscriberServiceImpl.waitForOpenedStreams(expectedChannelCount));
subscriber.stopAsync().awaitTerminated();
}
@Test
public void testFailedChannel_recoverableError_channelReopened() throws Exception {
int expectedChannelCount = 1;
Subscriber subscriber =
startSubscriber(
getTestSubscriberBuilder(testReceiver)
.setSystemExecutorProvider(
InstantiatingExecutorProvider.newBuilder().setExecutorThreadCount(1).build()));
// Recoverable error
fakeSubscriberServiceImpl.sendError(new StatusException(Status.INTERNAL));
assertEquals(1, fakeSubscriberServiceImpl.waitForClosedStreams(1));
assertEquals(
expectedChannelCount, fakeSubscriberServiceImpl.waitForOpenedStreams(expectedChannelCount));
subscriber.stopAsync().awaitTerminated();
}
@Test
public void testFailedChannel_testTerminated() throws Exception {
final ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(5);
ExecutorProvider provider =
new ExecutorProvider() {
@Override
public boolean shouldAutoClose() {
return true;
}
@Override
public ScheduledExecutorService getExecutor() {
return scheduledExecutorService;
}
};
try {
Subscriber subscriber =
startSubscriber(
getTestSubscriberBuilder(testReceiver).setSystemExecutorProvider(provider));
// wait long enough for the MessageDispatcher to set up, which at one point
// caused shutdown problems.
Thread.sleep(100);
subscriber.stopAsync().awaitTerminated();
assertTrue(scheduledExecutorService.awaitTermination(10, TimeUnit.SECONDS));
} finally {
scheduledExecutorService.shutdownNow();
}
}
@Test(expected = IllegalStateException.class)
public void testFailedChannel_fatalError_subscriberFails() throws Exception {
Subscriber subscriber =
startSubscriber(
getTestSubscriberBuilder(testReceiver)
.setSystemExecutorProvider(
InstantiatingExecutorProvider.newBuilder().setExecutorThreadCount(10).build()));
// Fatal error
fakeSubscriberServiceImpl.sendError(new StatusException(Status.INVALID_ARGUMENT));
try {
subscriber.awaitTerminated();
} finally {
// The subscriber must finish with an state error because its FAILED status.
assertEquals(Subscriber.State.FAILED, subscriber.state());
Throwable t = subscriber.failureCause();
assertTrue(t instanceof ApiException);
ApiException ex = (ApiException) (t);
assertTrue(ex.getStatusCode() instanceof GrpcStatusCode);
GrpcStatusCode grpcCode = (GrpcStatusCode) ex.getStatusCode();
assertEquals(StatusCode.Code.INVALID_ARGUMENT, grpcCode.getCode());
}
}
@Test(expected = IllegalStateException.class)
public void testFailedChannel_shutdownBackgroundResources() throws Exception {
ExecutorProvider provider =
new ExecutorProvider() {
@Override
public boolean shouldAutoClose() {
return true;
}
@Override
public ScheduledExecutorService getExecutor() {
return fakeExecutor;
}
};
Subscriber subscriber =
startSubscriber(getTestSubscriberBuilder(testReceiver).setExecutorProvider(provider));
// Fatal error
fakeSubscriberServiceImpl.sendError(new StatusException(Status.INVALID_ARGUMENT));
try {
subscriber.awaitTerminated();
} finally {
// The subscriber must finish with an state error because its FAILED status.
assertEquals(Subscriber.State.FAILED, subscriber.state());
// Make sure that our executor is shut down after a failure
assertTrue(fakeExecutor.isShutdown());
}
}
private Subscriber startSubscriber(Builder testSubscriberBuilder) {
Subscriber subscriber = testSubscriberBuilder.build();
subscriber.startAsync().awaitRunning();
return subscriber;
}
private Builder getTestSubscriberBuilder(MessageReceiver receiver) {
return Subscriber.newBuilder(TEST_SUBSCRIPTION, receiver)
.setExecutorProvider(FixedExecutorProvider.create(fakeExecutor))
.setSystemExecutorProvider(FixedExecutorProvider.create(fakeExecutor))
.setChannelProvider(
FixedTransportChannelProvider.create(GrpcTransportChannel.create(testChannel)))
.setCredentialsProvider(NoCredentialsProvider.create())
.setClock(fakeExecutor.getClock())
.setParallelPullCount(1)
.setMaxDurationPerAckExtension(Duration.ofSeconds(5));
}
}