@Test(timeout = 30_000)
public void testMonitorWorksForMultipleLeaderUpdates() throws Exception {

    // Note we intentionally didn't set the initial value of master description because we'd like to make sure
    // that the monitor will work property even if it fails occasionally (in this case, it will fail to deserialize
    // the master description in the very beginning
    ExtTestSubscriber<MasterDescription> leaderSubscriber = new ExtTestSubscriber<>();
    masterMonitor.getLeaderObservable().filter(Objects::nonNull).subscribe(leaderSubscriber);
    for (int i = 0; i < 5; i++) {
        curator.setData()
                .forPath(zkPaths.getLeaderAnnouncementPath(), ObjectMappers.defaultMapper().writeValueAsBytes(newMasterDescription(i)));
        // Try a few times, as we can get update for the same entity more than once.
        for (int j = 0; j < 3; j++) {
            MasterDescription newLeader = leaderSubscriber.takeNext(5, TimeUnit.SECONDS);
            if (newLeader != null && newLeader.getApiPort() == i) {
                return;
            }
        }
        fail("Did not received TitusMaster update for iteration " + i);
    }
}