Search in sources :

Example 1 with ZpeMetric

use of com.yahoo.athenz.zpe.ZpeMetric in project athenz by yahoo.

the class TestZpeMetric method testZpeMetric.

@Test
public void testZpeMetric() throws IOException {
    // setting the system property to write in file every 5 secs
    System.setProperty(ZpeConsts.ZPE_PROP_METRIC_WRITE_INTERVAL, "5000");
    final String metricDirPath = "/tmp/zpe-metrics";
    File metricsDir = new File(metricDirPath);
    metricsDir.mkdirs();
    System.setProperty(ZpeConsts.ZPE_PROP_METRIC_FILE_PATH, metricDirPath);
    final String TEST_DOMAIN = "test";
    ZpeMetric.statsEnabled = true;
    ZpeMetric test = new ZpeMetric();
    // cleaning the directory
    File dir = new File(test.getFilePath());
    if (dir.exists()) {
        for (File file : dir.listFiles()) {
            if (!file.isDirectory()) {
                file.delete();
            }
        }
    } else {
        dir.mkdirs();
    }
    // incrementing metrics for testing
    test.increment(ZpeConsts.ZPE_METRIC_NAME, AuthZpeClient.DEFAULT_DOMAIN);
    test.increment(ZpeConsts.ZPE_METRIC_NAME, AuthZpeClient.DEFAULT_DOMAIN);
    test.increment(ZpeConsts.ZPE_METRIC_NAME, AuthZpeClient.DEFAULT_DOMAIN);
    test.increment(ZpeConsts.ZPE_METRIC_NAME, AuthZpeClient.DEFAULT_DOMAIN);
    test.increment(ZpeConsts.ZPE_METRIC_NAME, AuthZpeClient.DEFAULT_DOMAIN);
    test.increment(ZpeConsts.ZPE_METRIC_NAME, AuthZpeClient.DEFAULT_DOMAIN);
    try {
        Thread.sleep(4000);
    } catch (InterruptedException e) {
    }
    test.increment(ZpeConsts.ZPE_METRIC_NAME, TEST_DOMAIN);
    test.increment(ZpeConsts.ZPE_METRIC_NAME, TEST_DOMAIN);
    test.increment(ZpeConsts.ZPE_METRIC_NAME, AuthZpeClient.DEFAULT_DOMAIN);
    test.increment(ZpeConsts.ZPE_METRIC_NAME, AuthZpeClient.DEFAULT_DOMAIN);
    test.increment(ZpeConsts.ZPE_METRIC_NAME, AuthZpeClient.DEFAULT_DOMAIN);
    test.increment(ZpeConsts.ZPE_METRIC_NAME, AuthZpeClient.DEFAULT_DOMAIN);
    try {
        Thread.sleep(2000);
    } catch (InterruptedException e) {
    }
    // Reading from the json file generated
    boolean sysDomainMetricVerified = false;
    boolean testDomainMetricVerified = false;
    for (File file : dir.listFiles()) {
        String filepath = test.getFilePath() + file.getName();
        Path path = Paths.get(filepath);
        DomainMetrics domainMetrics = JSON.fromBytes(Files.readAllBytes(path), DomainMetrics.class);
        // verifying the value of the metric
        List<DomainMetric> metricList = domainMetrics.getMetricList();
        for (DomainMetric metric : metricList) {
            if (metric.getMetricType().toString().equals(ZpeConsts.ZPE_METRIC_NAME)) {
                if (domainMetrics.getDomainName().equals("sys.auth")) {
                    assertEquals(10, metric.getMetricVal());
                    sysDomainMetricVerified = true;
                } else if (domainMetrics.getDomainName().equals("test")) {
                    assertEquals(2, metric.getMetricVal());
                    testDomainMetricVerified = true;
                }
            }
        }
    }
    assertTrue(sysDomainMetricVerified);
    assertTrue(testDomainMetricVerified);
    // unsetting the system property
    System.clearProperty(ZpeConsts.ZPE_PROP_METRIC_WRITE_INTERVAL);
}
Also used : Path(java.nio.file.Path) DomainMetrics(com.yahoo.athenz.zts.DomainMetrics) DomainMetric(com.yahoo.athenz.zts.DomainMetric) File(java.io.File) ZpeMetric(com.yahoo.athenz.zpe.ZpeMetric) Test(org.testng.annotations.Test)

Aggregations

ZpeMetric (com.yahoo.athenz.zpe.ZpeMetric)1 DomainMetric (com.yahoo.athenz.zts.DomainMetric)1 DomainMetrics (com.yahoo.athenz.zts.DomainMetrics)1 File (java.io.File)1 Path (java.nio.file.Path)1 Test (org.testng.annotations.Test)1