-
Notifications
You must be signed in to change notification settings - Fork 8.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: Improve the test case coverage of [metrics] module to 70% (#6752)
- Loading branch information
1 parent
193876c
commit f5a350d
Showing
10 changed files
with
699 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
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
70 changes: 70 additions & 0 deletions
70
metrics/seata-metrics-api/src/test/java/org/apache/seata/metrics/IdTest.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,70 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.seata.metrics; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
public class IdTest { | ||
private Id id; | ||
|
||
@BeforeEach | ||
public void setUp() { | ||
id = new Id("test"); | ||
} | ||
|
||
@Test | ||
public void testGetId() { | ||
assertNotNull(id.getId()); | ||
} | ||
|
||
@Test | ||
public void testGetName() { | ||
assertEquals("test", id.getName()); | ||
} | ||
|
||
@Test | ||
public void testGetTags() { | ||
assertFalse(( id.getTags()).iterator().hasNext()); | ||
} | ||
|
||
@Test | ||
public void testGetTagCount() { | ||
assertEquals(0, id.getTagCount()); | ||
} | ||
|
||
@Test | ||
public void testWithTag() { | ||
id.withTag("key", "value"); | ||
assertEquals(1, id.getTagCount()); | ||
} | ||
|
||
@Test | ||
public void testGetMeterKey() { | ||
id.withTag("key", "value"); | ||
assertEquals("test(value)", id.getMeterKey()); | ||
} | ||
|
||
@Test | ||
public void testToString() { | ||
id.withTag("key", "value"); | ||
assertEquals("test(key=value)", id.toString()); | ||
} | ||
} |
83 changes: 83 additions & 0 deletions
83
...ompact/src/test/java/org/apache/seata/metrics/exporter/prometheus/CompactCounterTest.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,83 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.seata.metrics.exporter.prometheus; | ||
|
||
import org.apache.seata.metrics.Clock; | ||
import org.apache.seata.metrics.Id; | ||
import org.apache.seata.metrics.Measurement; | ||
import org.apache.seata.metrics.registry.compact.CompactCounter; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.Mockito; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
public class CompactCounterTest { | ||
private CompactCounter compactCounter; | ||
private Id id; | ||
private Clock clock; | ||
|
||
@BeforeEach | ||
public void setup() { | ||
id = Mockito.mock(Id.class); | ||
clock = Mockito.mock(Clock.class); | ||
compactCounter = new CompactCounter(id, clock); | ||
} | ||
|
||
@Test | ||
public void testIncrease() { | ||
long value = 5L; | ||
long result = compactCounter.increase(value); | ||
assertEquals(value, result); | ||
assertEquals(value, compactCounter.get()); | ||
} | ||
|
||
@Test | ||
public void testDecrease() { | ||
long value = 5L; | ||
compactCounter.increase(10L); | ||
long result = compactCounter.decrease(value); | ||
assertEquals(10L - value, result); | ||
assertEquals(10L - value, compactCounter.get()); | ||
} | ||
|
||
@Test | ||
public void testGet() { | ||
long value = 10L; | ||
compactCounter.increase(value); | ||
assertEquals(value, compactCounter.get()); | ||
} | ||
|
||
@Test | ||
public void testMeasure() { | ||
long value = 10L; | ||
compactCounter.increase(value); | ||
Mockito.when(clock.getCurrentMilliseconds()).thenReturn((double) 1000L); | ||
Measurement expectedMeasurement = new Measurement(id, 1000L, value); | ||
Iterable<Measurement> actualMeasurement = compactCounter.measure(); | ||
Measurement next = actualMeasurement.iterator().next(); | ||
|
||
assertEquals(expectedMeasurement.getId(), next.getId()); | ||
assertEquals(expectedMeasurement.getTimestamp(), next.getTimestamp()); | ||
assertEquals(expectedMeasurement.getValue(), next.getValue()); | ||
} | ||
|
||
@Test | ||
public void testGetId() { | ||
assertEquals(id, compactCounter.getId()); | ||
} | ||
} |
78 changes: 78 additions & 0 deletions
78
...-compact/src/test/java/org/apache/seata/metrics/exporter/prometheus/CompactGaugeTest.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,78 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.seata.metrics.exporter.prometheus; | ||
|
||
import org.apache.seata.metrics.Clock; | ||
import org.apache.seata.metrics.Id; | ||
import org.apache.seata.metrics.Measurement; | ||
import org.apache.seata.metrics.registry.compact.CompactGauge; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.Mock; | ||
import org.mockito.MockitoAnnotations; | ||
|
||
import java.util.function.Supplier; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.mockito.Mockito.*; | ||
|
||
public class CompactGaugeTest { | ||
@Mock | ||
private Id id; | ||
@Mock | ||
private Supplier<Number> supplier; | ||
@Mock | ||
private Clock clock; | ||
|
||
private CompactGauge<Number> compactGauge; | ||
|
||
@BeforeEach | ||
public void setUp() { | ||
MockitoAnnotations.openMocks(this); | ||
compactGauge = new CompactGauge<>(id, supplier, clock); | ||
} | ||
|
||
@Test | ||
public void testGet() { | ||
Number expected = 10; | ||
when(supplier.get()).thenReturn(expected); | ||
|
||
Number actual = compactGauge.get(); | ||
|
||
assertEquals(expected, actual); | ||
verify(supplier, times(1)).get(); | ||
} | ||
|
||
@Test | ||
public void testGetId() { | ||
assertEquals(id, compactGauge.getId()); | ||
} | ||
|
||
@Test | ||
public void testMeasure() { | ||
Number expectedValue = 10; | ||
when(supplier.get()).thenReturn(expectedValue); | ||
double expectedTimestamp = 1000.0; | ||
when(clock.getCurrentMilliseconds()).thenReturn(expectedTimestamp); | ||
|
||
Measurement actualMeasurement = compactGauge.measure().iterator().next(); | ||
|
||
assertEquals(id, actualMeasurement.getId()); | ||
assertEquals(expectedTimestamp, actualMeasurement.getTimestamp()); | ||
assertEquals(expectedValue.doubleValue(), actualMeasurement.getValue()); | ||
} | ||
} |
111 changes: 111 additions & 0 deletions
111
...mpact/src/test/java/org/apache/seata/metrics/exporter/prometheus/CompactRegistryTest.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,111 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.seata.metrics.exporter.prometheus; | ||
|
||
import org.apache.seata.metrics.*; | ||
import org.apache.seata.metrics.registry.compact.CompactRegistry; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.Mock; | ||
import org.mockito.MockitoAnnotations; | ||
|
||
import java.util.SortedMap; | ||
import java.util.TreeMap; | ||
import java.util.function.Supplier; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.mockito.Mockito.when; | ||
|
||
public class CompactRegistryTest { | ||
@Mock | ||
private Id id; | ||
@Mock | ||
private Supplier<Number> supplier; | ||
|
||
private CompactRegistry compactRegistry; | ||
|
||
@BeforeEach | ||
public void setUp() { | ||
MockitoAnnotations.openMocks(this); | ||
compactRegistry = new CompactRegistry(); | ||
} | ||
|
||
@Test | ||
public void testGetGauge() { | ||
when(id.getName()).thenReturn("test"); | ||
SortedMap<String, String> sortedMap = new TreeMap<>(); | ||
sortedMap.put("testTag", "testValue"); | ||
when(id.getTags()).thenReturn(sortedMap.entrySet()); | ||
when(id.getMeterKey()).thenReturn("testKey"); | ||
when(supplier.get()).thenReturn(1); | ||
when(id.getMeterKey()).thenReturn("testKey"); | ||
when(supplier.get()).thenReturn(1); | ||
|
||
Gauge<Number> gauge = compactRegistry.getGauge(id, supplier); | ||
|
||
assertEquals(id.getName(), gauge.getId().getName()); | ||
assertEquals(id.getTags(), gauge.getId().getTags()); | ||
assertEquals(1, gauge.get().intValue()); | ||
compactRegistry.clearUp(); | ||
} | ||
|
||
@Test | ||
public void testGetCounter() { | ||
when(id.getName()).thenReturn("test"); | ||
SortedMap<String, String> sortedMap = new TreeMap<>(); | ||
sortedMap.put("testTag", "testValue"); | ||
when(id.getTags()).thenReturn(sortedMap.entrySet()); | ||
when(id.getMeterKey()).thenReturn("testKey"); | ||
Counter counter = (Counter)compactRegistry.getCounter(id); | ||
|
||
Id id2 = new Id(id.getName()).withTag(id.getTags()); | ||
assertEquals(id2.getName(), counter.getId().getName()); | ||
assertEquals(id2.getTags(), counter.getId().getTags()); | ||
compactRegistry.clearUp(); | ||
} | ||
|
||
@Test | ||
public void testGetSummary() { | ||
when(id.getName()).thenReturn("test"); | ||
SortedMap<String, String> sortedMap = new TreeMap<>(); | ||
sortedMap.put("testTag", "testValue"); | ||
when(id.getTags()).thenReturn(sortedMap.entrySet()); | ||
when(id.getMeterKey()).thenReturn("testKey"); | ||
Summary summary = compactRegistry.getSummary(id); | ||
|
||
Id id2 = new Id(id.getName()).withTag(id.getTags()); | ||
assertEquals(id2.getName(), summary.getId().getName()); | ||
assertEquals(id2.getTags(), summary.getId().getTags()); | ||
compactRegistry.clearUp(); | ||
} | ||
|
||
@Test | ||
public void testGetTimer() { | ||
when(id.getName()).thenReturn("test"); | ||
SortedMap<String, String> sortedMap = new TreeMap<>(); | ||
sortedMap.put("testTag", "testValue"); | ||
when(id.getTags()).thenReturn(sortedMap.entrySet()); | ||
when(id.getMeterKey()).thenReturn("testKey"); | ||
Timer timer = compactRegistry.getTimer(id); | ||
|
||
Id id2 = new Id(id.getName()).withTag(id.getTags()); | ||
assertEquals(id2.getName(), timer.getId().getName()); | ||
assertEquals(id2.getTags(), timer.getId().getTags()); | ||
compactRegistry.clearUp(); | ||
} | ||
|
||
} |
Oops, something went wrong.