'Wait for an event to happen.
Args:
callback: Gets called whenever an event happens. If this returns a
truthy value then ``wait_for_replication`` returns, otherwise
it waits for another event.
timeout: How many milliseconds to wait for callback return a truthy
value.
Returns:
A deferred that resolves with the value returned by the callback.'

@defer.inlineCallbacks
def wait_for_replication(self, callback, timeout):

    listener = _NotificationListener(None)
    end_time = (self.clock.time_msec() + timeout)
    while True:
        listener.deferred = self.replication_deferred.observe()
        result = (yield callback())
        if result:
            break
        now = self.clock.time_msec()
        if (end_time <= now):
            break
        try:
            with PreserveLoggingContext():
                (yield self.clock.time_bound_deferred(listener.deferred, time_out=((end_time - now) / 1000.0)))
        except DeferredTimedOutError:
            break
        except defer.CancelledError:
            break
    defer.returnValue(result)
