Skip to content

Commit

Permalink
FFM-12087 Tests for flush
Browse files Browse the repository at this point in the history
  • Loading branch information
erdirowlands committed Oct 3, 2024
1 parent 94142b4 commit a54afff
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 3 deletions.
53 changes: 50 additions & 3 deletions src/test/java/io/harness/cf/client/api/MetricsProcessorTest.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package io.harness.cf.client.api;

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

import io.harness.cf.client.connector.Connector;
import io.harness.cf.client.connector.ConnectorException;
Expand Down Expand Up @@ -34,11 +33,59 @@ public void setup() {
MockitoAnnotations.openMocks(this);
metricsProcessor =
Mockito.spy(
new MetricsProcessor(connector, BaseConfig.builder().bufferSize(10_001).build(), this));
new MetricsProcessor(
connector,
BaseConfig.builder().bufferSize(10_001).flushAnalyticsOnClose(true).build(),
this));

metricsProcessor.reset();
}

@Test
public void testFlushAnalyticsOnCloseEnabled() throws ConnectorException, InterruptedException {
// Arrange
Metrics mockMetrics = mock(Metrics.class);
doNothing().when(connector).postMetrics(mockMetrics);

// Act: Push some metrics data and call flush
Target target = Target.builder().identifier("target-1").build();
Variation variation = Variation.builder().identifier("true").value("true").build();
metricsProcessor.pushToQueue(target, "feature-1", variation);

// Mimic shutdown behavior
metricsProcessor.close();

// Assert: Verify that postMetrics is called during shutdown
verify(connector, times(1)).postMetrics(any(Metrics.class));
verifyNoMoreInteractions(connector);
}

@Test
public void testFlushAnalyticsOnCloseDisabled() throws ConnectorException, InterruptedException {
// Arrange
metricsProcessor =
Mockito.spy(
new MetricsProcessor(
connector,
BaseConfig.builder().bufferSize(10_001).flushAnalyticsOnClose(false).build(),
this));

Metrics mockMetrics = mock(Metrics.class);
doNothing().when(connector).postMetrics(mockMetrics);

// Act: Push some metrics data and call flush
Target target = Target.builder().identifier("target-1").build();
Variation variation = Variation.builder().identifier("true").value("true").build();
metricsProcessor.pushToQueue(target, "feature-1", variation);

// Mimic shutdown behavior
metricsProcessor.close();

// Assert: Verify that postMetrics not called
verify(connector, times(0)).postMetrics(any(Metrics.class));
metricsProcessor.reset();
}

@Test
public void testPushToQueue() throws InterruptedException {
ExecutorService WORKER_THREAD_POOL = Executors.newFixedThreadPool(BUFFER_SIZE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,7 @@ public int getTotalMetricEvaluations() {

@Override
public void close() {}

@Override
public void setIsShuttingDown() {}
}

0 comments on commit a54afff

Please sign in to comment.