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 — [fix][test] Replace testGetTopicPoliciesWithCleanCache with a namespa… · apache/pulsar@23cd08a

File tree

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

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

Copy file name to clipboardExpand all lines: pulsar-broker/src/test/java/org/apache/pulsar/broker/service/SystemTopicBasedTopicPoliciesServiceTest.java
+55-55Lines changed: 55 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
import static org.mockito.ArgumentMatchers.any;
2222
import static org.mockito.ArgumentMatchers.anyBoolean;
23-
import static org.mockito.Mockito.spy;
2423
import static org.mockito.Mockito.times;
2524
import static org.mockito.Mockito.verify;
2625
import static org.testng.AssertJUnit.assertEquals;
@@ -29,20 +28,23 @@
2928
import static org.testng.AssertJUnit.assertNull;
3029
import static org.testng.AssertJUnit.assertSame;
3130
import static org.testng.AssertJUnit.assertTrue;
31+
import java.util.ArrayList;
3232
import java.util.HashSet;
3333
import java.util.List;
3434
import java.util.Map;
3535
import java.util.Optional;
3636
import java.util.Set;
3737
import java.util.UUID;
3838
import java.util.concurrent.CompletableFuture;
39+
import java.util.concurrent.CompletionException;
3940
import java.util.concurrent.ConcurrentHashMap;
4041
import java.util.concurrent.CopyOnWriteArrayList;
4142
import java.util.concurrent.ExecutionException;
4243
import java.util.concurrent.ExecutorService;
4344
import java.util.concurrent.Executors;
4445
import java.util.concurrent.TimeUnit;
4546
import java.util.concurrent.TimeoutException;
47+
import java.util.concurrent.atomic.AtomicBoolean;
4648
import lombok.Cleanup;
4749
import lombok.extern.slf4j.Slf4j;
4850
import org.apache.commons.lang3.reflect.FieldUtils;
@@ -353,69 +355,67 @@ public void testHandleNamespaceBeingDeleted() throws Exception {
353355
service.deleteTopicPoliciesAsync(TOPIC1).get();
354356
}
355357

356-
@Test
357-
public void testGetTopicPoliciesWithCleanCache() throws Exception {
358+
@Test(timeOut = 120_000)
359+
public void testGetTopicPoliciesWhileNamespaceIsUnloaded() throws Exception {
360+
// A namespace unload is what drops a namespace's cached topic policies in a broker: the policies are loaded
361+
// again by a new __change_events reader when the namespace is served again. Reads that run while the
362+
// namespace is unloaded over and over may fail with a retriable error while it is not served, but they must
363+
// never answer "no policies" (or other policies) for a topic that has a policy. The exact interleaving of a
364+
// read with the replacement of the cache is pinned by
365+
// testGetTopicPoliciesWhenBundleBounceReplacesPolicyCacheGeneration.
358366
final String topic = "persistent://" + NAMESPACE1 + "/test" + UUID.randomUUID();
359-
pulsarClient.newProducer().topic(topic).create().close();
360-
361-
SystemTopicBasedTopicPoliciesService topicPoliciesService =
362-
(SystemTopicBasedTopicPoliciesService) pulsar.getTopicPoliciesService();
363-
364-
ConcurrentHashMap<TopicName, TopicPolicies> spyPoliciesCache =
365-
spy(new ConcurrentHashMap<TopicName, TopicPolicies>());
366-
FieldUtils.writeDeclaredField(topicPoliciesService, "policiesCache", spyPoliciesCache, true);
367-
368-
Awaitility.await().untilAsserted(() -> Assertions.assertThat(
369-
TopicPolicyTestUtils.getTopicPolicies(topicPoliciesService, TopicName.get(topic))).isNull());
370-
367+
final TopicName topicName = TopicName.get(topic);
368+
admin.topics().createNonPartitionedTopic(topic);
371369
admin.topicPolicies().setMaxConsumersPerSubscription(topic, 1);
372-
Awaitility.await().untilAsserted(() -> {
373-
Assertions.assertThat(TopicPolicyTestUtils.getTopicPolicies(pulsar.getTopicPoliciesService(),
374-
TopicName.get(topic))).isNotNull();
375-
});
376-
377-
Map<NamespaceName, CompletableFuture<SystemTopicClient.Reader<PulsarEvent>>> readers =
378-
(Map<NamespaceName, CompletableFuture<SystemTopicClient.Reader<PulsarEvent>>>)
379-
FieldUtils.readDeclaredField(topicPoliciesService, "readerCaches", true);
380-
381-
Mockito.doAnswer(invocation -> {
382-
Thread.sleep(1000);
383-
return invocation.callRealMethod();
384-
}).when(spyPoliciesCache).get(Mockito.any());
370+
final TopicPoliciesService service = pulsar.getTopicPoliciesService();
371+
Awaitility.await().untilAsserted(() -> Assertions.assertThat(
372+
TopicPolicyTestUtils.getTopicPolicies(service, topicName)).isNotNull());
385373

386-
CompletableFuture<Void> result = new CompletableFuture<>();
387-
Thread thread = new Thread(() -> {
374+
final int unloads = 10;
375+
final AtomicBoolean unloading = new AtomicBoolean(true);
376+
final CompletableFuture<Void> unloader = CompletableFuture.runAsync(() -> {
388377
try {
389-
for (int i = 0; i < 10; i++) {
390-
final var policies = TopicPolicyTestUtils.getTopicPolicies(topicPoliciesService,
391-
TopicName.get(topic));
392-
if (policies == null) {
393-
throw new Exception("null policies for " + i + "th get");
394-
}
378+
for (int i = 0; i < unloads; i++) {
379+
admin.namespaces().unload(NAMESPACE1);
395380
}
396-
result.complete(null);
397-
} catch (Exception e) {
398-
result.completeExceptionally(e);
381+
} catch (PulsarAdminException e) {
382+
throw new CompletionException(e);
383+
} finally {
384+
unloading.set(false);
399385
}
400386
});
401-
402-
Thread thread2 = new Thread(() -> {
403-
for (int i = 0; i < 10; i++) {
404-
CompletableFuture<SystemTopicClient.Reader<PulsarEvent>> readerCompletableFuture =
405-
readers.get(TopicName.get(topic).getNamespaceObject());
406-
if (readerCompletableFuture != null) {
407-
readerCompletableFuture.join().closeAsync().join();
387+
final List<String> wrongAnswers = new ArrayList<>();
388+
final List<String> errors = new ArrayList<>();
389+
int answered = 0;
390+
while (unloading.get()) {
391+
try {
392+
final Optional<TopicPolicies> policies =
393+
service.getTopicPoliciesAsync(topicName, TopicPoliciesService.GetType.LOCAL_ONLY)
394+
.get(30, TimeUnit.SECONDS);
395+
if (policies.isEmpty()
396+
|| !Integer.valueOf(1).equals(policies.get().getMaxConsumersPerSubscription())) {
397+
wrongAnswers.add(String.valueOf(policies));
398+
} else {
399+
answered++;
408400
}
401+
} catch (ExecutionException e) {
402+
// Not served at this moment (the namespace is being unloaded); a caller retries.
403+
errors.add(String.valueOf(e.getCause()));
409404
}
410-
});
411-
412-
thread.start();
413-
thread2.start();
414-
415-
thread.join();
416-
thread2.join();
417-
418-
result.join();
405+
}
406+
unloader.get(60, TimeUnit.SECONDS);
407+
408+
Assertions.assertThat(wrongAnswers)
409+
.describedAs("reads answered without the topic's policy while its namespace was unloaded")
410+
.isEmpty();
411+
Assertions.assertThat(answered)
412+
.describedAs("no read was answered while the namespace was unloaded, so the assertion above holds"
413+
+ " vacuously; errors: %s", errors)
414+
.isPositive();
415+
// Served again once the unloads are over.
416+
Awaitility.await().untilAsserted(() -> Assertions.assertThat(
417+
TopicPolicyTestUtils.getTopicPolicies(service, topicName).getMaxConsumersPerSubscription())
418+
.isEqualTo(1));
419419
}
420420

421421
@Test

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.