-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
W-17464266: Add unit tests for cc-metrics-reporter
- Loading branch information
1 parent
e560b63
commit 5356061
Showing
5 changed files
with
177 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
...ter/src/test/java/com/sfcc/um/metrics_reporter/metricset/TestOperatingSystemGaugeSet.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")); | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
...cs-reporter/src/test/java/com/sfcc/um/metrics_reporter/reporter/TestGraphiteReporter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() { | ||
} | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
...porter/src/test/java/com/sfcc/um/metrics_reporter/transport/TestGraphiteTCPTransport.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
...er/src/test/java/com/sfcc/um/metrics_reporter/transport/TestGraphiteTransportBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |