Commit ce80bcd5 authored by Krishna Nikhil Vedurumudi's avatar Krishna Nikhil Vedurumudi
Browse files

Merge branch 'rosakhuj/eventgridRetry' into 'master'

Added Retry for eventgrid

See merge request !105
parents c6506166 b033f2f7
Pipeline #49151 passed with stages
in 7 minutes and 36 seconds
/** Copyright © Microsoft Corporation
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 org.opengroup.osdu.azure.di;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
/**
* EventGridTopic settings.
*/
@Configuration
@ConfigurationProperties("azure.eventgrid.topic")
@Setter
public class EventGridTopicRetryConfiguration {
private static final int DEFAULT_INT_VALUE = -1;
private int longRunningOperationRetryTimeout = DEFAULT_INT_VALUE;
/**
* Fetches value of Long Running Operation Retry Timeout.
* @return Value of longRunningOperationRetryTimeout greater than 0, else -1
*/
public int getLongRunningOperationRetryTimeout() {
if (this.longRunningOperationRetryTimeout < 0) {
return DEFAULT_INT_VALUE;
}
return this.longRunningOperationRetryTimeout;
}
/**
* Used to check if timeout is configured in application.properties.
* @return True if retry is configured.
*/
public boolean isTimeoutConfigured() {
if (this.getLongRunningOperationRetryTimeout() != DEFAULT_INT_VALUE) {
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,13 +106,17 @@ class EventGridTopicClientFactoryImplTest {
.topicAccessKey(VALID_TOPICKEY_NAME).build());
when(this.clientCache.containsKey(any())).thenReturn(false);
when(this.eventGridTopicRetryConfiguration.isTimeoutConfigured()).thenReturn(true);
when(this.eventGridTopicRetryConfiguration.getLongRunningOperationRetryTimeout()).thenReturn(20);
// Act
EventGridClient eventGridClient = this.sut.getClient(VALID_DATA_PARTIION_ID, "validtopic");
// Assert
assertNotNull(eventGridClient);
assertEquals(eventGridClient.longRunningOperationRetryTimeout(), 20);
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