GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — [improve][test] Introduce shared Pulsar cluster for faster integratio… · apache/pulsar@ce5bc13

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • pulsar-broker/src/test/java/org/apache/pulsar/broker/service
    • NullValueTest.java
    • SharedPulsarBaseTest.java
    • SharedPulsarCluster.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎pulsar-broker/src/test/java/org/apache/pulsar/broker/service/NullValueTest.java‎

Copy file name to clipboardExpand all lines: pulsar-broker/src/test/java/org/apache/pulsar/broker/service/NullValueTest.java
+25-40Lines changed: 25 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@
3434
import org.apache.pulsar.common.schema.KeyValue;
3535
import org.apache.pulsar.common.schema.KeyValueEncodingType;
3636
import org.testng.Assert;
37-
import org.testng.annotations.AfterMethod;
38-
import org.testng.annotations.BeforeMethod;
3937
import org.testng.annotations.DataProvider;
4038
import org.testng.annotations.Test;
4139

@@ -44,41 +42,30 @@
4442
*/
4543
@Slf4j
4644
@Test(groups = "broker")
47-
public class NullValueTest extends BrokerTestBase {
45+
public class NullValueTest extends SharedPulsarBaseTest {
4846

49-
@BeforeMethod
50-
@Override
51-
protected void setup() throws Exception {
52-
super.baseSetup();
53-
}
54-
55-
@AfterMethod(alwaysRun = true)
56-
@Override
57-
protected void cleanup() throws Exception {
58-
super.internalCleanup();
59-
}
60-
61-
@DataProvider(name = "topics")
62-
public static Object[][] topics() {
47+
@DataProvider(name = "partitions")
48+
public static Object[][] partitions() {
6349
return new Object[][]{
64-
{"persistent://prop/ns-abc/null-value-test-0", 1},
65-
{"persistent://prop/ns-abc/null-value-test-1", 3},
50+
{1},
51+
{3},
6652
};
6753
}
6854

69-
@Test(dataProvider = "topics")
70-
public void nullValueBytesSchemaTest(String topic, int partitions)
55+
@Test(dataProvider = "partitions")
56+
public void nullValueBytesSchemaTest(int partitions)
7157
throws PulsarClientException, PulsarAdminException {
58+
String topic = newTopicName();
7259
admin.topics().createPartitionedTopic(topic, partitions);
7360

7461
@Cleanup
75-
Producer producer = pulsarClient.newProducer()
62+
Producer<byte[]> producer = pulsarClient.newProducer()
7663
.topic(topic)
7764
.messageRoutingMode(MessageRoutingMode.SinglePartition)
7865
.create();
7966

8067
@Cleanup
81-
Consumer consumer = pulsarClient.newConsumer()
68+
Consumer<byte[]> consumer = pulsarClient.newConsumer()
8269
.topic(topic)
8370
.subscriptionName("test")
8471
.subscribe();
@@ -93,7 +80,7 @@ public void nullValueBytesSchemaTest(String topic, int partitions)
9380
}
9481

9582
for (int i = 0; i < numMessage; i++) {
96-
Message message = consumer.receive();
83+
Message<byte[]> message = consumer.receive();
9784
if (i % 2 == 0) {
9885
Assert.assertNotNull(message.getData());
9986
Assert.assertNotNull(message.getValue());
@@ -114,7 +101,7 @@ public void nullValueBytesSchemaTest(String topic, int partitions)
114101
}
115102

116103
for (int i = 0; i < numMessage; i++) {
117-
CompletableFuture<Message> completableFuture = consumer.receiveAsync();
104+
CompletableFuture<Message<byte[]>> completableFuture = consumer.receiveAsync();
118105
final int index = i;
119106
completableFuture.whenComplete((message, throwable) -> {
120107
Assert.assertNull(throwable);
@@ -133,12 +120,12 @@ public void nullValueBytesSchemaTest(String topic, int partitions)
133120
}
134121
});
135122
}
136-
137123
}
138124

139-
@Test(dataProvider = "topics")
140-
public void nullValueBooleanSchemaTest(String topic, int partitions)
125+
@Test(dataProvider = "partitions")
126+
public void nullValueBooleanSchemaTest(int partitions)
141127
throws PulsarClientException, PulsarAdminException {
128+
String topic = newTopicName();
142129
admin.topics().createPartitionedTopic(topic, partitions);
143130

144131
@Cleanup
@@ -163,12 +150,12 @@ public void nullValueBooleanSchemaTest(String topic, int partitions)
163150
Assert.assertNull(message.getValue());
164151
Assert.assertNull(message.getData());
165152
}
166-
167153
}
168154

169-
@Test(dataProvider = "topics")
170-
public void keyValueNullInlineTest(String topic, int partitions)
155+
@Test(dataProvider = "partitions")
156+
public void keyValueNullInlineTest(int partitions)
171157
throws PulsarClientException, PulsarAdminException {
158+
String topic = newTopicName();
172159
admin.topics().createPartitionedTopic(topic, partitions);
173160

174161
@Cleanup
@@ -198,24 +185,24 @@ public void keyValueNullInlineTest(String topic, int partitions)
198185
message = consumer.receive();
199186
keyValue = message.getValue();
200187
Assert.assertNull(keyValue.getKey());
201-
Assert.assertEquals("test", keyValue.getValue());
188+
Assert.assertEquals(keyValue.getValue(), "test");
202189

203190
message = consumer.receive();
204191
keyValue = message.getValue();
205-
Assert.assertEquals("test", keyValue.getKey());
192+
Assert.assertEquals(keyValue.getKey(), "test");
206193
Assert.assertNull(keyValue.getValue());
207194

208195
message = consumer.receive();
209196
keyValue = message.getValue();
210197
Assert.assertNull(keyValue.getKey());
211198
Assert.assertNull(keyValue.getValue());
212199
}
213-
214200
}
215201

216-
@Test(dataProvider = "topics")
217-
public void keyValueNullSeparatedTest(String topic, int partitions)
202+
@Test(dataProvider = "partitions")
203+
public void keyValueNullSeparatedTest(int partitions)
218204
throws PulsarClientException, PulsarAdminException {
205+
String topic = newTopicName();
219206
admin.topics().createPartitionedTopic(topic, partitions);
220207

221208
@Cleanup
@@ -252,19 +239,17 @@ public int choosePartition(Message<?> msg, TopicMetadata metadata) {
252239
message = consumer.receive();
253240
keyValue = message.getValue();
254241
Assert.assertNull(keyValue.getKey());
255-
Assert.assertEquals("test", keyValue.getValue());
242+
Assert.assertEquals(keyValue.getValue(), "test");
256243

257244
message = consumer.receive();
258245
keyValue = message.getValue();
259-
Assert.assertEquals("test", keyValue.getKey());
246+
Assert.assertEquals(keyValue.getKey(), "test");
260247
Assert.assertNull(keyValue.getValue());
261248

262249
message = consumer.receive();
263250
keyValue = message.getValue();
264251
Assert.assertNull(keyValue.getKey());
265252
Assert.assertNull(keyValue.getValue());
266253
}
267-
268254
}
269-
270255
}
Collapse file

‎pulsar-broker/src/test/java/org/apache/pulsar/broker/service/SharedPulsarBaseTest.java‎

Copy file name to clipboard
+166Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.pulsar.broker.service;
20+
21+
import java.util.Optional;
22+
import java.util.Set;
23+
import java.util.UUID;
24+
import java.util.concurrent.CompletableFuture;
25+
import lombok.extern.slf4j.Slf4j;
26+
import org.apache.pulsar.broker.PulsarService;
27+
import org.apache.pulsar.broker.ServiceConfiguration;
28+
import org.apache.pulsar.client.admin.PulsarAdmin;
29+
import org.apache.pulsar.client.api.PulsarClient;
30+
import org.apache.pulsar.client.api.PulsarClientException;
31+
import org.apache.pulsar.common.protocol.schema.SchemaStorage;
32+
import org.testng.annotations.AfterMethod;
33+
import org.testng.annotations.BeforeClass;
34+
import org.testng.annotations.BeforeMethod;
35+
36+
/**
37+
* Base class for tests that share a single {@link SharedPulsarCluster} singleton across all test classes.
38+
*
39+
* <p>Each test method gets its own unique namespace (created in {@link #setupSharedTest()} and
40+
* force-deleted in {@link #cleanupSharedTest()}), providing full isolation without the overhead
41+
* of starting a new broker per test.
42+
*
43+
* <p>Subclasses get access to {@link #admin} and {@link #pulsarClient} (initialized once per class)
44+
* and can use {@link #newTopicName()} to generate unique topic names within the test namespace.
45+
*/
46+
@Slf4j
47+
public abstract class SharedPulsarBaseTest {
48+
49+
private PulsarService pulsar;
50+
protected PulsarAdmin admin;
51+
protected PulsarClient pulsarClient;
52+
53+
private String namespace;
54+
55+
/**
56+
* Returns the unique namespace assigned to the current test method.
57+
*/
58+
protected String getNamespace() {
59+
return namespace;
60+
}
61+
62+
/**
63+
* Returns the broker service URL (pulsar://...) for creating dedicated PulsarClient instances.
64+
*/
65+
protected String getBrokerServiceUrl() {
66+
return pulsar.getBrokerServiceUrl();
67+
}
68+
69+
/**
70+
* Returns the web service URL (http://...) for HTTP-based lookups and admin operations.
71+
*/
72+
protected String getWebServiceUrl() {
73+
return pulsar.getWebServiceAddress();
74+
}
75+
76+
/**
77+
* Creates a new PulsarClient connected to the shared cluster. The caller is responsible for closing it.
78+
*/
79+
protected PulsarClient newPulsarClient() throws PulsarClientException {
80+
return PulsarClient.builder().serviceUrl(pulsar.getBrokerServiceUrl()).build();
81+
}
82+
83+
/**
84+
* Returns the shared broker's {@link ServiceConfiguration} for runtime config inspection or changes.
85+
*/
86+
protected ServiceConfiguration getConfig() {
87+
return pulsar.getConfig();
88+
}
89+
90+
/**
91+
* Returns the shared broker's {@link SchemaStorage} instance.
92+
*/
93+
protected SchemaStorage getSchemaStorage() {
94+
return pulsar.getSchemaStorage();
95+
}
96+
97+
/**
98+
* Returns the topic reference for an already-loaded topic, if present.
99+
*/
100+
protected Optional<Topic> getTopicReference(String topic) {
101+
return pulsar.getBrokerService().getTopicReference(topic);
102+
}
103+
104+
/**
105+
* Looks up a topic by name, optionally creating it if it doesn't exist.
106+
*/
107+
protected CompletableFuture<Optional<Topic>> getTopic(String topic, boolean createIfMissing) {
108+
return pulsar.getBrokerService().getTopic(topic, createIfMissing);
109+
}
110+
111+
/**
112+
* Returns the topic if it exists in the broker, loading it from storage if necessary.
113+
*/
114+
protected CompletableFuture<Optional<Topic>> getTopicIfExists(String topic) {
115+
return pulsar.getBrokerService().getTopicIfExists(topic);
116+
}
117+
118+
/**
119+
* Initializes the shared cluster singleton and sets up {@link #admin} and {@link #pulsarClient}.
120+
* Called once per test class.
121+
*/
122+
@BeforeClass(alwaysRun = true)
123+
public void setupSharedCluster() throws Exception {
124+
SharedPulsarCluster cluster = SharedPulsarCluster.get();
125+
pulsar = cluster.getPulsarService();
126+
admin = cluster.getAdmin();
127+
pulsarClient = cluster.getClient();
128+
}
129+
130+
/**
131+
* Creates a unique namespace for the current test method. The namespace is automatically
132+
* cleaned up in {@link #cleanupSharedTest()}.
133+
*/
134+
@BeforeMethod(alwaysRun = true)
135+
public void setupSharedTest() throws Exception {
136+
String nsName = "test-" + UUID.randomUUID().toString().substring(0, 8);
137+
String ns = SharedPulsarCluster.TENANT_NAME + "/" + nsName;
138+
namespace = ns;
139+
admin.namespaces().createNamespace(ns, Set.of(SharedPulsarCluster.CLUSTER_NAME));
140+
log.info("Created test namespace: {}", ns);
141+
}
142+
143+
/**
144+
* Force-deletes the namespace created by {@link #setupSharedTest()}, including all topics in it.
145+
*/
146+
@AfterMethod(alwaysRun = true)
147+
public void cleanupSharedTest() throws Exception {
148+
String ns = namespace;
149+
if (ns != null) {
150+
namespace = null;
151+
try {
152+
admin.namespaces().deleteNamespace(ns, true);
153+
log.info("Deleted test namespace: {}", ns);
154+
} catch (Exception e) {
155+
log.warn("Failed to delete namespace {}: {}", ns, e.getMessage());
156+
}
157+
}
158+
}
159+
160+
/**
161+
* Generates a unique persistent topic name within the current test namespace.
162+
*/
163+
protected String newTopicName() {
164+
return "persistent://" + namespace + "/topic-" + UUID.randomUUID().toString().substring(0, 8);
165+
}
166+
}

0 commit comments

Comments
0 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.