Skip to content

Commit

Permalink
W-17464266: Add unit tests for cc-metrics-reporter
Browse files Browse the repository at this point in the history
  • Loading branch information
peterzxu-crm committed Jan 6, 2025
1 parent e560b63 commit 5356061
Show file tree
Hide file tree
Showing 5 changed files with 177 additions and 0 deletions.
1 change: 1 addition & 0 deletions cc-metrics-reporter/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ publishing {

test {
useJUnitPlatform()
jvmArgs += '--add-opens=jdk.management/com.sun.management.internal=ALL-UNNAMED'
}

jacocoTestReport {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* Copyright (c) 2018, salesforce.com, inc.
* All rights reserved.
* SPDX-License-Identifier: BSD-3-Clause
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
package com.sfcc.um.metrics_reporter.metricset;

import com.codahale.metrics.Metric;
import org.junit.jupiter.api.Test;

import java.util.Map;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class TestOperatingSystemGaugeSet {
@Test
public void test() {
OperatingSystemGaugeSet osg = new OperatingSystemGaugeSet();
Map<String, Metric> metrics = osg.getMetrics();
assertEquals(9, metrics.size());
assertTrue(metrics.containsKey("committedVirtualMemorySize"));
assertTrue(metrics.containsKey("totalSwapSpaceSize"));
assertTrue(metrics.containsKey("freeSwapSpaceSize"));
assertTrue(metrics.containsKey("processCpuTime"));
assertTrue(metrics.containsKey("freePhysicalMemorySize"));
assertTrue(metrics.containsKey("totalPhysicalMemorySize"));
assertTrue(metrics.containsKey("fd.usage"));
assertTrue(metrics.containsKey("systemCpuLoad"));
assertTrue(metrics.containsKey("processCpuLoad"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/**
* Copyright (c) 2018, salesforce.com, inc.
* All rights reserved.
* SPDX-License-Identifier: BSD-3-Clause
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
package com.sfcc.um.metrics_reporter.reporter;

import com.codahale.metrics.*;
import com.sfcc.um.metrics_reporter.transport.GraphiteTransport;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.List;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.concurrent.TimeUnit;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class TestGraphiteReporter {
@Test
public void testGraphiteReporter() {
MetricRegistry registry = new MetricRegistry();
GraphiteReporter.Builder builder = GraphiteReporter.forRegistry(registry);
TestGraphiteTransport graphiteTransport = new TestGraphiteTransport();
try (GraphiteReporter graphiteReporter = builder
.withPrefix("prefix")
.withClock(Clock.defaultClock())
.convertRatesTo(TimeUnit.SECONDS)
.convertDurationsTo(TimeUnit.MILLISECONDS)
.filter(MetricFilter.ALL)
.build(graphiteTransport)) {
SortedMap<String, Gauge> gauges = new TreeMap<>();
gauges.put("gauge", (Gauge<Double>) () -> 1.0);
SortedMap<String, Counter> counters = new TreeMap<>();
counters.put("counter", new Counter());
SortedMap<String, Histogram> histograms = new TreeMap<>();
histograms.put("histogram", new Histogram(new SlidingWindowReservoir(1)));
SortedMap<String, Meter> meters = new TreeMap<>();
meters.put("meter", new Meter());
SortedMap<String, Timer> timers = new TreeMap<>();
timers.put("timer", new Timer());
graphiteReporter.report(gauges, counters, histograms, meters, timers);
}
assertEquals(99, graphiteTransport.data.size());
}

private static class TestGraphiteTransport implements GraphiteTransport {
private final List<String> data = new ArrayList<>();

@Override
public GraphiteTransport open() {
return null;
}

@Override
public void send(String name, String value, long timestamp) {
data.add(name + " " + value + " " + timestamp);
}

@Override
public void close() {
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* Copyright (c) 2018, salesforce.com, inc.
* All rights reserved.
* SPDX-License-Identifier: BSD-3-Clause
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
package com.sfcc.um.metrics_reporter.transport;

import com.codahale.metrics.MetricRegistry;
import org.junit.jupiter.api.Test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.fail;

public class TestGraphiteTCPTransport {
@Test
public void test() throws Exception {
String name = "foo.bar";
String value = "123.45";
int current = (int) (System.currentTimeMillis() / 1000);
ServerSocket serverSocket = new ServerSocket(0);
Thread thread = new Thread(() -> {
try (Socket socket = serverSocket.accept()) {
BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String line = reader.readLine();
assertNotNull(line);
String[] parts = line.split(" ");
assertEquals(name, parts[0]);
assertEquals(value, parts[1]);
assertEquals(current, Integer.parseInt(parts[2]));
} catch (IOException e) {
fail(e.getMessage());
}
});
thread.start();
int port = serverSocket.getLocalPort();
MetricRegistry metricRegistry = new MetricRegistry();
GraphiteTCPTransport graphiteTCPTransport = new GraphiteTCPTransport("127.0.0.1", port, 1, metricRegistry);
graphiteTCPTransport.open();
graphiteTCPTransport.send(name, value, current);
graphiteTCPTransport.close();
serverSocket.close();
thread.join();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Copyright (c) 2018, salesforce.com, inc.
* All rights reserved.
* SPDX-License-Identifier: BSD-3-Clause
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
package com.sfcc.um.metrics_reporter.transport;

import com.codahale.metrics.MetricRegistry;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertInstanceOf;

public class TestGraphiteTransportBuilder {
@Test
public void testBuild() {
MetricRegistry metricRegistry = new MetricRegistry();
GraphiteTransportBuilder graphiteTransportBuilder = new GraphiteTransportBuilder(GraphiteTransportType.TCP, "localhost", 80, metricRegistry);
GraphiteTransport graphiteTransport = graphiteTransportBuilder.batchSize(1).metricRegistry(metricRegistry).build();
assertInstanceOf(GraphiteTCPTransport.class, graphiteTransport);
graphiteTransportBuilder = new GraphiteTransportBuilder(GraphiteTransportType.UDP, "localhost", 80, metricRegistry);
graphiteTransport = graphiteTransportBuilder.build();
assertInstanceOf(GraphiteUDPTransport.class, graphiteTransport);
}
}

0 comments on commit 5356061

Please sign in to comment.