Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code coverage additions for Processing Directory #17622

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
/*
* 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.druid.java.util.metrics;

import com.google.common.collect.ImmutableMap;
import org.apache.druid.java.util.emitter.core.ConcurrentTimeCounter;
import org.apache.druid.java.util.emitter.core.HttpPostEmitter;
import org.apache.druid.java.util.emitter.service.ServiceEmitter;
import org.apache.druid.java.util.emitter.service.ServiceMetricEvent;
import org.junit.Assert;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.atLeastOnce;
import static org.mockito.Mockito.atMost;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

public class HttpPostEmitterMonitorTest
{
private HttpPostEmitter mockHttpPostEmitter;
private ServiceEmitter mockServiceEmitter;
private ImmutableMap<String, String> mockExtraDimensions;
private HttpPostEmitterMonitor monitor;

@BeforeEach
public void setUp()
{
mockHttpPostEmitter = mock(HttpPostEmitter.class);
mockServiceEmitter = mock(ServiceEmitter.class);
mockExtraDimensions = ImmutableMap.of("dimensionKey", "dimensionValue");

monitor = new HttpPostEmitterMonitor("testFeed", mockHttpPostEmitter, mockExtraDimensions);
}

@Test
public void testDoMonitor()
{
when(mockHttpPostEmitter.getTotalEmittedEvents()).thenReturn(100L);
when(mockHttpPostEmitter.getTotalDroppedBuffers()).thenReturn(10);
when(mockHttpPostEmitter.getTotalAllocatedBuffers()).thenReturn(20);
when(mockHttpPostEmitter.getTotalFailedBuffers()).thenReturn(5);
when(mockHttpPostEmitter.getBatchFillingTimeCounter()).thenReturn(mock(ConcurrentTimeCounter.class));
when(mockHttpPostEmitter.getSuccessfulSendingTimeCounter()).thenReturn(mock(ConcurrentTimeCounter.class));
when(mockHttpPostEmitter.getFailedSendingTimeCounter()).thenReturn(mock(ConcurrentTimeCounter.class));
when(mockHttpPostEmitter.getEventsToEmit()).thenReturn(200L);
when(mockHttpPostEmitter.getLargeEventsToEmit()).thenReturn(50L);
when(mockHttpPostEmitter.getBuffersToEmit()).thenReturn(30);
when(mockHttpPostEmitter.getBuffersToReuse()).thenReturn(15);

assertTrue(monitor.doMonitor(mockServiceEmitter));

ArgumentCaptor<ServiceMetricEvent.Builder> captor = ArgumentCaptor.forClass(ServiceMetricEvent.Builder.class);
verify(mockServiceEmitter, atLeastOnce()).emit(captor.capture());
}

@Test
public void testEmitEmittedEvents()
{
when(mockHttpPostEmitter.getTotalEmittedEvents()).thenReturn(100L);
when(mockHttpPostEmitter.getTotalDroppedBuffers()).thenReturn(10);
when(mockHttpPostEmitter.getTotalAllocatedBuffers()).thenReturn(20);
when(mockHttpPostEmitter.getTotalFailedBuffers()).thenReturn(5);
when(mockHttpPostEmitter.getBatchFillingTimeCounter()).thenReturn(mock(ConcurrentTimeCounter.class));
when(mockHttpPostEmitter.getSuccessfulSendingTimeCounter()).thenReturn(mock(ConcurrentTimeCounter.class));
when(mockHttpPostEmitter.getFailedSendingTimeCounter()).thenReturn(mock(ConcurrentTimeCounter.class));
when(mockHttpPostEmitter.getEventsToEmit()).thenReturn(200L);
when(mockHttpPostEmitter.getLargeEventsToEmit()).thenReturn(50L);
when(mockHttpPostEmitter.getBuffersToEmit()).thenReturn(30);
when(mockHttpPostEmitter.getBuffersToReuse()).thenReturn(15);

monitor.doMonitor(mockServiceEmitter);

ArgumentCaptor<ServiceMetricEvent.Builder> captor = ArgumentCaptor.forClass(ServiceMetricEvent.Builder.class);
verify(mockServiceEmitter, atLeastOnce()).emit(captor.capture());
}


@Test
public void testEmitFailedBuffers() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException
{
when(mockHttpPostEmitter.getTotalFailedBuffers()).thenReturn(5);

// Use reflection to call the private emitFailedBuffers method
Method emitFailedBuffersMethod = HttpPostEmitterMonitor.class.getDeclaredMethod("emitFailedBuffers", ServiceEmitter.class);
emitFailedBuffersMethod.setAccessible(true);
emitFailedBuffersMethod.invoke(monitor, mockServiceEmitter);

// Capture the emitted metrics
ArgumentCaptor<ServiceMetricEvent.Builder> captor = ArgumentCaptor.forClass(ServiceMetricEvent.Builder.class);
verify(mockServiceEmitter, atLeastOnce()).emit(captor.capture());

// Verify the failed buffers metric
Assert.assertTrue(captor.getAllValues().stream().anyMatch(builder -> builder.build(ImmutableMap.of()).getMetric().equals("emitter/buffers/failed/delta") && builder.build(ImmutableMap.of()).getValue().equals(5)));
}

@Test
public void testEmitTimeCounterMetrics() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException
{
ConcurrentTimeCounter mockTimeCounter = mock(ConcurrentTimeCounter.class);

when(mockTimeCounter.getTimeSumAndCountAndReset()).thenReturn(1311768465173141116L);
when(mockTimeCounter.getAndResetMaxTime()).thenReturn(300);
when(mockTimeCounter.getAndResetMinTime()).thenReturn(100);

Method emitTimeCounterMetricsMethod = HttpPostEmitterMonitor.class.getDeclaredMethod("emitTimeCounterMetrics", ServiceEmitter.class, ConcurrentTimeCounter.class, String.class);
emitTimeCounterMetricsMethod.setAccessible(true);
emitTimeCounterMetricsMethod.invoke(monitor, mockServiceEmitter, mockTimeCounter, "emitter/test/");

ArgumentCaptor<ServiceMetricEvent.Builder> captor = ArgumentCaptor.forClass(ServiceMetricEvent.Builder.class);
verify(mockServiceEmitter, atMost(4)).emit(captor.capture());
}

@Test
public void testEmitTimeCounterMetricsDoesNotEmit() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException
{
ConcurrentTimeCounter mockTimeCounter = mock(ConcurrentTimeCounter.class);

when(mockTimeCounter.getTimeSumAndCountAndReset()).thenReturn(0L);
when(mockTimeCounter.getAndResetMaxTime()).thenReturn(null);
when(mockTimeCounter.getAndResetMinTime()).thenReturn(null);

Method emitTimeCounterMetricsMethod = HttpPostEmitterMonitor.class.getDeclaredMethod("emitTimeCounterMetrics", ServiceEmitter.class, ConcurrentTimeCounter.class, String.class);
emitTimeCounterMetricsMethod.setAccessible(true);
emitTimeCounterMetricsMethod.invoke(monitor, mockServiceEmitter, mockTimeCounter, "emitter/test/");

ArgumentCaptor<ServiceMetricEvent.Builder> captor = ArgumentCaptor.forClass(ServiceMetricEvent.Builder.class);
verify(mockServiceEmitter, never()).emit(captor.capture());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,46 @@ public void testSerdeWithDefaults() throws Exception

Assert.assertFalse(config.isEnabled());
}

@Test
public void testEquals()
{
JavaScriptConfig config1 = new JavaScriptConfig(true);
JavaScriptConfig config2 = new JavaScriptConfig(true);
JavaScriptConfig config3 = new JavaScriptConfig(false);

Assert.assertEquals(config1, config2);
Assert.assertNotEquals(config1, config3);
Assert.assertNotEquals(config2, config3);
Assert.assertNotEquals(config1, null);
Assert.assertNotEquals(config1, new Object());

Assert.assertTrue(config1.equals(config1));
}

@Test
public void testHashCode()
{
JavaScriptConfig config1 = new JavaScriptConfig(true);
JavaScriptConfig config2 = new JavaScriptConfig(true);
JavaScriptConfig config3 = new JavaScriptConfig(false);

Assert.assertEquals(config1.hashCode(), config2.hashCode());
Assert.assertNotEquals(config1.hashCode(), config3.hashCode());
}

@Test
public void testToString()
{
JavaScriptConfig javaScriptConfig = new JavaScriptConfig(true);
Assert.assertEquals("JavaScriptConfig{enabled=true}", javaScriptConfig.toString());
}

@Test
public void testEnabledInstance()
{
JavaScriptConfig javaScriptConfig = JavaScriptConfig.getEnabledInstance();
Assert.assertTrue(javaScriptConfig.isEnabled());
Assert.assertSame(javaScriptConfig, JavaScriptConfig.getEnabledInstance());
}
}
Loading