This repository was archived by the owner on Apr 7, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 141
Expand file tree
/
Copy pathITQueueTest.java
More file actions
163 lines (142 loc) · 5.7 KB
/
Copy pathITQueueTest.java
File metadata and controls
163 lines (142 loc) · 5.7 KB
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
/*
* Copyright 2025 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.spanner.it;
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertThrows;
import static org.junit.Assume.assumeTrue;
import com.google.cloud.ByteArray;
import com.google.cloud.spanner.*;
import com.google.cloud.spanner.connection.ConnectionOptions;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.junit.*;
import org.junit.experimental.categories.Category;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
/** Integration test for Cloud Spanner Queue. */
@Category(ParallelIntegrationTest.class)
@RunWith(Parameterized.class)
public class ITQueueTest {
@ClassRule public static IntegrationTestEnv env = new IntegrationTestEnv();
@Parameterized.Parameters(name = "Dialect = {0}")
public static List<DialectTestParameter> data() {
List<DialectTestParameter> params = new ArrayList<>();
params.add(new DialectTestParameter(Dialect.GOOGLE_STANDARD_SQL));
return params;
}
@Parameterized.Parameter() public DialectTestParameter dialect;
private static DatabaseClient googleStandardSQLClient;
private static final String[] GOOGLE_STANDARD_SQL_SCHEMA =
new String[] {
"CREATE Queue Q1 ("
+ " Id INT64 NOT NULL,"
+ " Payload BYTES(MAX) NOT NULL,"
+ ") PRIMARY KEY (Id), "
+ "OPTIONS (receive_mode = 'PULL')",
"CREATE TABLE T1 ("
+ " K1 INT64 NOT NULL,"
+ " K INT64 NOT NULL,"
+ ") PRIMARY KEY (K1)"
};
private static DatabaseClient client;
private Struct readRow(String queue, Key key, String... columns) {
return client.singleUse(TimestampBound.strong()).readRow(queue, key, Arrays.asList(columns));
}
@BeforeClass
public static void setUpTestSuite() {
// TODO: remove once the feature is fully enabled in prod
assumeTrue("Queue tests are temporarily disabled", false);
Database googleStandardSQLDatabase =
env.getTestHelper().createTestDatabase(GOOGLE_STANDARD_SQL_SCHEMA);
googleStandardSQLClient = env.getTestHelper().getDatabaseClient(googleStandardSQLDatabase);
System.out.println("Database created");
}
@Before
public void setUp() {
// TODO: add postgres schema & client after feature is enabled
client = googleStandardSQLClient;
}
@AfterClass
public static void teardown() {
ConnectionOptions.closeSpanner();
}
@Test
public void testSendAndAckMutation() {
client.write(
Arrays.asList(
Mutation.newSendBuilder("Q1")
.setKey(Key.of(1))
.setPayload(Value.bytes(ByteArray.copyFrom("payload1")))
.build(),
Mutation.newSendBuilder("Q1")
.setKey(Key.of(2))
.setPayload(Value.bytes(ByteArray.copyFrom("payload2")))
.build(),
Mutation.newSendBuilder("Q1")
.setKey(Key.of(3))
.setPayload(Value.bytes(ByteArray.copyFrom("payload3")))
.setDeliveryTime(Instant.now())
.build()));
// Verifying messages are in the queue.
Struct row = readRow("Q1", Key.of(1), "Payload");
assertThat(row == null).isFalse();
assertThat(row.isNull(0)).isFalse();
assertThat(row.getBytes(0)).isEqualTo(ByteArray.copyFrom("payload1"));
row = readRow("Q1", Key.of(2), "Payload");
assertThat(row.isNull(0)).isFalse();
assertThat(row.getBytes(0)).isEqualTo(ByteArray.copyFrom("payload2"));
row = readRow("Q1", Key.of(3), "Payload");
assertThat(row.isNull(0)).isFalse();
assertThat(row.getBytes(0)).isEqualTo(ByteArray.copyFrom("payload3"));
// Ack-ing the first two messages.
client.write(
Arrays.asList(
Mutation.newAckBuilder("Q1").setKey(Key.of(1)).build(),
Mutation.newAckBuilder("Q1").setKey(Key.of(2)).build()));
// Verifying the first 2 messages are acked and remvoed from the queue
row = readRow("Q1", Key.of(1), "Payload");
assertThat(row == null).isTrue();
row = readRow("Q1", Key.of(2), "Payload");
assertThat(row == null).isTrue();
row = readRow("Q1", Key.of(3), "Payload");
assertThat(row.isNull(0)).isFalse();
assertThat(row.getBytes(0)).isEqualTo(ByteArray.copyFrom("payload3"));
}
@Test
public void testAckNotFound() {
// Enable IgnoreNotFound.
client.write(
Collections.singletonList(
Mutation.newAckBuilder("Q1").setKey(Key.of(1)).setIgnoreNotFound(true).build()));
Struct row = readRow("Q1", Key.of(1), "Payload");
assertThat(row == null).isTrue();
// Disable IgnoreNotFound.
SpannerException thrown =
assertThrows(
SpannerException.class,
() ->
client.write(
Collections.singletonList(
Mutation.newAckBuilder("Q1")
.setKey(Key.of(1))
.setIgnoreNotFound(false)
.build())));
assertThat(thrown).hasMessageThat().contains("NOT_FOUND: Message not found");
}
}