Skip to content

Commit

Permalink
test: Improve the test case coverage of [metrics] module to 70% (#6752)
Browse files Browse the repository at this point in the history
  • Loading branch information
lightClouds917 authored Aug 31, 2024
1 parent 193876c commit f5a350d
Show file tree
Hide file tree
Showing 10 changed files with 699 additions and 0 deletions.
2 changes: 2 additions & 0 deletions changes/en-us/2.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ Add changes here for all PR submitted to the 2.x branch.
- [[#6608](https://github.com/apache/incubator-seata/pull/6608)] add unit test for sql-parser-core
- [[#6647](https://github.com/apache/incubator-seata/pull/6647)] improve the test case coverage of saga module to 70%
- [[#6695](https://github.com/apache/incubator-seata/pull/6695)] old version(< 0.7.1) client test case for multi-version protocol
- [[#6752](https://github.com/apache/incubator-seata/pull/6752)] Improve the test case coverage of metrics module
- [[#6764](https://github.com/apache/incubator-seata/pull/6764)] add Apollo mock test case
- [[#6750](https://github.com/apache/incubator-seata/pull/6750)] increase spring autoconfigure module unit test converage
- [[#6773](https://github.com/apache/incubator-seata/pull/6773)] fix the wrong code coverage from codecov icon in default branch
Expand Down Expand Up @@ -114,6 +115,7 @@ Thanks to these contributors for their code commits. Please report an unintended
- [caohdgege](https://github.com/caohdgege)
- [imashimaro](https://github.com/hmj776521114)
- [lyl2008dsg](https://github.com/lyl2008dsg)
- [lightClouds917](https://github.com/lightClouds917)
- [l81893521](https://github.com/l81893521)
- [laywin](https://github.com/laywin)

Expand Down
2 changes: 2 additions & 0 deletions changes/zh-cn/2.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
- [[#6608](https://github.com/apache/incubator-seata/pull/6608)] 添加sql-parser-core模块测试用例
- [[#6647](https://github.com/apache/incubator-seata/pull/6647)] 增加saga模块的测试用例覆盖率
- [[#6695](https://github.com/apache/incubator-seata/pull/6695)] 多版本协议的旧版本(< 0.7.1)客户端测试用例
- [[#6752](https://github.com/apache/incubator-seata/pull/6752)] 增加metrics模块测试用例覆盖率
- [[#6764](https://github.com/apache/incubator-seata/pull/6764)] 增加 Apollo Mock 测试用例
- [[#6750](https://github.com/apache/incubator-seata/pull/6750)] 提升spring autoconfigure模块单测覆盖率
- [[#6773](https://github.com/apache/incubator-seata/pull/6773)] 修复codecov图标显示错误的代码覆盖率
Expand Down Expand Up @@ -117,6 +118,7 @@
- [caohdgege](https://github.com/caohdgege)
- [imashimaro](https://github.com/hmj776521114)
- [lyl2008dsg](https://github.com/lyl2008dsg)
- [lightClouds917](https://github.com/lightClouds917)
- [l81893521](https://github.com/l81893521)
- [laywin](https://github.com/laywin)

Expand Down
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());
}
}
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());
}
}
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());
}
}
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();
}

}
Loading

0 comments on commit f5a350d

Please sign in to comment.