/* -- Consumer tests -- */
 @Test
 public void testConsumerName() throws Exception {
   final String name = "consumer1";
   ProfileCollector collector = getCollector(serverNode);
   ProfileConsumer cons = collector.getConsumer(name);
   assertEquals(name, cons.getName());
 }
  @Test
  public void testSampleLevel() throws Exception {
    final String name = "MySamples";
    ProfileCollector collector = getCollector(serverNode);
    ProfileConsumer cons1 = collector.getConsumer("cons1");
    final ProfileSample sample = cons1.registerSampleSource(name, true, -1, ProfileLevel.MAX);

    // Because the listener is running in a different thread, JUnit
    // is not able to report the assertions and failures.
    // Use an exchanger to synchronize between the threads and communicate
    // any problems.
    final Exchanger<AssertionError> errorExchanger = new Exchanger<AssertionError>();

    final List<Long> testValues = new ArrayList<Long>();
    testValues.add(101L);
    testValues.add(-22L);
    // The owner for our positive test.  The listener uses this owner
    // to find the ProfileReport for the task in this test.
    final Identity positiveOwner = new DummyIdentity("samplelevel");
    TestListener test =
        new TestListener(
            new TestSampleReport(
                name, positiveOwner,
                errorExchanger, testValues));
    profileCollector.addListener(test, true);
    txnScheduler.runTask(
        new TestAbstractKernelRunnable() {
          public void run() {
            // The default profile level is MIN so we don't expect
            // to see the samples.
            for (Long v : testValues) {
              sample.addSample(v);
            }
          }
        },
        taskOwner);

    AssertionError error = errorExchanger.exchange(null, 100, TimeUnit.MILLISECONDS);
    if (error != null) {
      throw new AssertionError(error);
    }

    cons1.setProfileLevel(ProfileLevel.MAX);
    txnScheduler.runTask(
        new TestAbstractKernelRunnable() {
          public void run() {
            // Because we bumped the consumer's profile level,
            // we expect the samples to appear
            for (Long v : testValues) {
              sample.addSample(v);
            }
          }
        },
        positiveOwner);

    error = errorExchanger.exchange(null, 100, TimeUnit.MILLISECONDS);
    if (error != null) {
      throw new AssertionError(error);
    }
  }
 /* -- Counter tests -- */
 @Test
 public void testCounterName() throws Exception {
   final String name = "counter";
   ProfileCollector collector = getCollector(serverNode);
   ProfileConsumer cons1 = collector.getConsumer("c1");
   ProfileCounter counter1 = cons1.registerCounter(name, true, ProfileLevel.MAX);
   assertEquals(name, counter1.getCounterName());
 }
 /* -- Operation tests -- */
 @Test
 public void testOperationName() throws Exception {
   final String name = "myOperation";
   ProfileCollector collector = getCollector(serverNode);
   ProfileConsumer cons1 = collector.getConsumer("c1");
   ProfileOperation op1 = cons1.registerOperation(name, ProfileLevel.MAX);
   assertEquals(name, op1.getOperationName());
 }
 /* -- Sample tests -- */
 @Test
 public void testSampleName() throws Exception {
   final String name = "SomeSamples";
   ProfileCollector collector = getCollector(serverNode);
   ProfileConsumer cons1 = collector.getConsumer("c1");
   ProfileSample sample1 = cons1.registerSampleSource(name, true, -1, ProfileLevel.MAX);
   assertEquals(name, sample1.getSampleName());
 }
  ChannelServiceStats(ProfileCollector collector) {
    ProfileConsumer consumer =
        collector.getConsumer(ProfileCollectorImpl.CORE_CONSUMER_PREFIX + "ChannelService");
    ProfileLevel level = ProfileLevel.MAX;
    ProfileDataType type = ProfileDataType.TASK_AND_AGGREGATE;

    // Manager operations
    createChannelOp = consumer.createOperation("createChannel", type, level);
    getChannelOp = consumer.createOperation("getChannel", type, level);
  }
  @Test
  public void testSampleType() throws Exception {
    ProfileCollector collector = getCollector(serverNode);
    ProfileConsumer cons1 = collector.getConsumer("c1");
    ProfileSample s1 = cons1.registerSampleSource("samples", true, -1, ProfileLevel.MAX);

    assertTrue(s1.isTaskLocal());

    ProfileSample s2 = cons1.registerSampleSource("other", false, -1, ProfileLevel.MAX);
    assertFalse(s2.isTaskLocal());
  }
  @Test
  public void testCounterType() throws Exception {
    ProfileCollector collector = getCollector(serverNode);
    ProfileConsumer cons1 = collector.getConsumer("c1");
    ProfileCounter counter = cons1.registerCounter("counter", true, ProfileLevel.MIN);

    assertTrue(counter.isTaskLocal());

    ProfileCounter counter1 = cons1.registerCounter("other", false, ProfileLevel.MIN);
    assertFalse(counter1.isTaskLocal());
  }
  @Test
  public void testCounterLevel() throws Exception {
    final String name = "MyCounter";
    ProfileCollector collector = getCollector(serverNode);
    ProfileConsumer cons1 = collector.getConsumer("c1");
    // Register a counter to be updated only at the max level
    final ProfileCounter counter = cons1.registerCounter(name, true, ProfileLevel.MAX);

    // Because the listener is running in a different thread, JUnit
    // is not able to report the assertions and failures.
    // Use an exchanger to synchronize between the threads and communicate
    // any problems.
    final Exchanger<AssertionError> errorExchanger = new Exchanger<AssertionError>();

    // The owner for our positive test.  The listener uses this owner
    // to find the ProfileReport for the task in this test.
    final Identity positiveOwner = new DummyIdentity("counterlevel");
    TestListener test =
        new TestListener(new TestCounterReport(name, positiveOwner, errorExchanger, 1));
    profileCollector.addListener(test, true);

    txnScheduler.runTask(
        new TestAbstractKernelRunnable() {
          public void run() {
            // The default profile level is MIN so we don't expect
            // to see the counter incremented.
            counter.incrementCount();
          }
        },
        taskOwner);

    AssertionError error = errorExchanger.exchange(null, 100, TimeUnit.MILLISECONDS);
    if (error != null) {
      throw new AssertionError(error);
    }

    cons1.setProfileLevel(ProfileLevel.MAX);
    txnScheduler.runTask(
        new TestAbstractKernelRunnable() {
          public void run() {
            // Because we bumped the consumer's profile level,
            // we expect the counter
            counter.incrementCount();
          }
        },
        positiveOwner);

    error = errorExchanger.exchange(null, 100, TimeUnit.MILLISECONDS);
    if (error != null) {
      throw new AssertionError(error);
    }
  }
  @Test
  public void testCounter() throws Exception {
    final String name = "counter";
    ProfileCollector collector = getCollector(serverNode);
    ProfileConsumer cons1 = collector.getConsumer("c1");
    // Register a counter to be noted at all profiling levels
    final ProfileCounter counter = cons1.registerCounter(name, true, ProfileLevel.MIN);

    // Because the listener is running in a different thread, JUnit
    // is not able to report the assertions and failures.
    // Use an exchanger to synchronize between the threads and communicate
    // any problems.
    final Exchanger<AssertionError> errorExchanger = new Exchanger<AssertionError>();

    // The owner for our positive test.  The listener uses this owner
    // to find the ProfileReport for the task in this test.
    final Identity positiveOwner = new DummyIdentity("owner");
    TestListener test =
        new TestListener(new TestCounterReport(name, positiveOwner, errorExchanger, 1));
    profileCollector.addListener(test, true);

    // We run with the myOwner because we expect to see the
    // value in the test report.
    txnScheduler.runTask(
        new TestAbstractKernelRunnable() {
          public void run() {
            counter.incrementCount();
          }
        },
        positiveOwner);

    AssertionError error = errorExchanger.exchange(null, 100, TimeUnit.MILLISECONDS);
    if (error != null) {
      // Rethrow with the original error as the cause so we see
      // both stack traces.
      throw new AssertionError(error);
    }

    txnScheduler.runTask(
        new TestAbstractKernelRunnable() {
          public void run() {}
        },
        taskOwner);

    error = errorExchanger.exchange(null, 100, TimeUnit.MILLISECONDS);
    if (error != null) {
      throw new AssertionError(error);
    }
  }
  @Test
  public void testConsumerSetCollectorLevel() throws Exception {
    ProfileCollector collector = getCollector(serverNode);
    ProfileConsumer cons1 = collector.getConsumer("c1");
    ProfileLevel cons1Level = cons1.getProfileLevel();
    assertEquals(profileCollector.getDefaultProfileLevel(), cons1Level);

    // Change default level from what the kernel set, make sure it
    // affects later consumers.
    profileCollector.setDefaultProfileLevel(ProfileLevel.MIN);
    ProfileConsumer cons2 = collector.getConsumer("c2");
    assertEquals(profileCollector.getDefaultProfileLevel(), cons2.getProfileLevel());
    // and make sure other consumers aren't affected
    assertEquals(cons1Level, cons1.getProfileLevel());
  }
  @Test
  public void testOperationMediumToMaxLevel() throws Exception {
    ProfileCollector collector = getCollector(serverNode);
    ProfileConsumer cons1 = collector.getConsumer("c1");
    final ProfileOperation op = cons1.registerOperation("something", ProfileLevel.MEDIUM);

    // Because the listener is running in a different thread, JUnit
    // is not able to report the assertions and failures.
    // Use an exchanger to synchronize between the threads and communicate
    // any problems.
    final Exchanger<AssertionError> errorExchanger = new Exchanger<AssertionError>();

    // The owner for our positive test.  The listener uses this owner
    // to find the ProfileReport for the task in this test.
    final Identity positiveOwner = new DummyIdentity("opmedtomax");
    TestListener test =
        new TestListener(new TestOperationReport(op, positiveOwner, errorExchanger));
    profileCollector.addListener(test, true);

    txnScheduler.runTask(
        new TestAbstractKernelRunnable() {
          public void run() {
            // We do not expect to see this reported.
            op.report();
          }
        },
        taskOwner);

    AssertionError error = errorExchanger.exchange(null, 100, TimeUnit.MILLISECONDS);
    if (error != null) {
      throw new AssertionError(error);
    }

    cons1.setProfileLevel(ProfileLevel.MAX);
    txnScheduler.runTask(
        new TestAbstractKernelRunnable() {
          public void run() {
            op.report();
          }
        },
        positiveOwner);

    error = errorExchanger.exchange(null, 100, TimeUnit.MILLISECONDS);
    if (error != null) {
      throw new AssertionError(error);
    }
  }
  @Test
  public void testConsumerSetLevel() throws Exception {
    ProfileCollector collector = getCollector(serverNode);
    ProfileConsumer cons1 = collector.getConsumer("c1");
    assertEquals(profileCollector.getDefaultProfileLevel(), cons1.getProfileLevel());

    cons1.setProfileLevel(ProfileLevel.MIN);
    assertEquals(ProfileLevel.MIN, cons1.getProfileLevel());
    cons1.setProfileLevel(ProfileLevel.MEDIUM);
    assertEquals(ProfileLevel.MEDIUM, cons1.getProfileLevel());
    cons1.setProfileLevel(ProfileLevel.MAX);
    assertEquals(ProfileLevel.MAX, cons1.getProfileLevel());
  }
  @Test
  public void testOperationTwice() throws Exception {
    final String name = "myOperation";
    ProfileCollector collector = getCollector(serverNode);
    ProfileConsumer cons1 = collector.getConsumer("c1");
    ProfileOperation op1 = cons1.registerOperation(name, ProfileLevel.MAX);

    // Try creating with same name and parameters
    ProfileOperation op2 = cons1.registerOperation(name, ProfileLevel.MAX);
    assertSame(op1, op2);

    // Try creating with same name and different parameters
    // Note we expect this behavior to change with upcoming API changes
    ProfileOperation op3 = cons1.registerOperation(name, ProfileLevel.MIN);
    assertSame(op1, op3);

    // Try creating with a different name
    ProfileOperation op4 = cons1.registerOperation("somethingelse", ProfileLevel.MAX);
    assertNotSame(op1, op4);
  }
  @Test
  public void testCounterTwice() throws Exception {
    final String name = "counter";
    ProfileCollector collector = getCollector(serverNode);
    ProfileConsumer cons1 = collector.getConsumer("c1");
    ProfileCounter counter1 = cons1.registerCounter(name, true, ProfileLevel.MAX);

    // Try creating with same name and parameters
    ProfileCounter counter2 = cons1.registerCounter(name, true, ProfileLevel.MAX);
    assertSame(counter1, counter2);

    // Try creating with same name and different parameters
    // Note we expect this behavior to change with upcoming API changes
    ProfileCounter op3 = cons1.registerCounter(name, false, ProfileLevel.MAX);
    assertSame(counter1, op3);

    // Try creating with a different name
    ProfileCounter counter4 = cons1.registerCounter("somethingelse", true, ProfileLevel.MAX);
    assertNotSame(counter1, counter4);
  }
  @Test
  public void testSampleTwice() throws Exception {
    final String name = "mySamples";
    ProfileCollector collector = getCollector(serverNode);
    ProfileConsumer cons1 = collector.getConsumer("c1");
    ProfileSample s1 = cons1.registerSampleSource(name, true, -1, ProfileLevel.MAX);

    // Try creating with same name and parameters
    ProfileSample s2 = cons1.registerSampleSource(name, true, -1, ProfileLevel.MAX);
    assertSame(s1, s2);

    // Try creating with same name and different parameters
    // Note we expect this behavior to change with upcoming API changes
    ProfileSample s3 = cons1.registerSampleSource(name, false, -1, ProfileLevel.MIN);
    assertSame(s1, s3);
    ProfileSample s4 = cons1.registerSampleSource(name, true, 25, ProfileLevel.MIN);
    assertSame(s1, s4);

    // Try creating with a different name
    ProfileSample s5 = cons1.registerSampleSource("somethingelse", true, -1, ProfileLevel.MAX);
    assertNotSame(s1, s5);
  }
  @Test
  public void testOperationMultiple() throws Exception {
    ProfileCollector collector = getCollector(serverNode);
    ProfileConsumer cons1 = collector.getConsumer("c1");
    final ProfileOperation op = cons1.registerOperation("something", ProfileLevel.MIN);
    final ProfileOperation op1 = cons1.registerOperation("else", ProfileLevel.MIN);

    // Because the listener is running in a different thread, JUnit
    // is not able to report the assertions and failures.
    // Use an exchanger to synchronize between the threads and communicate
    // any problems.
    final Exchanger<AssertionError> errorExchanger = new Exchanger<AssertionError>();

    final Identity myOwner = new DummyIdentity("me");
    TestListener test =
        new TestListener(
            new Runnable() {
              public void run() {
                AssertionError error = null;
                ProfileReport report = TestListener.report;
                if (report.getTaskOwner().equals(myOwner)) {
                  try {
                    List<ProfileOperation> ops = TestListener.report.getReportedOperations();
                    for (ProfileOperation po : ops) {
                      System.err.println(po);
                    }
                    int opIndex1 = ops.indexOf(op);
                    int opIndex2 = ops.lastIndexOf(op);
                    int op1Index1 = ops.indexOf(op1);
                    int op1Index2 = ops.lastIndexOf(op1);

                    // We expect to see op twice, and op1 once
                    assertTrue(opIndex1 != -1);
                    assertTrue(opIndex2 != -1);
                    assertTrue(op1Index1 != -1);
                    assertTrue(op1Index2 == op1Index1);

                    // We expect the op ordering to be maintained
                    assertTrue(opIndex1 < op1Index1);
                    assertTrue(op1Index1 < opIndex2);
                  } catch (AssertionError e) {
                    error = e;
                  }
                }

                // Signal that we're done, and return the exception
                try {
                  errorExchanger.exchange(error);
                } catch (InterruptedException ignored) {
                  // do nothing
                }
              }
            });
    profileCollector.addListener(test, true);

    txnScheduler.runTask(
        new TestAbstractKernelRunnable() {
          public void run() {
            // We expect to see the operation in the profile report
            op.report();
            op1.report();
            op.report();
          }
        },
        myOwner);

    AssertionError error = errorExchanger.exchange(null, 100, TimeUnit.MILLISECONDS);
    if (error != null) {
      throw new AssertionError(error);
    }
  }