Skip to content

Commit

Permalink
W-17464266: Add unit tests to bump up coverage over 70%
Browse files Browse the repository at this point in the history
  • Loading branch information
peterzxu-crm committed Dec 24, 2024
1 parent 61c1db7 commit befdb02
Show file tree
Hide file tree
Showing 63 changed files with 2,063 additions and 75 deletions.
4 changes: 2 additions & 2 deletions carbonj.service/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ RUN yum update -y && \
yum install -y gcc-c++ gcc make libtool automake autoconf make python3-devel && \
rpm --import http://repos.azulsystems.com/RPM-GPG-KEY-azulsystems && \
yum install -y https://cdn.azul.com/zulu/bin/zulu-repo-1.0.0-1.noarch.rpm && \
yum install -y https://mirror.stream.centos.org/9-stream/AppStream/$(uname -m)/os/Packages/pcp-conf-6.2.1-1.el9.$(uname -m).rpm && \
yum install -y https://mirror.stream.centos.org/9-stream/AppStream/$(uname -m)/os/Packages/pcp-libs-6.2.1-1.el9.$(uname -m).rpm && \
yum install -y https://mirror.stream.centos.org/9-stream/AppStream/$(uname -m)/os/Packages/pcp-conf-6.2.2-6.el9.$(uname -m).rpm && \
yum install -y https://mirror.stream.centos.org/9-stream/AppStream/$(uname -m)/os/Packages/pcp-libs-6.2.2-6.el9.$(uname -m).rpm && \
#
# If sysstat version is updated, confirm iolog.sh execution and update associated version check in entrypoint.sh
#
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,6 @@
*/
package com.demandware.carbonj.service.db.model;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

import com.demandware.carbonj.service.accumulator.MetricAggregationPolicy;
import com.google.common.base.Preconditions;

import com.demandware.carbonj.service.strings.StringsCache;

public class MsgPackMetric
{
final public String path;
Expand All @@ -26,4 +16,4 @@ public MsgPackMetric( Metric metric )
this.path = metric.name;
this.isLeaf = metric.isLeaf();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
package com.demandware.carbonj.service.db.model;

import java.util.List;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonCreator;

Expand All @@ -32,12 +31,7 @@ public class MsgPackSeries

public MsgPackSeries( Series series)
{
this.start = series.start;
this.end = series.end;
this.step = series.step;
this.name = series.name;
this.pathExpression = series.name;
this.values = series.values;
this(series.start, series.end, series.step, series.name, series.name, series.values);
}

@JsonCreator
Expand Down Expand Up @@ -66,4 +60,4 @@ public String toString()
", values=" + values +
'}';
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.stream.Collectors;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -26,9 +25,9 @@ public class StorageAggregationPolicySource
private static final Logger log = LoggerFactory.getLogger( StorageAggregationPolicySource.class );

// reuse same instance across multiple metrics.
private CopyOnWriteArrayList<AggregationPolicy> policies = new CopyOnWriteArrayList<>( );
private final CopyOnWriteArrayList<AggregationPolicy> policies = new CopyOnWriteArrayList<>( );

private StorageAggregationRulesLoader rulesLoader;
private final StorageAggregationRulesLoader rulesLoader;

public StorageAggregationPolicySource( StorageAggregationRulesLoader rulesLoader)
{
Expand Down Expand Up @@ -75,15 +74,11 @@ public synchronized void cleanup()
{
log.info("checking for obsolete aggregation policies to remove from cache");
List<AggregationPolicy> obsolete = policies.stream()
.filter( p -> p.configChanged() )
.collect( Collectors.toList());
.filter(AggregationPolicy::configChanged)
.toList();
// no need to keep policies that represent obsolete config.
policies.removeAll( obsolete );
log.info("purged obsolete aggregation policies from cache. Number of obsolete policies found: "
+ obsolete.size() + ", total number of policies after purge: " + policies.size());
}

public StorageAggregationRulesLoader getRulesLoader() {
return rulesLoader;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,15 @@ public class InputQueue<T> extends Thread implements Consumer<T>, StatsAware, Cl

private final QueueProcessor<T> pointProcessor;

private int batchSize;
private final int batchSize;

private BlockingQueue<T> queue;
private final BlockingQueue<T> queue;

private volatile boolean stop = false;

private long emptyQueuePauseMillis;
private final long emptyQueuePauseMillis;

private RejectionHandler rh;

private final int queueCapacity;
private final RejectionHandler<T> rh;

public InputQueue(MetricRegistry metricRegistry, String name, QueueProcessor<T> queueProcessor, int queueSize,
RejectionHandler<T> rejectionHandler, int batchSize,
Expand All @@ -65,7 +63,6 @@ public InputQueue(MetricRegistry metricRegistry, String name, QueueProcessor<T>
this.pointProcessor = Preconditions.checkNotNull(queueProcessor);
this.batchSize = batchSize;
this.emptyQueuePauseMillis = emptyQueuePauseMillis;
this.queueCapacity = queueSize;
this.queue = new ArrayBlockingQueue<>( queueSize );
this.rh = rejectionHandler;

Expand Down Expand Up @@ -100,13 +97,6 @@ public void drain()
@Override
public void run()
{

if ( queue == null )
{
stop = true;
return;
}

// queue consumer loop.
while ( true )
{
Expand Down Expand Up @@ -202,19 +192,7 @@ public void refreshStats()

public int queuedItemsCount()
{
if ( queue == null )
{
return 0;
}
else
{
return queue.size();
}
}

public int queueCapacity()
{
return queueCapacity;
return queue.size();
}

private String queueSizeGaugeName(String name)
Expand Down
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.demandware.carbonj.service.accumulator;

import com.demandware.carbonj.service.engine.DataPoint;

import java.util.concurrent.atomic.AtomicInteger;

public class CountingLatePointLogger implements LatePointLogger {

private final AtomicInteger latePoints;

public CountingLatePointLogger(AtomicInteger latePoints) {
this.latePoints = latePoints;
}

@Override
public void logLatePoint(DataPoint m, long now, Reason r, String context) {
latePoints.incrementAndGet();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* 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.demandware.carbonj.service.accumulator;

import org.junit.jupiter.api.Test;

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

public class TestAggregateFunction {
@Test
public void testAggregateFunction() {
AggregateFunction aggregateFunction = AggregateFunction.create("foo.bar.p95", MetricAggregationMethod.CUSTOM1);
assertInstanceOf(AggregateFunction.AvgAggregateFunction.class, aggregateFunction);
assertEquals(0, aggregateFunction.apply());
try {
aggregateFunction.getValues();
fail("Should have thrown an exception");
} catch (UnsupportedOperationException e) {
}
aggregateFunction = AggregateFunction.create("foo.bar.latency", MetricAggregationMethod.LATENCY);
assertInstanceOf(AggregateFunction.LatencyAggregateFunction.class, aggregateFunction);
assertEquals(0, aggregateFunction.apply());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* 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.demandware.carbonj.service.accumulator;

import com.codahale.metrics.MetricRegistry;
import com.demandware.carbonj.service.engine.DataPoint;
import org.junit.jupiter.api.Test;

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

public class TestLatePointLoggerImpl {
@Test
public void test() {
MetricRegistry metricRegistry = new MetricRegistry();
LatePointLoggerImpl latePointLogger = new LatePointLoggerImpl(metricRegistry);
latePointLogger.logLatePoint(new DataPoint("foo.bar", 123, 60), 120, LatePointLogger.Reason.SLOT_CLOSED, "Context");
assertEquals(1, metricRegistry.getCounters().get("aggregator.skippedDelayed").getCount());
assertEquals(60, metricRegistry.getHistograms().get("aggregator.pointAgeHistogram").getSnapshot().get95thPercentile());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* 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.demandware.carbonj.service.accumulator;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

public class TestMetricAggregationRule {
@Test
public void test() {
String aggregationRule = "ocapi.<realm>.<tenant>.<metric> (60) c = custom1 pod[0-9]{3,6}.ecom.<realm>.<tenant>.*.*.ocapi.clients.*.<<metric>>";
MetricAggregationRule metricAggregationRule = MetricAggregationRule.parseDefinition(aggregationRule, 1, false);
assertFalse(metricAggregationRule.isStopRule());
assertEquals(MetricAggregationMethod.CUSTOM1, metricAggregationRule.getMethod());
assertTrue(metricAggregationRule.equals(metricAggregationRule));
assertFalse(metricAggregationRule.equals(new Object()));
assertTrue(metricAggregationRule.hashCode() > 0);
assertEquals(String.format("com.demandware.carbonj.service.accumulator.MetricAggregationRule@%x", System.identityHashCode(metricAggregationRule)), metricAggregationRule.toString());
assertFalse(metricAggregationRule.equals(MetricAggregationRule.parseDefinition(
"ocapi.<realm>.<tenant>.<metric> (60) c = sum pod[0-9]{3,6}.ecom.<realm>.<tenant>.*.*.ocapi.clients.*.<<metric>>", 1, false)));
MetricAggregationRule.Result result = metricAggregationRule.apply("pod807.ecom.bgzz.bgzz_prd.blade_1.bgzz_prd.ocapi.clients.client.foo.bar");
assertTrue(result.equals(result));
assertFalse(result.equals(null));
assertNotEquals(0, result.hashCode());
assertEquals("Result{aggregateName=ocapi.bgzz.bgzz_prd.foo.bar, method=CUSTOM1, dropOriginal=false}", result.toString());
assertFalse(result.equals(metricAggregationRule.apply("pod807.ecom.bgzz.bgzz_prd.blade_1.bgzz_prd.ocapi.clients.client.foo.bar2")));

String aggregationRule2 = "ocapi.<realm>.<tenant>.<metric> (60) cc = custom1 pod[0-9]{3,6}.ecom.<realm>.<tenant>.*.*.ocapi.clients.*.<<metric>>";
try {
MetricAggregationRule.parseDefinition(aggregationRule2, 2, false);
fail("Should have thrown an exception");
} catch (RuntimeException e) {
assertEquals("Unsupported flag: [cc]", e.getMessage());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* 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.demandware.carbonj.service.accumulator;

import org.junit.jupiter.api.Test;

import java.util.ArrayList;

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

public class TestMetricAggregationRules {
@Test
public void test() {
MetricAggregationRules metricAggregationRules = new MetricAggregationRules(1, new ArrayList<>());
assertTrue(metricAggregationRules.isEmpty());
assertEquals("MetricAggregationRules{revision=1, rules=[]}", metricAggregationRules.toString());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* 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.demandware.carbonj.service.accumulator;

import com.codahale.metrics.Meter;
import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.Timer;
import com.demandware.carbonj.service.engine.DataPoint;
import org.junit.jupiter.api.Test;

import java.util.concurrent.atomic.AtomicInteger;

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

public class TestSlot {
@Test
public void testSlot() {
MetricRegistry metricRegistry = new MetricRegistry();
Timer aggregatorFlushTimer = metricRegistry.timer(MetricRegistry.name("aggregator", "slotFlushTimer"));
Meter flushedAggregates = metricRegistry.meter(MetricRegistry.name("aggregator", "aggregates"));
Meter createdSlots = metricRegistry.meter(MetricRegistry.name("aggregator", "slotCreated"));
AtomicInteger counter = new AtomicInteger();
Slot slot = new Slot(60, new CountingLatePointLogger(counter), 1, aggregatorFlushTimer, flushedAggregates, createdSlots);
MetricAggregate metricAggregate = new MetricAggregate("foo.bar.aggregated", MetricAggregationMethod.LATENCY, false);
slot.apply(metricAggregate, new DataPoint("foo.bar", 123, 90, false), 90);
assertEquals(1, slot.size());
assertEquals(60, slot.getTs());
slot.close(dataPoints -> {System.out.println(dataPoints.get(0));});
assertEquals(4, flushedAggregates.getCount());
assertEquals(0, counter.get());
slot.apply(metricAggregate, new DataPoint("foo.bar", 123, 90, false), 90);
assertEquals(1, counter.get());
}
}
Loading

0 comments on commit befdb02

Please sign in to comment.