Skip to content

Commit

Permalink
[#11997] Add test code
Browse files Browse the repository at this point in the history
  • Loading branch information
minwoo-jung committed Feb 2, 2025
1 parent 73e5947 commit 2dff5c3
Show file tree
Hide file tree
Showing 6 changed files with 309 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,13 @@ public class DataSourceDataCollector extends DataCollector implements DataSource

private final Logger logger = LogManager.getLogger(DataSourceDataCollector.class);
private static final String EMTPY_STRING = "";
private final static String METRIC_NAME = "dataSource";
private final static String FIELD_ACTIVE_CONNECTION = "activeConnectionSize";
private final static String FIELD_MAX_CONNECTION = "maxConnectionSize";
protected final static String METRIC_NAME = "dataSource";
protected final static String FIELD_ACTIVE_CONNECTION = "activeConnectionSize";
protected final static String FIELD_MAX_CONNECTION = "maxConnectionSize";
private final static String JDBC_URL = "jdbcUrl";
private final static String ID = "id";
private final static String DATABASE_NAME = "databaseName";

private final AlarmDao alarmDao;
private final Application application;
private final List<String> agentIds;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@
public class HeapDataCollector extends DataCollector implements HeapDataGetter {

private final static String METRIC_NAME = "jvmGc";
private final static String FIELD_HEAP_USED = "heapUsed";
private final static String FIELD_HEAP_MAX = "heapMax";
protected final static String FIELD_HEAP_USED = "heapUsed";
protected final static String FIELD_HEAP_MAX = "heapMax";
private final AlarmDao alarmDao;
private final Application application;
private final Map<String, Long> agentHeapUsageRate = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* Copyright 2025 NAVER Corp.
*
* Licensed 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 com.navercorp.pinpoint.batch.alarm.collector.pinot;

import com.navercorp.pinpoint.batch.alarm.DataCollectorFactory;
import com.navercorp.pinpoint.batch.alarm.dao.AlarmDao;
import com.navercorp.pinpoint.batch.alarm.vo.AgentFieldUsage;
import com.navercorp.pinpoint.batch.alarm.vo.DataSourceAlarmVO;
import com.navercorp.pinpoint.common.model.TagInformation;
import com.navercorp.pinpoint.common.server.util.time.Range;
import com.navercorp.pinpoint.common.trace.ServiceType;
import com.navercorp.pinpoint.metric.common.model.Tag;
import com.navercorp.pinpoint.web.alarm.DataCollectorCategory;
import com.navercorp.pinpoint.web.vo.Application;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.when;



/**
* @author minwoo-jung
*/
@ExtendWith({MockitoExtension.class})
class DataSourceDataCollectorTest {


@Mock
private AlarmDao alarmDao;

@Test
public void collect() {
final String applicationName = "testApplication";
Application application = new Application(applicationName, ServiceType.STAND_ALONE);
long now = System.currentTimeMillis();
Range range = Range.unchecked(now - DataCollectorFactory.SLOT_INTERVAL_FIVE_MIN, now);

final String testAgent1 = "testAgent1";
final String testAgent2 = "testAgent2";
List<String> agentIds = List.of(testAgent1, testAgent2);

when(alarmDao.selectTagInfo(any(), any(), any(), any(), any())).thenReturn(CompletableFuture.completedFuture(List.of(new Tag("jdbcUrl", "jdbc:mysql://localhost:3306/testdb"))));

when(alarmDao.getTagInfoContainedSpecificTag(any(), eq(testAgent1), any(), any(), any(), any())).
thenReturn(CompletableFuture.completedFuture(
List.of(
new TagInformation(applicationName, testAgent1, DataSourceDataCollector.METRIC_NAME, DataSourceDataCollector.FIELD_ACTIVE_CONNECTION, List.of(new Tag("jdbcUrl", "jdbc:mysql://localhost:3306/testdb")))
)));

when(alarmDao.getTagInfoContainedSpecificTag(any(), eq(testAgent2), any(), any(), any(), any())).
thenReturn(CompletableFuture.completedFuture(
List.of(
new TagInformation(applicationName, testAgent2, DataSourceDataCollector.METRIC_NAME, DataSourceDataCollector.FIELD_ACTIVE_CONNECTION, List.of(new Tag("jdbcUrl", "jdbc:mysql://localhost:3306/testdb")))
)));

when(alarmDao.selectAvgGroupByField(any(), eq(testAgent1), any(), any(), any(), any())).
thenReturn(CompletableFuture.completedFuture(List.of(new AgentFieldUsage(testAgent1, DataSourceDataCollector.FIELD_ACTIVE_CONNECTION, 20D),
new AgentFieldUsage(testAgent1, DataSourceDataCollector.FIELD_MAX_CONNECTION, 40D))));;

when(alarmDao.selectAvgGroupByField(any(), eq(testAgent2), any(), any(), any(), any())).
thenReturn(CompletableFuture.completedFuture(List.of(new AgentFieldUsage(testAgent2, DataSourceDataCollector.FIELD_ACTIVE_CONNECTION, 20D),
new AgentFieldUsage(testAgent2, DataSourceDataCollector.FIELD_MAX_CONNECTION, 40D))));;

DataSourceDataCollector dataSourceDataCollector = new DataSourceDataCollector(DataCollectorCategory.DATA_SOURCE_STAT, alarmDao, application, agentIds, now, DataCollectorFactory.SLOT_INTERVAL_FIVE_MIN);
dataSourceDataCollector.collect();

Map<String, List<DataSourceAlarmVO>> dataSourceConnectionUsageRate = dataSourceDataCollector.getDataSourceConnectionUsageRate();
assertEquals(dataSourceConnectionUsageRate.size(), 2);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Copyright 2025 NAVER Corp.
*
* Licensed 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 com.navercorp.pinpoint.batch.alarm.collector.pinot;

import com.navercorp.pinpoint.batch.alarm.DataCollectorFactory;
import com.navercorp.pinpoint.batch.alarm.dao.AlarmDao;
import com.navercorp.pinpoint.batch.alarm.vo.AgentFieldUsage;
import com.navercorp.pinpoint.common.trace.ServiceType;
import com.navercorp.pinpoint.web.alarm.DataCollectorCategory;
import com.navercorp.pinpoint.web.vo.Application;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import java.util.List;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;

/**
* @author minwoo-jung
*/
@ExtendWith({MockitoExtension.class})
class HeapDataCollectorTest {

@Mock
AlarmDao alarmDao;

@Test
public void collect() {
Application application = new Application("test", ServiceType.STAND_ALONE);
long now = System.currentTimeMillis();

List<AgentFieldUsage> agentFieldUsageList = List.of(
new AgentFieldUsage("testAgent1", HeapDataCollector.FIELD_HEAP_USED, 1000D),
new AgentFieldUsage("testAgent1", HeapDataCollector.FIELD_HEAP_MAX, 2000D),
new AgentFieldUsage("testAgent2", HeapDataCollector.FIELD_HEAP_USED, 1000D),
new AgentFieldUsage("testAgent2", HeapDataCollector.FIELD_HEAP_MAX, 2000D),
new AgentFieldUsage("testAgent3", HeapDataCollector.FIELD_HEAP_USED, 1000D),
new AgentFieldUsage("testAgent3", HeapDataCollector.FIELD_HEAP_MAX, 2000D),
new AgentFieldUsage("testAgent4", HeapDataCollector.FIELD_HEAP_USED, 1000D),
new AgentFieldUsage("testAgent4", HeapDataCollector.FIELD_HEAP_MAX, 2000D)
);

when(alarmDao.selectSumGroupByField(any(), any(), any(), any())).thenReturn(agentFieldUsageList);

HeapDataCollector heapDataCollector = new HeapDataCollector(DataCollectorCategory.HEAP_USAGE_RATE, alarmDao, application, now, DataCollectorFactory.SLOT_INTERVAL_FIVE_MIN);
heapDataCollector.collect();

Map<String, Long> heapUsageRate = heapDataCollector.getHeapUsageRate();
assertEquals(4, heapUsageRate.size());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright 2025 NAVER Corp.
*
* Licensed 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 com.navercorp.pinpoint.batch.alarm.collector.pinot;

import com.navercorp.pinpoint.batch.alarm.DataCollectorFactory;
import com.navercorp.pinpoint.batch.alarm.dao.AlarmDao;
import com.navercorp.pinpoint.batch.alarm.vo.AgentUsageCount;
import com.navercorp.pinpoint.common.trace.ServiceType;
import com.navercorp.pinpoint.web.alarm.DataCollectorCategory;
import com.navercorp.pinpoint.web.vo.Application;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import java.util.List;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;

/**
* @author minwoo-jung
*/

@ExtendWith({MockitoExtension.class})
class JvmCpuDataCollectorTest {

@Mock
AlarmDao alarmDao;

@Test
public void collect() {
Application application = new Application("test", ServiceType.STAND_ALONE);
long now = System.currentTimeMillis();

List<AgentUsageCount> agentUsageCountList = List.of(
new AgentUsageCount("testAgent1", 1000D, 20D),
new AgentUsageCount("testAgent2", 2000D, 20D),
new AgentUsageCount("testAgent3", 1500D,20D),
new AgentUsageCount("testAgent4", 2000D, 20D),
new AgentUsageCount("testAgent5", 1000D, 20D),
new AgentUsageCount("testAgent6", 2000D, 20D)
);

when(alarmDao.selectSumCount(any(), any(), any(), any())).thenReturn(agentUsageCountList);
JvmCpuDataCollector jvmCpuDataCollector = new JvmCpuDataCollector(DataCollectorCategory.JVM_CPU_USAGE_RATE, alarmDao, application, now, DataCollectorFactory.SLOT_INTERVAL_FIVE_MIN);
jvmCpuDataCollector.collect();

Map<String, Long> jvmCpuUsageRate = jvmCpuDataCollector.getJvmCpuUsageRate();
assertEquals(6, jvmCpuUsageRate.size());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Copyright 2025 NAVER Corp.
*
* Licensed 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 com.navercorp.pinpoint.batch.alarm.collector.pinot;

import com.navercorp.pinpoint.batch.alarm.DataCollectorFactory;
import com.navercorp.pinpoint.batch.alarm.dao.AlarmDao;
import com.navercorp.pinpoint.batch.alarm.vo.AgentUsageCount;
import com.navercorp.pinpoint.common.trace.ServiceType;
import com.navercorp.pinpoint.web.alarm.DataCollectorCategory;
import com.navercorp.pinpoint.web.vo.Application;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import java.util.List;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;

/**
* @author minwoo-jung
*/
@ExtendWith({MockitoExtension.class})
class SystemCpuDataCollectorTest {

@Mock
AlarmDao alarmDao;

@Test
public void collect() {
Application application = new Application("test", ServiceType.STAND_ALONE);
long now = System.currentTimeMillis();

List<AgentUsageCount> agentUsageCountList = List.of(
new AgentUsageCount("testAgent1", 1000D, 20D),
new AgentUsageCount("testAgent2", 2000D, 20D),
new AgentUsageCount("testAgent3", 1500D,20D),
new AgentUsageCount("testAgent4", 2000D, 20D),
new AgentUsageCount("testAgent5", 1000D, 20D),
new AgentUsageCount("testAgent6", 2000D, 20D)
);

when(alarmDao.selectSumCount(any(), any(), any(), any())).thenReturn(agentUsageCountList);

SystemCpuDataCollector systemCpuDataCollector = new SystemCpuDataCollector(DataCollectorCategory.SYSTEM_CPU_USAGE_RATE, alarmDao, application, now, DataCollectorFactory.SLOT_INTERVAL_FIVE_MIN);
systemCpuDataCollector.collect();

Map<String, Long> systemCpuUsageRate = systemCpuDataCollector.getSystemCpuUsageRate();
assertEquals(6, systemCpuUsageRate.size());
}
}

0 comments on commit 2dff5c3

Please sign in to comment.