Skip to content

Commit 7de0d45

Browse files
authored
Add serialization test for PubSub classes (#1072)
1 parent 7a7574d commit 7de0d45

File tree

2 files changed

+110
-1
lines changed

2 files changed

+110
-1
lines changed

gcloud-java-pubsub/pom.xml

+8-1
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,20 @@
3030
<dependency>
3131
<groupId>io.grpc</groupId>
3232
<artifactId>grpc-all</artifactId>
33-
<version>0.12.0</version>
33+
<version>0.14.0</version>
3434
</dependency>
3535
<dependency>
3636
<groupId>com.google.auto.value</groupId>
3737
<artifactId>auto-value</artifactId>
3838
<version>1.1</version>
3939
</dependency>
40+
<dependency>
41+
<groupId>${project.groupId}</groupId>
42+
<artifactId>gcloud-java-core</artifactId>
43+
<version>${project.version}</version>
44+
<type>test-jar</type>
45+
<scope>test</scope>
46+
</dependency>
4047
<dependency>
4148
<groupId>junit</groupId>
4249
<artifactId>junit</artifactId>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*
2+
* Copyright 2016 Google Inc. All Rights Reserved.
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;
18+
19+
import com.google.cloud.AuthCredentials;
20+
import com.google.cloud.BaseSerializationTest;
21+
import com.google.cloud.GrpcServiceOptions.ExecutorFactory;
22+
import com.google.cloud.Restorable;
23+
import com.google.cloud.pubsub.PubSub.ListOption;
24+
import com.google.cloud.pubsub.PubSub.PullOption;
25+
26+
import java.io.Serializable;
27+
import java.util.concurrent.ScheduledExecutorService;
28+
29+
public class SerializationTest extends BaseSerializationTest {
30+
31+
private static final PubSub PUB_SUB = PubSubOptions.builder()
32+
.projectId("p")
33+
.authCredentials(AuthCredentials.noAuth())
34+
.host("localhost")
35+
.build().service();
36+
private static final Message MESSAGE = Message.of("payload");
37+
private static final com.google.pubsub.v1.ReceivedMessage RECEIVED_MESSAGE_PB =
38+
com.google.pubsub.v1.ReceivedMessage.newBuilder()
39+
.setMessage(MESSAGE.toPb())
40+
.setAckId("ackId")
41+
.build();
42+
private static final ReceivedMessage RECEIVED_MESSAGE =
43+
ReceivedMessage.fromPb(PUB_SUB, "subscription", RECEIVED_MESSAGE_PB);
44+
private static final SubscriptionInfo SUBSCRIPTION_INFO = SubscriptionInfo.of("topic", "sub");
45+
private static final Subscription SUBSCRIPTION =
46+
new Subscription(PUB_SUB, new SubscriptionInfo.BuilderImpl(SUBSCRIPTION_INFO));
47+
private static final SubscriptionId SUBSCRIPTION_ID = new SubscriptionId("project", "sub");
48+
private static final TopicInfo TOPIC_INFO = TopicInfo.of("topic");
49+
private static final Topic TOPIC =
50+
new Topic(PUB_SUB, new TopicInfo.BuilderImpl(TOPIC_INFO));
51+
private static final ListOption PAGE_TOKEN_OPTION = ListOption.pageToken("cursor");
52+
private static final ListOption PAGE_SIZE_OPTION = ListOption.pageSize(42);
53+
private static final PullOption MAX_QUEUED_CALLBACKS_OPTION = PullOption.maxQueuedCallbacks(42);
54+
private static final PullOption EXECUTOR_FACTORY_OPTION =
55+
PullOption.executorFactory(new TestExecutorFactory());
56+
57+
public static class TestExecutorFactory
58+
implements ExecutorFactory<ScheduledExecutorService>, Serializable {
59+
60+
private static final long serialVersionUID = -2154875338174302704L;
61+
62+
@Override
63+
public ScheduledExecutorService get() {
64+
return null;
65+
}
66+
67+
@Override
68+
public void release(ScheduledExecutorService executor) {
69+
// do nothing
70+
}
71+
72+
@Override
73+
public boolean equals(Object obj) {
74+
return obj instanceof TestExecutorFactory;
75+
}
76+
77+
@Override
78+
public int hashCode() {
79+
return 1;
80+
}
81+
}
82+
83+
@Override
84+
protected Serializable[] serializableObjects() {
85+
PubSubOptions options = PubSubOptions.builder()
86+
.projectId("p1")
87+
.initialTimeout(1234)
88+
.build();
89+
PubSubOptions otherOptions = options.toBuilder()
90+
.projectId("p2")
91+
.executorFactory(new TestExecutorFactory())
92+
.build();
93+
return new Serializable[]{options, otherOptions, MESSAGE, RECEIVED_MESSAGE, SUBSCRIPTION_INFO,
94+
SUBSCRIPTION, SUBSCRIPTION_ID, TOPIC_INFO, TOPIC, PAGE_TOKEN_OPTION, PAGE_SIZE_OPTION,
95+
MAX_QUEUED_CALLBACKS_OPTION, EXECUTOR_FACTORY_OPTION};
96+
}
97+
98+
@Override
99+
protected Restorable<?>[] restorableObjects() {
100+
return null;
101+
}
102+
}

0 commit comments

Comments
 (0)