Skip to content
This repository has been archived by the owner on Aug 2, 2022. It is now read-only.

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bug Fix in Some Collector UTs
Browse files Browse the repository at this point in the history
meetshah777 committed Apr 3, 2021

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 3fbd7ba commit c70b19d
Showing 4 changed files with 117 additions and 474 deletions.
Original file line number Diff line number Diff line change
@@ -1,216 +1,61 @@
/*
* Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
* Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://www.apache.org/licenses/LICENSE-2.0
* http://www.apache.org/licenses/LICENSE-2.0
*
* or in the "license" file accompanying this file. This file 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.
* or in the "license" file accompanying this file. This file 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.amazon.opendistro.elasticsearch.performanceanalyzer.collectors;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import com.amazon.opendistro.elasticsearch.performanceanalyzer.CustomMetricsLocationTestBase;
import com.amazon.opendistro.elasticsearch.performanceanalyzer.ESResources;
import com.amazon.opendistro.elasticsearch.performanceanalyzer.config.PerformanceAnalyzerController;
import com.amazon.opendistro.elasticsearch.performanceanalyzer.config.overrides.ConfigOverridesWrapper;
import com.amazon.opendistro.elasticsearch.performanceanalyzer.metrics.AllMetrics;
import com.amazon.opendistro.elasticsearch.performanceanalyzer.metrics.MetricsConfiguration;
import com.amazon.opendistro.elasticsearch.performanceanalyzer.metrics.PerformanceAnalyzerMetrics;
import com.amazon.opendistro.elasticsearch.performanceanalyzer.reader_writer_shared.Event;
import org.junit.Test;
import org.mockito.Mockito;

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

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.elasticsearch.cluster.service.ClusterApplierService;
import org.elasticsearch.cluster.service.ClusterService;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.reflect.Whitebox;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

@RunWith(PowerMockRunner.class)
@PrepareForTest({ESResources.class})
public class ClusterApplierServiceStatsCollectorTests extends CustomMetricsLocationTestBase {
ObjectMapper mapper = new ObjectMapper();

public void cleanUp() throws Exception {
super.setUp();
// clean metricQueue before running every test
TestUtil.readEvents();
System.setProperty("performanceanalyzer.metrics.log.enabled", "False");
}

@Test
public void testClusterApplierServiceStats_saveMetricValues() throws Exception {
cleanUp();
public void testClusterApplierServiceStatsMetrics() {
MetricsConfiguration.CONFIG_MAP.put(ClusterApplierServiceStatsCollector.class, MetricsConfiguration.cdefault);
System.setProperty("performanceanalyzer.metrics.log.enabled", "False");

long startTimeInMills = 1153721339;
PerformanceAnalyzerController controller = Mockito.mock(
PerformanceAnalyzerController.class);
PerformanceAnalyzerController controller = Mockito.mock(PerformanceAnalyzerController.class);
ConfigOverridesWrapper configOverrides = Mockito.mock(ConfigOverridesWrapper.class);
ClusterApplierServiceStatsCollector clusterApplierServiceStatsCollector = new ClusterApplierServiceStatsCollector(
controller, configOverrides);
Mockito.when(controller.isCollectorEnabled(configOverrides, "ClusterApplierServiceStatsCollector"))
.thenReturn(true);
clusterApplierServiceStatsCollector.saveMetricValues("cluster_applier_service",
startTimeInMills);
ClusterApplierServiceStatsCollector clusterApplierServiceStatsCollector = new ClusterApplierServiceStatsCollector(
controller, configOverrides);
clusterApplierServiceStatsCollector.saveMetricValues("testMetric", startTimeInMills);

List<Event> metrics = new ArrayList<>();
PerformanceAnalyzerMetrics.metricQueue.drainTo(metrics);

assertEquals(1, metrics.size());
assertEquals("cluster_applier_service", metrics.get(0).value);
assertEquals("testMetric", metrics.get(0).value);

try {
clusterApplierServiceStatsCollector.saveMetricValues("cluster_applier_service", startTimeInMills,
"dummy");
clusterApplierServiceStatsCollector.saveMetricValues("Cluster_applier", startTimeInMills, "123");
assertTrue("Negative scenario test: Should have been a RuntimeException", true);
} catch (RuntimeException ex) {
//- expecting exception...1 values passed; 0 expected
}
}

@SuppressWarnings("unchecked")
@Ignore
@Test
public void testClusterApplierServiceStats_collectMetrics() throws Exception {
cleanUp();
MetricsConfiguration.CONFIG_MAP.put(ClusterApplierServiceStatsCollector.class, MetricsConfiguration.cdefault);
System.setProperty("performanceanalyzer.metrics.log.enabled", "False");
long startTimeInMills = 1153721339;
PerformanceAnalyzerController controller = Mockito.mock(PerformanceAnalyzerController.class);
ConfigOverridesWrapper configOverrides = Mockito.mock(ConfigOverridesWrapper.class);
ClusterApplierServiceStatsCollector clusterApplierServiceStatsCollector = new
ClusterApplierServiceStatsCollector(controller, configOverrides);
ClusterApplierServiceStatsCollector spyCollector = Mockito.spy(clusterApplierServiceStatsCollector);
Mockito.doReturn(new ClusterApplierServiceStatsCollector.
ClusterApplierServiceStats(23L, 15L, 2L, -1L))
.when(spyCollector).getClusterApplierServiceStats();
Mockito.when(controller.isCollectorEnabled(configOverrides,
ClusterApplierServiceStatsCollector.class.getSimpleName())).thenReturn(true);

ESResources esResources = Mockito.mock(ESResources.class);
ClusterService clusterService = Mockito.mock(ClusterService.class);
ClusterApplierService clusterApplierService = Mockito.mock(ClusterApplierService.class);
Whitebox.setInternalState(ESResources.class, "INSTANCE", esResources);
Mockito.when(esResources.getClusterService()).thenReturn(clusterService);
Mockito.when(clusterService.getClusterApplierService()).thenReturn(clusterApplierService);

spyCollector.collectMetrics(startTimeInMills);

List<Event> metrics = new ArrayList<>();
PerformanceAnalyzerMetrics.metricQueue.drainTo(metrics);

assertEquals(1, metrics.size());
String[] lines = metrics.get(0).value.split(System.lineSeparator());
Map<String, String> map = mapper.readValue(lines[1], Map.class);
assertEquals(0.6521739130434783, map.get(AllMetrics.ClusterApplierServiceStatsValue
.CLUSTER_APPLIER_SERVICE_LATENCY.toString()));
assertEquals(2.0, map.get(AllMetrics.ClusterApplierServiceStatsValue
.CLUSTER_APPLIER_SERVICE_FAILURE.toString()));
}

@SuppressWarnings("unchecked")
@Test
public void testClusterApplierServiceStats_collectMetricsWithPreviousClusterApplierMetrics() throws Exception {
cleanUp();
MetricsConfiguration.CONFIG_MAP.put(ClusterApplierServiceStatsCollector.class, MetricsConfiguration.cdefault);
System.setProperty("performanceanalyzer.metrics.log.enabled", "False");
long startTimeInMills = 1153721339;
PerformanceAnalyzerController controller = Mockito.mock(PerformanceAnalyzerController.class);
ConfigOverridesWrapper configOverrides = Mockito.mock(ConfigOverridesWrapper.class);
ClusterApplierServiceStatsCollector clusterApplierServiceStatsCollector = new
ClusterApplierServiceStatsCollector(controller, configOverrides);
ClusterApplierServiceStatsCollector spyCollector = Mockito.spy(clusterApplierServiceStatsCollector);
Mockito.doReturn(new ClusterApplierServiceStatsCollector.
ClusterApplierServiceStats(23L, 46L, 2L, -1L))
.when(spyCollector).getClusterApplierServiceStats();
Mockito.when(controller.isCollectorEnabled(configOverrides,
ClusterApplierServiceStatsCollector.class.getSimpleName())).thenReturn(true);

ESResources esResources = Mockito.mock(ESResources.class);
ClusterService clusterService = Mockito.mock(ClusterService.class);
ClusterApplierService clusterApplierService = Mockito.mock(ClusterApplierService.class);
Whitebox.setInternalState(ESResources.class, "INSTANCE", esResources);
Mockito.when(esResources.getClusterService()).thenReturn(clusterService);
Mockito.when(clusterService.getClusterApplierService()).thenReturn(clusterApplierService);

spyCollector.resetPrevClusterApplierServiceStats();
spyCollector.collectMetrics(startTimeInMills);

List<Event> metrics = new ArrayList<>();
PerformanceAnalyzerMetrics.metricQueue.drainTo(metrics);

assertEquals(1, metrics.size());
String[] lines = metrics.get(0).value.split(System.lineSeparator());
Map<String, String> map = mapper.readValue(lines[1], Map.class);
assertEquals(2.0, map.get(AllMetrics.ClusterApplierServiceStatsValue
.CLUSTER_APPLIER_SERVICE_LATENCY.toString()));
assertEquals(2.0, map.get(AllMetrics.ClusterApplierServiceStatsValue
.CLUSTER_APPLIER_SERVICE_FAILURE.toString()));

Mockito.doReturn(new ClusterApplierServiceStatsCollector.
ClusterApplierServiceStats(25L, 54L, 2L, -1L))
.when(spyCollector).getClusterApplierServiceStats();

spyCollector.collectMetrics(startTimeInMills);

metrics.clear();
PerformanceAnalyzerMetrics.metricQueue.drainTo(metrics);

assertEquals(1, metrics.size());
String[] lines2 = metrics.get(0).value.split(System.lineSeparator());
map = mapper.readValue(lines2[1], Map.class);
assertEquals(4.0, map.get(AllMetrics.ClusterApplierServiceStatsValue
.CLUSTER_APPLIER_SERVICE_LATENCY.toString()));
assertEquals(0.0, map.get(AllMetrics.ClusterApplierServiceStatsValue
.CLUSTER_APPLIER_SERVICE_FAILURE.toString()));


}

@SuppressWarnings("unchecked")
@Test
public void testClusterApplierServiceStats_collectMetrics_ClassNotFoundException() throws Exception {
cleanUp();
MetricsConfiguration.CONFIG_MAP.put(ClusterApplierServiceStatsCollector.class, MetricsConfiguration.cdefault);
System.setProperty("performanceanalyzer.metrics.log.enabled", "False");
long startTimeInMills = 1153721339;
PerformanceAnalyzerController controller = Mockito.mock(PerformanceAnalyzerController.class);
ConfigOverridesWrapper configOverrides = Mockito.mock(ConfigOverridesWrapper.class);
ClusterApplierServiceStatsCollector clusterApplierServiceStatsCollector = new
ClusterApplierServiceStatsCollector(controller, configOverrides);
ClusterApplierServiceStatsCollector spyCollector = Mockito.spy(clusterApplierServiceStatsCollector);
Mockito.when(controller.isCollectorEnabled(configOverrides,
ClusterApplierServiceStatsCollector.class.getSimpleName())).thenReturn(true);

ESResources esResources = Mockito.mock(ESResources.class);
ClusterService clusterService = Mockito.mock(ClusterService.class);
ClusterApplierService clusterApplierService = Mockito.mock(ClusterApplierService.class);
Whitebox.setInternalState(ESResources.class, "INSTANCE", esResources);
Mockito.when(esResources.getClusterService()).thenReturn(clusterService);
Mockito.when(clusterService.getClusterApplierService()).thenReturn(clusterApplierService);

spyCollector.collectMetrics(startTimeInMills);

List<Event> metrics = new ArrayList<>();
PerformanceAnalyzerMetrics.metricQueue.drainTo(metrics);
// No method found to get cluster state applier thread stats. Skipping ClusterApplierServiceStatsCollector.
assertEquals(0, metrics.size());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Copyright <2021> Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* or in the "license" file accompanying this file. This file 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.amazon.opendistro.elasticsearch.performanceanalyzer.collectors;

import org.junit.Ignore;
import org.junit.Test;

import com.amazon.opendistro.elasticsearch.performanceanalyzer.CustomMetricsLocationTestBase;
import com.amazon.opendistro.elasticsearch.performanceanalyzer.config.PluginSettings;
import com.amazon.opendistro.elasticsearch.performanceanalyzer.metrics.MetricsConfiguration;
import com.amazon.opendistro.elasticsearch.performanceanalyzer.metrics.PerformanceAnalyzerMetrics;
import com.amazon.opendistro.elasticsearch.performanceanalyzer.metrics.MetricsConfiguration;
import com.amazon.opendistro.elasticsearch.performanceanalyzer.config.PerformanceAnalyzerController;
import com.amazon.opendistro.elasticsearch.performanceanalyzer.config.overrides.ConfigOverridesWrapper;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import org.mockito.Mockito;
import com.amazon.opendistro.elasticsearch.performanceanalyzer.collectors.ElectionTermCollector;

@Ignore
public class ElectionTermCollectorTests extends CustomMetricsLocationTestBase {

@Test
public void testElectionTermMetrics() {
MetricsConfiguration.CONFIG_MAP.put(ElectionTermCollector.class, new MetricsConfiguration.MetricConfig(1000, 0, 0));
System.setProperty("performanceanalyzer.metrics.log.enabled", "False");
long startTimeInMills = 1353723339;

PerformanceAnalyzerController controller = Mockito.mock(
PerformanceAnalyzerController.class);
ConfigOverridesWrapper configOverrides = Mockito.mock(ConfigOverridesWrapper.class);

ElectionTermCollector electionTermCollector = new ElectionTermCollector(controller,configOverrides);
electionTermCollector.saveMetricValues("Election_Term", startTimeInMills);


String fetchedValue = PerformanceAnalyzerMetrics.getMetric(PluginSettings.instance().getMetricsLocation() +
PerformanceAnalyzerMetrics.getTimeInterval(startTimeInMills)+"/election_term/");
PerformanceAnalyzerMetrics.removeMetrics(PluginSettings.instance().getMetricsLocation()
+ PerformanceAnalyzerMetrics.getTimeInterval(startTimeInMills));
assertEquals("Election_Term", fetchedValue);

try {
electionTermCollector.saveMetricValues("Election_term", startTimeInMills, "current");
assertTrue("Negative scenario test: Should have been a RuntimeException", true);
} catch (RuntimeException ex) {
//- expecting exception... 1 values passed; 0 expected
}

try {
electionTermCollector.saveMetricValues("Election_termm", startTimeInMills,"current","start");
assertTrue("Negative scenario test: Should have been a RuntimeException", true);
} catch (RuntimeException ex) {
//- expecting exception...only 2 values passed; 0 expected
}

}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,208 +1,61 @@
/*
* Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
* Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://www.apache.org/licenses/LICENSE-2.0
* http://www.apache.org/licenses/LICENSE-2.0
*
* or in the "license" file accompanying this file. This file 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.
* or in the "license" file accompanying this file. This file 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.amazon.opendistro.elasticsearch.performanceanalyzer.collectors;

import com.amazon.opendistro.elasticsearch.performanceanalyzer.ESResources;
import com.amazon.opendistro.elasticsearch.performanceanalyzer.CustomMetricsLocationTestBase;
import com.amazon.opendistro.elasticsearch.performanceanalyzer.config.PerformanceAnalyzerController;
import com.amazon.opendistro.elasticsearch.performanceanalyzer.config.overrides.ConfigOverridesWrapper;
import com.amazon.opendistro.elasticsearch.performanceanalyzer.metrics.AllMetrics;
import com.amazon.opendistro.elasticsearch.performanceanalyzer.metrics.MetricsConfiguration;
import com.amazon.opendistro.elasticsearch.performanceanalyzer.metrics.PerformanceAnalyzerMetrics;
import com.amazon.opendistro.elasticsearch.performanceanalyzer.reader_writer_shared.Event;
import com.amazon.opendistro.elasticsearch.performanceanalyzer.util.TestUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import junit.framework.TestCase;
import org.elasticsearch.cluster.service.ClusterService;
import org.elasticsearch.cluster.service.MasterService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.reflect.Whitebox;

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

@RunWith(PowerMockRunner.class)
@PrepareForTest({ESResources.class})
public class MasterClusterStateUpdateStatsCollectorTests extends TestCase {
ObjectMapper mapper = new ObjectMapper();
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

public void cleanUp() throws Exception {
super.setUp();
// clean metricQueue before running every test
TestUtil.readEvents();
System.setProperty("performanceanalyzer.metrics.log.enabled", "False");
}
public class MasterClusterStateUpdateStatsCollectorTests extends CustomMetricsLocationTestBase {

@Test
public void testMasterClusterStateUpdateStats_saveMetricValues() throws Exception {
cleanUp();
public void testMasterClusterStateUpdateStatsMetrics() {
MetricsConfiguration.CONFIG_MAP.put(MasterClusterStateUpdateStatsCollector.class, MetricsConfiguration.cdefault);
System.setProperty("performanceanalyzer.metrics.log.enabled", "False");

long startTimeInMills = 1153721339;
PerformanceAnalyzerController controller = Mockito.mock(
PerformanceAnalyzerController.class);
PerformanceAnalyzerController controller = Mockito.mock(PerformanceAnalyzerController.class);
ConfigOverridesWrapper configOverrides = Mockito.mock(ConfigOverridesWrapper.class);
MasterClusterStateUpdateStatsCollector masterClusterStateUpdateStatsCollector = new MasterClusterStateUpdateStatsCollector(
controller, configOverrides);
Mockito.when(controller.isCollectorEnabled(configOverrides, "MasterClusterStateUpdateStatsCollector"))
.thenReturn(true);
masterClusterStateUpdateStatsCollector.saveMetricValues("master_cluster_update",
startTimeInMills);
MasterClusterStateUpdateStatsCollector masterClusterStateUpdateStatsCollector = new MasterClusterStateUpdateStatsCollector(
controller, configOverrides);
masterClusterStateUpdateStatsCollector.saveMetricValues("testMetric", startTimeInMills);

List<Event> metrics = new ArrayList<>();
PerformanceAnalyzerMetrics.metricQueue.drainTo(metrics);

assertEquals(1, metrics.size());
assertEquals("master_cluster_update", metrics.get(0).value);
assertEquals("testMetric", metrics.get(0).value);

try {
masterClusterStateUpdateStatsCollector.saveMetricValues("master_cluster_update", startTimeInMills,
"dummy");
masterClusterStateUpdateStatsCollector.saveMetricValues("master_cluster_stat_update", startTimeInMills, "123");
assertTrue("Negative scenario test: Should have been a RuntimeException", true);
} catch (RuntimeException ex) {
//- expecting exception...1 values passed; 0 expected
}
}

@SuppressWarnings("unchecked")
@Test
public void testMasterClusterStateUpdateStats_collectMetrics() throws Exception {
cleanUp();
MetricsConfiguration.CONFIG_MAP.put(MasterClusterStateUpdateStatsCollector.class, MetricsConfiguration.cdefault);
System.setProperty("performanceanalyzer.metrics.log.enabled", "False");
long startTimeInMills = 1153721339;
PerformanceAnalyzerController controller = Mockito.mock(PerformanceAnalyzerController.class);
ConfigOverridesWrapper configOverrides = Mockito.mock(ConfigOverridesWrapper.class);
MasterClusterStateUpdateStatsCollector masterClusterStateUpdateStatsCollector = new
MasterClusterStateUpdateStatsCollector(controller, configOverrides);
MasterClusterStateUpdateStatsCollector spyCollector = Mockito.spy(masterClusterStateUpdateStatsCollector);
Mockito.doReturn(new MasterClusterStateUpdateStatsCollector.MasterClusterStateUpdateStats(23L, 15L, 2L))
.when(spyCollector).getMasterClusterStateUpdateStats();
Mockito.when(controller.isCollectorEnabled(configOverrides,
MasterClusterStateUpdateStatsCollector.class.getSimpleName())).thenReturn(true);

ESResources esResources = Mockito.mock(ESResources.class);
ClusterService clusterService = Mockito.mock(ClusterService.class);
MasterService masterService = Mockito.mock(MasterService.class);
Whitebox.setInternalState(ESResources.class, "INSTANCE", esResources);
Mockito.when(esResources.getClusterService()).thenReturn(clusterService);
Mockito.when(clusterService.getMasterService()).thenReturn(masterService);

spyCollector.collectMetrics(startTimeInMills);

List<Event> metrics = new ArrayList<>();
PerformanceAnalyzerMetrics.metricQueue.drainTo(metrics);

assertEquals(1, metrics.size());
String[] lines = metrics.get(0).value.split(System.lineSeparator());
Map<String, String> map = mapper.readValue(lines[1], Map.class);
assertEquals(0.6521739130434783, map.get(AllMetrics.MasterClusterUpdateStatsValue
.PUBLISH_CLUSTER_STATE_LATENCY.toString()));
assertEquals(2.0, map.get(AllMetrics.MasterClusterUpdateStatsValue
.PUBLISH_CLUSTER_STATE_FAILURE.toString()));
}

@SuppressWarnings("unchecked")
@Test
public void testMasterClusterStateUpdateStats_collectMetricsWithPreviousMasterClusterUpdateStats() throws Exception {
cleanUp();
MetricsConfiguration.CONFIG_MAP.put(MasterClusterStateUpdateStatsCollector.class, MetricsConfiguration.cdefault);
System.setProperty("performanceanalyzer.metrics.log.enabled", "False");
long startTimeInMills = 1153721339;
PerformanceAnalyzerController controller = Mockito.mock(PerformanceAnalyzerController.class);
ConfigOverridesWrapper configOverrides = Mockito.mock(ConfigOverridesWrapper.class);
MasterClusterStateUpdateStatsCollector masterClusterStateUpdateStatsCollector = new
MasterClusterStateUpdateStatsCollector(controller, configOverrides);
MasterClusterStateUpdateStatsCollector spyCollector = Mockito.spy(masterClusterStateUpdateStatsCollector);
Mockito.doReturn(new MasterClusterStateUpdateStatsCollector.
MasterClusterStateUpdateStats(23L, 46L, 2L))
.when(spyCollector).getMasterClusterStateUpdateStats();
Mockito.when(controller.isCollectorEnabled(configOverrides,
MasterClusterStateUpdateStatsCollector.class.getSimpleName())).thenReturn(true);

ESResources esResources = Mockito.mock(ESResources.class);
ClusterService clusterService = Mockito.mock(ClusterService.class);
MasterService masterService = Mockito.mock(MasterService.class);
Whitebox.setInternalState(ESResources.class, "INSTANCE", esResources);
Mockito.when(esResources.getClusterService()).thenReturn(clusterService);
Mockito.when(clusterService.getMasterService()).thenReturn(masterService);

spyCollector.collectMetrics(startTimeInMills);

List<Event> metrics = new ArrayList<>();
PerformanceAnalyzerMetrics.metricQueue.drainTo(metrics);

assertEquals(1, metrics.size());
String[] lines = metrics.get(0).value.split(System.lineSeparator());
Map<String, String> map = mapper.readValue(lines[1], Map.class);
assertEquals(2.0, map.get(AllMetrics.MasterClusterUpdateStatsValue
.PUBLISH_CLUSTER_STATE_LATENCY.toString()));
assertEquals(2.0, map.get(AllMetrics.MasterClusterUpdateStatsValue
.PUBLISH_CLUSTER_STATE_FAILURE.toString()));

Mockito.doReturn(new MasterClusterStateUpdateStatsCollector.
MasterClusterStateUpdateStats(25L, 54L, 2L))
.when(spyCollector).getMasterClusterStateUpdateStats();

spyCollector.collectMetrics(startTimeInMills);

metrics.clear();
PerformanceAnalyzerMetrics.metricQueue.drainTo(metrics);

assertEquals(1, metrics.size());
String[] lines2 = metrics.get(0).value.split(System.lineSeparator());
map = mapper.readValue(lines2[1], Map.class);
assertEquals(4.0, map.get(AllMetrics.MasterClusterUpdateStatsValue
.PUBLISH_CLUSTER_STATE_LATENCY.toString()));
assertEquals(0.0, map.get(AllMetrics.MasterClusterUpdateStatsValue
.PUBLISH_CLUSTER_STATE_FAILURE.toString()));


}

@SuppressWarnings("unchecked")
@Test
public void testMasterClusterStateUpdateStats_collectMetrics_ClassNotFoundException() throws Exception {
cleanUp();
MetricsConfiguration.CONFIG_MAP.put(MasterClusterStateUpdateStatsCollector.class, MetricsConfiguration.cdefault);
System.setProperty("performanceanalyzer.metrics.log.enabled", "False");
long startTimeInMills = 1153721339;
PerformanceAnalyzerController controller = Mockito.mock(PerformanceAnalyzerController.class);
ConfigOverridesWrapper configOverrides = Mockito.mock(ConfigOverridesWrapper.class);
MasterClusterStateUpdateStatsCollector masterClusterStateUpdateStatsCollector = new
MasterClusterStateUpdateStatsCollector(controller, configOverrides);
MasterClusterStateUpdateStatsCollector spyCollector = Mockito.spy(masterClusterStateUpdateStatsCollector);
Mockito.when(controller.isCollectorEnabled(configOverrides,
MasterClusterStateUpdateStatsCollector.class.getSimpleName())).thenReturn(true);

ESResources esResources = Mockito.mock(ESResources.class);
ClusterService clusterService = Mockito.mock(ClusterService.class);
MasterService masterService = Mockito.mock(MasterService.class);
Whitebox.setInternalState(ESResources.class, "INSTANCE", esResources);
Mockito.when(esResources.getClusterService()).thenReturn(clusterService);
Mockito.when(clusterService.getMasterService()).thenReturn(masterService);

spyCollector.collectMetrics(startTimeInMills);

List<Event> metrics = new ArrayList<>();
PerformanceAnalyzerMetrics.metricQueue.drainTo(metrics);
// No method found to get master cluster state update stats. Skipping MasterClusterStateUpdateStatsCollector.
assertEquals(0, metrics.size());
}
}
}

0 comments on commit c70b19d

Please sign in to comment.