Package com.google.common.eventbus

Examples of com.google.common.eventbus.EventBus


    @Test
    public void shouldReceiveMultipleEventTypesFromBus() throws Exception {

        // given
        EventBus eventBus = new EventBus("test");
        EventListener listener = new EventListener();
        EventReader reader = new EventReader();
        StringEventListener stringEventListener = new StringEventListener();

        eventBus.register(listener);
        eventBus.register(reader);
        eventBus.register(stringEventListener);

        // when
        eventBus.post(new OurTestEvent(200));
        eventBus.post(new StringTestEvent("Hello events!"));


        // then
        assertThat(reader.getLastMessage()).isEqualTo(200);
        assertThat(listener.getLastMessage()).isEqualTo(200);
View Full Code Here


    @Test
    public void shouldNotReceiveEventAfterUnsubscribe() throws Exception {

        // given
        EventBus eventBus = new EventBus("test");
        EventListener listener = new EventListener();
        EventReader reader = new EventReader();

        eventBus.register(listener);
        eventBus.register(reader);

        // when
        eventBus.post(new OurTestEvent(200));


        // then
        assertThat(reader.getLastMessage()).isEqualTo(200);
        assertThat(listener.getLastMessage()).isEqualTo(200);
       
       
        //when
        eventBus.unregister(reader);
        eventBus.post(new OurTestEvent(300));


        // then
        assertThat(listener.getLastMessage()).isEqualTo(300);
        // this one was unregistered
View Full Code Here

    @Test
    public void shouldDetectEventWithoutListeners() throws Exception {

        // given
        EventBus eventBus = new EventBus("test");

        DeadEventListener deadEventListener = new DeadEventListener();
        eventBus.register(deadEventListener);

        // when
        eventBus.post(new OurTestEvent(200));

        assertThat(deadEventListener.isNotDelivered()).isTrue();
    }
View Full Code Here

    @Test
    public void shouldGetEventsFromSubclass() throws Exception {

        // given
        EventBus eventBus = new EventBus("test");
        IntegerListener integerListener = new IntegerListener();
        NumberListener numberListener = new NumberListener();
        eventBus.register(integerListener);
        eventBus.register(numberListener);

        // when
        eventBus.post(new Integer(100));

        // then
        assertThat(integerListener.getLastMessage()).isEqualTo(100);
        assertThat(numberListener.getLastMessage()).isEqualTo(100);

        //when
        eventBus.post(new Long(200L));

        // then
        // this one should has the old value as it listens only for Integers
        assertThat(integerListener.getLastMessage()).isEqualTo(100);
        assertThat(numberListener.getLastMessage()).isEqualTo(200L);
View Full Code Here

    @Test
    public void shouldReceiveEvent() throws Exception {

        // given
        EventBus eventBus = new EventBus("test");
        EventListener listener = new EventListener();

        eventBus.register(listener);

        // when
        eventBus.post(new OurTestEvent(200));

        // then
        assertThat(listener.getLastMessage()).isEqualTo(200);
    }
View Full Code Here

                ph.setProperty(prop.getKey().toString(), prop.getValue().toString(), false);
            }
        }

        classLoader = loader;       
        eventBus = new EventBus();
    }
View Full Code Here

        return null;
    }

    @SuppressWarnings("PMD.AvoidCatchingThrowable")
    private void runEvaluation() throws IOException, RecommenderBuildException {
        EventBus bus = task.getProject().getEventBus();
        bus.post(JobEvents.started(this));
        Closer closer = Closer.create();
        try {
            outputs = task.getOutputs().getPrefixed(algorithmInfo, dataSet);
            TableWriter userResults = outputs.getUserWriter();
            List<Object> outputRow = Lists.newArrayList();

            logger.info("Building {} on {}", algorithmInfo, dataSet);
            StopWatch buildTimer = new StopWatch();
            buildTimer.start();
            buildRecommender();
            buildTimer.stop();
            logger.info("Built {} in {}", algorithmInfo.getName(), buildTimer);

            logger.info("Measuring {} on {}", algorithmInfo.getName(), dataSet.getName());

            StopWatch testTimer = new StopWatch();
            testTimer.start();
            List<Object> userRow = Lists.newArrayList();

            List<MetricWithAccumulator<?>> accumulators = Lists.newArrayList();

            for (Metric<?> eval: outputs.getMetrics()) {
                accumulators.add(makeMetricAccumulator(eval));
            }

            LongSet testUsers = dataSet.getTestData().getUserDAO().getUserIds();
            final NumberFormat pctFormat = NumberFormat.getPercentInstance();
            pctFormat.setMaximumFractionDigits(2);
            pctFormat.setMinimumFractionDigits(2);
            final int nusers = testUsers.size();
            logger.info("Testing {} on {} ({} users)", algorithmInfo, dataSet, nusers);
            int ndone = 0;
            for (LongIterator iter = testUsers.iterator(); iter.hasNext();) {
                if (Thread.interrupted()) {
                    throw new InterruptedException("eval job interrupted");
                }
                long uid = iter.nextLong();
                userRow.add(uid);
                userRow.add(null); // placeholder for the per-user time
                assert userRow.size() == 2;

                Stopwatch userTimer = Stopwatch.createStarted();
                TestUser test = getUserResults(uid);

                userRow.add(test.getTrainHistory().size());
                userRow.add(test.getTestHistory().size());

                for (MetricWithAccumulator<?> accum : accumulators) {
                    List<Object> ures = accum.measureUser(test);
                    if (ures != null) {
                        userRow.addAll(ures);
                    }
                }
                userTimer.stop();
                userRow.set(1, userTimer.elapsed(TimeUnit.MILLISECONDS) * 0.001);
                if (userResults != null) {
                    try {
                        userResults.writeRow(userRow);
                    } catch (IOException e) {
                        throw new RuntimeException("error writing user row", e);
                    }
                }
                userRow.clear();

                ndone += 1;
                if (ndone % 100 == 0) {
                    testTimer.split();
                    double time = testTimer.getSplitTime();
                    double tpu = time / ndone;
                    double tleft = (nusers - ndone) * tpu;
                    logger.info("tested {} of {} users ({}), ETA {}",
                                ndone, nusers, pctFormat.format(((double) ndone) / nusers),
                                DurationFormatUtils.formatDurationHMS((long) tleft));
                }
            }
            testTimer.stop();
            logger.info("Tested {} in {}", algorithmInfo.getName(), testTimer);

            writeMetricValues(buildTimer, testTimer, outputRow, accumulators);
            bus.post(JobEvents.finished(this));
        } catch (Throwable th) {
            bus.post(JobEvents.failed(this, th));
            throw closer.rethrow(th, RecommenderBuildException.class);
        } finally {
            try {
                cleanup();
            } finally {
View Full Code Here

      replay(mockFuture);
      replay(schedulerMock);

      AsyncMonitor<Object> monitor = mockMonitor(schedulerMock, new Object(), mockFunction(MonitorStatus.DONE),
            new EventBus());

      assertNull(monitor.getFuture());
      assertNull(monitor.getTimeout());

      monitor.startMonitoring(null, TimeUnit.MILLISECONDS);
View Full Code Here

   @Test(expectedExceptions = NullPointerException.class, expectedExceptionsMessageRegExp = "timeUnit must not be null when using timeouts")
   public void testStartMonitoringWithNullTimeout() {
      ScheduledExecutorService schedulerMock = EasyMock.createMock(ScheduledExecutorService.class);
      AsyncMonitor<Object> monitor = mockMonitor(schedulerMock, new Object(), mockFunction(MonitorStatus.DONE),
            new EventBus());

      monitor.startMonitoring(100L, null);
   }
View Full Code Here

      replay(mockFuture);
      replay(schedulerMock);

      AsyncMonitor<Object> monitor = mockMonitor(schedulerMock, new Object(), mockFunction(MonitorStatus.DONE),
            new EventBus());

      assertNull(monitor.getFuture());
      assertNull(monitor.getTimeout());

      // If the maxWait parameter is null, timeUnit is not required
View Full Code Here

TOP

Related Classes of com.google.common.eventbus.EventBus

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.