Examples of MockClock


Examples of com.cloudera.flume.util.MockClock

   * This version uses a mock clock to test roll over from one epoch to the
   * next. We can check mock clock time as well to make sure it works.
   */
  @Test
  public void testTestCountHistoryClocked() throws IOException {
    MockClock m = new MockClock(0);
    Clock.setClock(m);

    CountHistoryReporter r = new CountHistoryReporter("test timeline", 500, t);
    r.open();
    Event e = new EventImpl("Test message".getBytes());

    // just a little forward to make things slighlty "out of sync"
    // 3
    r.append(e);
    r.append(e);
    r.append(e);
    m.forward(501);

    // 2
    r.append(e);
    r.append(e);
    m.forward(501);

    // 4
    r.append(e);
    r.append(e);
    r.append(e);
    r.append(e);
    m.forward(501);

    // 0 // this never registers!
    m.forward(501);

    // 1
    r.append(e);
    m.forward(501);

    r.append(e);

    long[] times = { 0, 501, 1002, 1503, 2004 };
    long[] ans = { 3, 2, 4, 0, 1 };
View Full Code Here

Examples of com.cloudera.flume.util.MockClock

      FlumeSpecException {
    master = new FlumeMaster(cfg);
    master.serve();
    MasterAdminServer mas = new MasterAdminServer(master, cfg);

    MockClock mclk = new MockClock(0);
    Clock.setClock(mclk);

    // populate status
    MasterClientServer delegate = new MasterClientServer(master, cfg);
    master.getSpecMan().setConfig("foo", "my-test-flow", "null", "null"); // set
                                                                          // at
                                                                          // time
                                                                          // 0
    long cfgtime = Clock.unixTime();
    boolean changed1, changed2, changed3, changed4;

    mclk.forward(250);

    // make the first stamp in the "past" to force an update
    changed1 = delegate.heartbeat("foo", "foo", NetUtils.localhost(),
        StatusManager.NodeState.HELLO, cfgtime); // new
    // node
    assertTrue(changed1);
    mclk.forward(500);

    changed2 = delegate.heartbeat("foo", "foo", NetUtils.localhost(),
        StatusManager.NodeState.HELLO, cfgtime);
    assertFalse(changed2);

    master.getSpecMan().setConfig("foo", "my-test-flow", "null", "null");
    long oldcfgtime = cfgtime;
    cfgtime = Clock.unixTime();

    mclk.forward(500);

    // "check with the last version we had"

    changed3 = delegate.heartbeat("foo", "foo", NetUtils.localhost(),
        StatusManager.NodeState.HELLO, oldcfgtime);
    assertTrue(changed3);
    mclk.forward(500);

    // "ok, we did a config update on the node"

    changed4 = delegate.heartbeat("foo", "foo", NetUtils.localhost(),
        StatusManager.NodeState.HELLO, cfgtime);
    assertFalse(changed4);
    mclk.forward(500);

    mas.getNodeStatuses();

    // calls
    mas.getConfigs();
View Full Code Here

Examples of com.cloudera.flume.util.MockClock

    PusherMockMaster master = new PusherMockMaster();

    MasterReportPusher reportPusher = new MasterReportPusher(FlumeConfiguration
        .createTestableConfiguration(), ReportManager.get(), master);

    MockClock mockClock = new MockClock(0);
    Clock.setClock(mockClock);

    Reportable reportable = new Reportable() {

      @Override
      public String getName() {
        return "test-reportable";
      }

      @Override
      public ReportEvent getReport() {
        ReportEvent r = new ReportEvent("test-reportable-report");
        r.setStringMetric("foo", "bar");
        return r;
      }

    };

    rptMan.add(reportable);

    reportPusher.start();

    // Trigger the report pusher
    mockClock.forward(FlumeConfiguration.get().getReporterPollPeriod() * 2);

    // Wait for 'real' 5s delay for report pusher to a) get on a CPU and b) do
    // the push
    assertTrue("putReports not called within 5 seconds", latch.await(5,
        TimeUnit.SECONDS));
View Full Code Here

Examples of com.cloudera.flume.util.MockClock

   * tests a series of messages being sent when append of the primary will fail
   * succeed or fail based on its twiddle state.
   */
  @Test
  public void testFailOverSink() throws IOException {
    MockClock mock = new MockClock(0);
    Clock.setClock(mock);

    CounterSink primary = new CounterSink("primary");
    CounterSink secondary = new CounterSink("backup");
    ExceptionTwiddleDecorator<CounterSink> twiddle =
        new ExceptionTwiddleDecorator<CounterSink>(primary);
    BackOffFailOverSink failsink =
        new BackOffFailOverSink(twiddle, secondary, 100, 10000); // 100 ms
    // initial
    // backoff,
    // 10000ms max
    // backoff
    failsink.open();

    Event e = new EventImpl("event".getBytes());
    // two successful appends to primary.
    failsink.append(e);
    failsink.append(e);
    System.out.println(mock);
    System.out.printf("pri: %4d sec: %4d fail: %4d\n", primary.getCount(),
        secondary.getCount(), failsink.getFails());
    Assert.assertEquals(2, primary.getCount());
    Assert.assertEquals(0, secondary.getCount());

    mock.forward(100);
    twiddle.setAppendOk(false); // go to fail over.
    failsink.append(e); // primary fails and automatically go to 2ndary
    System.out.println(mock);
    System.out.printf("pri: %4d sec: %4d fail: %4d\n", primary.getCount(),
        secondary.getCount(), failsink.getFails());
    Assert.assertEquals(1, failsink.getFails()); // one attempt on primary failed.
    Assert.assertEquals(2, primary.getCount()); // same as before,
    Assert.assertEquals(1, secondary.getCount()); // message went to the secondary

    mock.forward(50);
    failsink.append(e); // skip primary and just go to 2ndary
    System.out.println(mock);
    System.out.printf("pri: %4d sec: %4d fail: %4d\n", primary.getCount(),
        secondary.getCount(), failsink.getFails());
    Assert.assertEquals(1, failsink.getFails()); // still only one attempt on primary
    Assert.assertEquals(2, primary.getCount()); // same as before,
    Assert.assertEquals(2, secondary.getCount()); // message went to the secondary

    mock.forward(50);
    failsink.append(e); // after this fails backoff is now 200
    System.out.println(mock);
    System.out.printf("pri: %4d sec: %4d fail: %4d\n", primary.getCount(),
        secondary.getCount(), failsink.getFails());
    Assert.assertEquals(2, failsink.getFails()); // try primary
    Assert.assertEquals(0, primary.getCount()); // resets because primary restarted
    // (and still fails)
    Assert.assertEquals(3, secondary.getCount()); // but failover to secondary

    mock.forward(200);
    failsink.append(e); // should go to 2ndary, after this fails backoff is now
    // 400
    System.out.println(mock);
    System.out.printf("pri: %4d sec: %4d fail: %4d\n", primary.getCount(),
        secondary.getCount(), failsink.getFails());
    Assert.assertEquals(3, failsink.getFails());
    Assert.assertEquals(0, primary.getCount());
    Assert.assertEquals(4, secondary.getCount());

    twiddle.setAppendOk(true);
    failsink.append(e); // even through primary is ok, we are backing off
    System.out.println(mock);
    System.out.printf("pri: %4d sec: %4d fail: %4d\n", primary.getCount(),
        secondary.getCount(), failsink.getFails());
    Assert.assertEquals(3, failsink.getFails());
    Assert.assertEquals(0, primary.getCount());
    Assert.assertEquals(5, secondary.getCount());

    mock.forward(400);
    failsink.append(e); // now that the backoff has expired, we retry the
    // primary and succeed
    System.out.println(mock);
    System.out.printf("pri: %4d sec: %4d fail: %4d\n", primary.getCount(),
        secondary.getCount(), failsink.getFails());
View Full Code Here

Examples of com.cloudera.flume.util.MockClock

   * Purposely tests backoff timeout.
   */
  @Test
  public void testFailTimeout() throws IOException {
    System.out.println("===========================");
    MockClock mock = new MockClock(0);
    Clock.setClock(mock);

    CounterSink primary = new CounterSink("primary");
    CounterSink secondary = new CounterSink("backup");
    ExceptionTwiddleDecorator<CounterSink> twiddle =
        new ExceptionTwiddleDecorator<CounterSink>(primary);
    BackOffFailOverSink failsink =
        new BackOffFailOverSink(twiddle, secondary, 100, 1000); // 100 ms
    // initial
    // backoff,
    // 10000ms max
    // backoff
    failsink.open();

    Event e = new EventImpl("event".getBytes());

    mock.forward(100);
    twiddle.setAppendOk(false); // go to fail over.
    failsink.append(e);
    failsink.append(e);
    System.out.println(mock);
    System.out.printf("pri: %4d sec: %4d fail: %4d\n", primary.getCount(),
        secondary.getCount(), failsink.getFails());

    mock.forward(100);
    failsink.append(e);
    failsink.append(e);
    System.out.println(mock);
    System.out.printf("pri: %4d sec: %4d fail: %4d\n", primary.getCount(),
        secondary.getCount(), failsink.getFails());

    mock.forward(200);
    failsink.append(e);
    failsink.append(e);
    System.out.println(mock);
    System.out.printf("pri: %4d sec: %4d fail: %4d\n", primary.getCount(),
        secondary.getCount(), failsink.getFails());

    mock.forward(400);
    failsink.append(e);
    failsink.append(e);
    System.out.println(mock);
    System.out.printf("pri: %4d sec: %4d fail: %4d\n", primary.getCount(),
        secondary.getCount(), failsink.getFails());
    Assert.assertEquals(4, failsink.getFails());
    Assert.assertEquals(0, primary.getCount());
    Assert.assertEquals(8, secondary.getCount());

    mock.forward(800);
    failsink.append(e);
    failsink.append(e);
    System.out.println(mock);
    System.out.printf("pri: %4d sec: %4d fail: %4d\n", primary.getCount(),
        secondary.getCount(), failsink.getFails());
    Assert.assertEquals(5, failsink.getFails());
    Assert.assertEquals(0, primary.getCount());
    Assert.assertEquals(10, secondary.getCount());

    // without capping there would be no new fail here bug still the
    // twelve on the secondary count.
    mock.forward(1000);
    failsink.append(e);
    failsink.append(e);
    System.out.println(mock);
    System.out.printf("pri: %4d sec: %4d fail: %4d\n", primary.getCount(),
        secondary.getCount(), failsink.getFails());
View Full Code Here

Examples of com.cloudera.flume.util.MockClock

    LOG.info(stats.getName());
  }

  @Test
  public void testCheckup() throws IOException {
    MockClock clk = new MockClock(0);
    Clock.setClock(clk);
    StatusManager stats = new StatusManager();

    // foo is in state HELLO and has configuration numbered 0
    long prev = Clock.unixTime();
    boolean needsRefresh = stats.updateHeartbeatStatus(NetUtils.localhost(),
        "physnode", "foo", NodeState.HELLO, 0);
    LOG.info(stats.getNodeStatuses());
    assertTrue(needsRefresh);

    // move forward in time, but not far enough to trigger being lost
    clk.forward(FlumeConfiguration.get().getConfigHeartbeatPeriod() * 5);
    stats.checkup();
    StatusManager.NodeStatus ns = stats.getNodeStatuses().get("foo");
    assertEquals(0, ns.version);
    assertTrue(prev <= ns.lastseen);
    assertEquals(NodeState.HELLO, ns.state);
    prev = ns.lastseen;

    clk.forward(FlumeConfiguration.get().getConfigHeartbeatPeriod() * 20);
    stats.checkup();
    ns = stats.getNodeStatuses().get("foo");
    assertEquals(0, ns.version);
    assertTrue(prev <= ns.lastseen);
    assertEquals(NodeState.LOST, ns.state);
View Full Code Here

Examples of org.springside.modules.metrics.utils.Clock.MockClock

    scheduler.stop();
  }

  private void runReport(Reporter reporter) {
    MetricRegistry metricRegistry = new MetricRegistry();
    MockClock clock = new MockClock();
    Counter.clock = clock;
    Timer.clock = clock;

    // counter
    Counter counter = metricRegistry.counter(MetricRegistry.name("UserService", "getUser.counter"));
    counter.inc(4);
    Counter counter2 = metricRegistry.counter(MetricRegistry.name("UserService", "setUser.counter"));
    counter2.inc(6);
    clock.increaseTime(1000);

    // histogram
    Histogram histogram = metricRegistry.histogram(MetricRegistry.name("UserService", "getUser.latency"));
    for (int i = 1; i <= 100; i++) {
      histogram.update(i);
    }
    Histogram histogram2 = metricRegistry.histogram(MetricRegistry.name("UserService", "setUser.latency"));
    for (int i = 1; i <= 100; i++) {
      histogram2.update(i * 2);
    }

    // timer
    Timer timer = metricRegistry.timer(MetricRegistry.name("UserService", "getUser.timer"));
    for (int i = 1; i <= 10; i++) {
      TimerContext timerContext = timer.start();
      clock.increaseTime(25);
      timerContext.stop();
    }
    Timer timer2 = metricRegistry.timer(MetricRegistry.name("UserService", "setUser.timer"));
    for (int i = 1; i <= 10; i++) {
      TimerContext timerContext = timer2.start();
      clock.increaseTime(75);
      timerContext.stop();
    }

    // totally 2 seconds past
    ReportScheduler scheduler = new ReportScheduler(metricRegistry, reporter);
View Full Code Here

Examples of org.springside.modules.metrics.utils.Clock.MockClock

public class TimerTest {

  @Test
  public void normal() {
    MockClock clock = new MockClock();
    Timer.clock = clock;
    Counter.clock = clock;
    Timer timer = new Timer(new Double[] { 90d });

    TimerContext timerContext = timer.start();
    clock.increaseTime(200);
    timerContext.stop();

    TimerContext timer2 = timer.start();
    clock.increaseTime(300);
    timer2.stop();

    TimerMetric metric = timer.calculateMetric();

    assertThat(metric.counterMetric.totalCount).isEqualTo(2);
View Full Code Here

Examples of org.springside.modules.utils.Clock.MockClock

  @Test
  public void registerUser() {
    User user = UserData.randomNewUser();
    Date currentTime = new Date();
    accountService.setClock(new MockClock(currentTime));

    accountService.registerUser(user);

    // 验证user的角色,注册日期和加密后的密码都被自动更新了。
    assertThat(user.getRoles()).isEqualTo("user");
View Full Code Here

Examples of org.springside.modules.utils.Clock.MockClock

  @Test
  public void registerUser() {
    User user = UserData.randomNewUser();
    Date currentTime = new Date();
    accountService.setClock(new MockClock(currentTime));

    accountService.registerUser(user);

    // 验证user的角色,注册日期和加密后的密码都被自动更新了。
    assertThat(user.getRoles()).isEqualTo("user");
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. 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.