Example usage for java.util Map wait

List of usage examples for java.util Map wait

Introduction

In this page you can find the example usage for java.util Map wait.

Prototype

public final native void wait(long timeoutMillis) throws InterruptedException;

Source Link

Document

Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed.

Usage

From source file:org.killbill.notificationq.TestNotificationQueue.java

@Test(groups = "slow")
public void testManyNotifications() throws Exception {
    final Map<NotificationEvent, Boolean> expectedNotifications = new TreeMap<NotificationEvent, Boolean>();

    final NotificationQueue queue = queueService.createNotificationQueue("test-svc", "many",
            new NotificationQueueHandler() {
                @Override//from  w w  w.  j av a 2  s.  c  o m
                public void handleReadyNotification(final NotificationEvent eventJson,
                        final DateTime eventDateTime, final UUID userToken, final Long searchKey1,
                        final Long searchKey2) {
                    synchronized (expectedNotifications) {
                        log.info("Handler received key: " + eventJson.toString());

                        expectedNotifications.put(eventJson, Boolean.TRUE);
                        expectedNotifications.notify();
                    }
                }
            });
    queue.startQueue();

    final DateTime now = clock.getUTCNow();
    final int MAX_NOTIFICATIONS = 100;
    for (int i = 0; i < MAX_NOTIFICATIONS; i++) {

        final int nextReadyTimeIncrementMs = 1000;

        final UUID key = UUID.randomUUID();
        final int currentIteration = i;

        final NotificationEvent eventJson = new TestNotificationKey(new Integer(i).toString());
        expectedNotifications.put(eventJson, Boolean.FALSE);

        final DBI dbi = getDBI();
        dbi.inTransaction(new TransactionCallback<Object>() {
            @Override
            public Object inTransaction(final Handle conn, final TransactionStatus status) throws Exception {
                queue.recordFutureNotificationFromTransaction(conn.getConnection(),
                        now.plus((currentIteration + 1) * nextReadyTimeIncrementMs), eventJson, TOKEN_ID,
                        SEARCH_KEY_1, SEARCH_KEY_2);
                return null;
            }
        });

        // Move time in the future after the notification effectiveDate
        if (i == 0) {
            ((ClockMock) clock).setDeltaFromReality(nextReadyTimeIncrementMs);
        } else {
            ((ClockMock) clock).addDeltaFromReality(nextReadyTimeIncrementMs);
        }
    }

    // Wait a little longer since there are a lot of callback that need to happen
    int nbTry = MAX_NOTIFICATIONS + 1;
    boolean success = false;
    do {
        synchronized (expectedNotifications) {
            final Collection<Boolean> completed = Collections2.filter(expectedNotifications.values(),
                    new Predicate<Boolean>() {
                        @Override
                        public boolean apply(final Boolean input) {
                            return input;
                        }
                    });

            if (completed.size() == MAX_NOTIFICATIONS) {
                success = true;
                break;
            }
            log.info(String.format("BEFORE WAIT : Got %d notifications at time %s (real time %s)",
                    completed.size(), clock.getUTCNow(), new DateTime()));
            expectedNotifications.wait(1000);
        }
    } while (nbTry-- > 0);

    queue.stopQueue();
    log.info("GOT SIZE " + Collections2.filter(expectedNotifications.values(), new Predicate<Boolean>() {
        @Override
        public boolean apply(final Boolean input) {
            return input;
        }
    }).size());
    assertEquals(success, true);
}