Commit af3e8d5d authored by Ronak Sakhuja's avatar Ronak Sakhuja
Browse files

Added Retry for eventgrid

parent c6506166
package org.opengroup.osdu.azure.di;
import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
/**
* EventGridTopic settings.
*/
@Configuration
@ConfigurationProperties("azure.eventgridtopic")
@Getter
@Setter
public class EventGridTopicRetryConfiguration {
private int longRunningOperationRetryTimeout = -1;
/**
* Used to check if timeout is configured in application.properties.
* @return True if retry is configured.
*/
public boolean isTimeoutConfigured() {
if (this.longRunningOperationRetryTimeout != -1) {
return true;
}
return false;
}
}
......@@ -18,6 +18,7 @@ import com.microsoft.azure.eventgrid.EventGridClient;
import com.microsoft.azure.eventgrid.TopicCredentials;
import com.microsoft.azure.eventgrid.implementation.EventGridClientImpl;
import org.opengroup.osdu.azure.cache.EventGridTopicClientCache;
import org.opengroup.osdu.azure.di.EventGridTopicRetryConfiguration;
import org.opengroup.osdu.azure.partition.EventGridTopicPartitionInfoAzure;
import org.opengroup.osdu.azure.partition.PartitionServiceEventGridClient;
import org.opengroup.osdu.common.Validators;
......@@ -42,6 +43,9 @@ public class EventGridTopicClientFactoryImpl implements IEventGridTopicClientFac
@Autowired
private EventGridTopicClientCache clientCache;
@Autowired
private EventGridTopicRetryConfiguration eventGridTopicRetryConfiguration;
/**
*
* @param dataPartitionId Data partition id
......@@ -64,8 +68,12 @@ public class EventGridTopicClientFactoryImpl implements IEventGridTopicClientFac
TopicCredentials topicCredentials =
new TopicCredentials(eventGridTopicPartitionInfoAzure.getTopicAccessKey());
EventGridClient eventGridClient = new EventGridClientImpl(topicCredentials);
EventGridClient eventGridClient;
if (eventGridTopicRetryConfiguration.isTimeoutConfigured()) {
eventGridClient = new EventGridClientImpl(topicCredentials).withLongRunningOperationRetryTimeout(eventGridTopicRetryConfiguration.getLongRunningOperationRetryTimeout());
} else {
eventGridClient = new EventGridClientImpl(topicCredentials);
}
this.clientCache.put(cacheKey, eventGridClient);
return eventGridClient;
}
......
......@@ -23,6 +23,7 @@ import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.opengroup.osdu.azure.cache.EventGridTopicClientCache;
import org.opengroup.osdu.azure.di.EventGridTopicRetryConfiguration;
import org.opengroup.osdu.azure.partition.EventGridTopicPartitionInfoAzure;
import org.opengroup.osdu.azure.partition.PartitionServiceEventGridClient;
import org.opengroup.osdu.core.common.partition.PartitionException;
......@@ -43,6 +44,9 @@ class EventGridTopicClientFactoryImplTest {
@Mock
private PartitionServiceEventGridClient partitionService;
@Mock
private EventGridTopicRetryConfiguration eventGridTopicRetryConfiguration;
@InjectMocks
private EventGridTopicClientFactoryImpl sut;
......@@ -74,7 +78,27 @@ class EventGridTopicClientFactoryImplTest {
}
@Test
public void should_return_validClient_given_validPartitionId() throws PartitionException {
public void should_return_validClient_given_validPartitionId_without_retrytimeout() throws PartitionException {
// Setup
when(this.partitionService.getEventGridTopicInPartition(VALID_DATA_PARTIION_ID, "validtopic")).thenReturn(
EventGridTopicPartitionInfoAzure.builder()
.topicName(VALID_TOPIC_NAME)
.topicAccessKey(VALID_TOPICKEY_NAME).build());
when(this.clientCache.containsKey(any())).thenReturn(false);
when(this.eventGridTopicRetryConfiguration.isTimeoutConfigured()).thenReturn(false);
// Act
EventGridClient eventGridClient = this.sut.getClient(VALID_DATA_PARTIION_ID, "validtopic");
// Assert
assertNotNull(eventGridClient);
verify(this.clientCache, times(1)).put(any(), any());
verify(this.eventGridTopicRetryConfiguration,times(0)).getLongRunningOperationRetryTimeout();
}
@Test
public void should_return_validClient_given_validPartitionId_with_retrytimeout() throws PartitionException {
// Setup
when(this.partitionService.getEventGridTopicInPartition(VALID_DATA_PARTIION_ID, "validtopic")).thenReturn(
EventGridTopicPartitionInfoAzure.builder()
......@@ -82,6 +106,7 @@ class EventGridTopicClientFactoryImplTest {
.topicAccessKey(VALID_TOPICKEY_NAME).build());
when(this.clientCache.containsKey(any())).thenReturn(false);
when(this.eventGridTopicRetryConfiguration.isTimeoutConfigured()).thenReturn(true);
// Act
EventGridClient eventGridClient = this.sut.getClient(VALID_DATA_PARTIION_ID, "validtopic");
......@@ -89,6 +114,7 @@ class EventGridTopicClientFactoryImplTest {
// Assert
assertNotNull(eventGridClient);
verify(this.clientCache, times(1)).put(any(), any());
verify(this.eventGridTopicRetryConfiguration,times(1)).getLongRunningOperationRetryTimeout();
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment