Skip to content

Commit

Permalink
fix: lazy load connectivity info
Browse files Browse the repository at this point in the history
Initial calls to getCachedConnectivityInfo() may return an empty list if
it occurs before an initial call to getConnectivityInfo(). This change
will ensure that getConnectivityInfo() has been called when returning
cached results.
  • Loading branch information
jbutler committed Aug 18, 2022
1 parent 017c954 commit c4616cb
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ public ConnectivityInfoProvider(DeviceConfiguration deviceConfiguration,
* @return list of cached connectivity info items
*/
public List<String> getCachedHostAddresses() {
if (cachedHostAddresses.isEmpty()) {
getConnectivityInfo();
}
return cachedHostAddresses;
}

Expand All @@ -68,7 +71,9 @@ public List<ConnectivityInfo> getConnectivityInfo() {
try {
GetConnectivityInfoResponse getConnectivityInfoResponse = clientFactory.getGreengrassV2DataClient()
.getConnectivityInfo(getConnectivityInfoRequest);
if (getConnectivityInfoResponse.hasConnectivityInfo()) {
if (getConnectivityInfoResponse == null) {
LOGGER.atWarn().log("Failed to retrieve connectivity info");
} else if (getConnectivityInfoResponse.hasConnectivityInfo()) {
// Filter out port and metadata since it is not needed
connectivityInfoList = getConnectivityInfoResponse.connectivityInfo();
cachedHostAddresses = new ArrayList<>(connectivityInfoList.stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public void setup() {

@SuppressWarnings("PMD.AvoidUsingHardCodedIP")
@Test
void GIVEN_connectivity_info_WHEN_get_connectivity_info_THEN_connectivity_info_returned() {
void GIVEN_connectivity_info_WHEN_getConnectivityInfo_THEN_connectivity_info_returned() {
ConnectivityInfo connectivityInfo = ConnectivityInfo.builder().hostAddress("172.8.8.10")
.metadata("").id("172.8.8.10").portNumber(8883).build();
ConnectivityInfo connectivityInfo1 = ConnectivityInfo.builder().hostAddress("localhost")
Expand All @@ -82,7 +82,7 @@ void GIVEN_connectivity_info_WHEN_get_connectivity_info_THEN_connectivity_info_r
}

@Test
void GIVEN_no_connectivity_info_WHEN_get_connectivity_info_THEN_no_connectivity_info_returned() {
void GIVEN_no_connectivity_info_WHEN_getConnectivityInfo_THEN_no_connectivity_info_returned() {
GetConnectivityInfoResponse getConnectivityInfoResponse = GetConnectivityInfoResponse.builder().build();
doReturn(getConnectivityInfoResponse).when(greengrassV2DataClient)
.getConnectivityInfo(any(GetConnectivityInfoRequest.class));
Expand All @@ -94,7 +94,7 @@ void GIVEN_no_connectivity_info_WHEN_get_connectivity_info_THEN_no_connectivity_
}

@Test
void GIVEN_cloudThrowValidationException_WHEN_get_connectivity_info_THEN_no_connectivity_info_returned(
void GIVEN_cloudThrowValidationException_WHEN_getConnectivityInfo_THEN_no_connectivity_info_returned(
ExtensionContext context) {
ignoreExceptionOfType(context, ValidationException.class);
when(greengrassV2DataClient.getConnectivityInfo(any(GetConnectivityInfoRequest.class)))
Expand All @@ -105,7 +105,7 @@ void GIVEN_cloudThrowValidationException_WHEN_get_connectivity_info_THEN_no_conn

@SuppressWarnings("PMD.AvoidUsingHardCodedIP")
@Test
void GIVEN_cached_connectivity_info_WHEN_get_cached_connectivity_info_THEN_connectivity_info_returned() {
void GIVEN_no_cached_connectivity_info_WHEN_getCachedHostAddresses_THEN_connectivity_info_lazy_loaded() {
ConnectivityInfo connectivityInfo = ConnectivityInfo.builder().hostAddress("172.8.8.10")
.metadata("").id("172.8.8.10").portNumber(8883).build();
ConnectivityInfo connectivityInfo1 = ConnectivityInfo.builder().hostAddress("localhost")
Expand All @@ -115,7 +115,7 @@ void GIVEN_cached_connectivity_info_WHEN_get_cached_connectivity_info_THEN_conne
doReturn(getConnectivityInfoResponse).when(greengrassV2DataClient)
.getConnectivityInfo(any(GetConnectivityInfoRequest.class));

connectivityInfoProvider.getConnectivityInfo();
//connectivityInfoProvider.getConnectivityInfo();
List<String> connectivityInfos = connectivityInfoProvider.getCachedHostAddresses();
assertThat(connectivityInfos, containsInAnyOrder("172.8.8.10", "localhost"));
}
Expand Down

0 comments on commit c4616cb

Please sign in to comment.