/*
   * Test that Metrics are automatically unregistered after the job is closed
   */
  @Test
  public void automaticMetricCleanup1() throws Exception {
    // Declare topology with custom metric oplet
    Topology t = newTopology();
    AtomicInteger n = new AtomicInteger(0);
    TStream<Integer> ints = t.poll(() -> n.incrementAndGet(), 10, TimeUnit.MILLISECONDS);
    ints.pipe(new TestOplet<Integer>());

    // Submit job
    Future<? extends Job> fj = getSubmitter().submit(t);
    Job job = fj.get();
    Thread.sleep(TimeUnit.MILLISECONDS.toMillis(50));

    // At least one tuple was processed
    int tupleCount = n.get();
    assertTrue("Expected more tuples than " + tupleCount, tupleCount > 0);

    // Each test oplet registers two metrics
    Map<String, Metric> all = metricRegistry.getMetrics();
    assertEquals(2, all.size());

    // After close all metrics have been unregistered
    job.stateChange(Job.Action.CLOSE);
    assertEquals(0, all.size());
  }