Skip to content

Commit a62e10a

Browse files
committed
Don't close LeaderElection in ServiceUnitStateChannelTest
1 parent 303933e commit a62e10a

File tree

1 file changed

+17
-15
lines changed

1 file changed

+17
-15
lines changed

pulsar-broker/src/test/java/org/apache/pulsar/broker/loadbalance/extensions/channel/ServiceUnitStateChannelTest.java

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,7 @@
3535
import static org.apache.pulsar.metadata.api.extended.SessionEvent.Reconnected;
3636
import static org.apache.pulsar.metadata.api.extended.SessionEvent.SessionLost;
3737
import static org.apache.pulsar.metadata.api.extended.SessionEvent.SessionReestablished;
38-
import static org.hamcrest.MatcherAssert.assertThat;
39-
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
40-
import static org.hamcrest.Matchers.lessThanOrEqualTo;
38+
import static org.assertj.core.api.Assertions.assertThat;
4139
import static org.mockito.ArgumentMatchers.any;
4240
import static org.mockito.ArgumentMatchers.anyLong;
4341
import static org.mockito.ArgumentMatchers.eq;
@@ -258,10 +256,18 @@ public void channelOwnerTest() throws Exception {
258256
assertEquals(channelOwner1, channelOwner2);
259257
LeaderElectionService leaderElectionService1 = (LeaderElectionService) FieldUtils.readDeclaredField(
260258
channel1, "leaderElectionService", true);
261-
leaderElectionService1.close();
262-
waitUntilNewChannelOwner(channel2, channelOwner1);
263-
leaderElectionService1.start();
264-
waitUntilNewChannelOwner(channel1, channelOwner1);
259+
260+
// delete the leader node to trigger a new leader election
261+
pulsar1.getLocalMetadataStore().delete("/loadbalance/leader", Optional.of(-1L)).get();
262+
263+
// wait for leader to lose leadership
264+
Thread.sleep(500);
265+
266+
// wait for leader election to happen
267+
Awaitility.await().untilAsserted(() -> {
268+
assertThat(channel1.getChannelOwnerAsync().get()).isPresent();
269+
assertThat(channel2.getChannelOwnerAsync().get()).isPresent();
270+
});
265271

266272
var newChannelOwner1 = channel1.getChannelOwnerAsync().get(2, TimeUnit.SECONDS);
267273
var newChannelOwner2 = channel2.getChannelOwnerAsync().get(2, TimeUnit.SECONDS);
@@ -698,35 +704,31 @@ public void handleMetadataSessionEventTest() throws IllegalAccessException {
698704
var lastMetadataSessionEventTimestamp = getLastMetadataSessionEventTimestamp(channel1);
699705

700706
assertEquals(SessionReestablished, lastMetadataSessionEvent);
701-
assertThat(lastMetadataSessionEventTimestamp,
702-
greaterThanOrEqualTo(ts));
707+
assertThat(lastMetadataSessionEventTimestamp).isGreaterThanOrEqualTo(ts);
703708

704709
ts = System.currentTimeMillis();
705710
channel1.handleMetadataSessionEvent(SessionLost);
706711
lastMetadataSessionEvent = getLastMetadataSessionEvent(channel1);
707712
lastMetadataSessionEventTimestamp = getLastMetadataSessionEventTimestamp(channel1);
708713

709714
assertEquals(SessionLost, lastMetadataSessionEvent);
710-
assertThat(lastMetadataSessionEventTimestamp,
711-
greaterThanOrEqualTo(ts));
715+
assertThat(lastMetadataSessionEventTimestamp).isGreaterThanOrEqualTo(ts);
712716

713717
ts = System.currentTimeMillis();
714718
channel1.handleMetadataSessionEvent(ConnectionLost);
715719
lastMetadataSessionEvent = getLastMetadataSessionEvent(channel1);
716720
lastMetadataSessionEventTimestamp = getLastMetadataSessionEventTimestamp(channel1);
717721

718722
assertEquals(SessionLost, lastMetadataSessionEvent);
719-
assertThat(lastMetadataSessionEventTimestamp,
720-
lessThanOrEqualTo(ts));
723+
assertThat(lastMetadataSessionEventTimestamp).isLessThanOrEqualTo(ts);
721724

722725
ts = System.currentTimeMillis();
723726
channel1.handleMetadataSessionEvent(Reconnected);
724727
lastMetadataSessionEvent = getLastMetadataSessionEvent(channel1);
725728
lastMetadataSessionEventTimestamp = getLastMetadataSessionEventTimestamp(channel1);
726729

727730
assertEquals(SessionLost, lastMetadataSessionEvent);
728-
assertThat(lastMetadataSessionEventTimestamp,
729-
lessThanOrEqualTo(ts));
731+
assertThat(lastMetadataSessionEventTimestamp).isLessThanOrEqualTo(ts);
730732

731733
}
732734

0 commit comments

Comments
 (0)