Minor concurrency and naming improvements.
Change-Id: I14dfb6d83dd5b84d5a8bb5cb67eda9f490b0e85a
Issue-ID: SDC-772
Signed-off-by: vempo <vitaliy.emporopulo@amdocs.com>
* @since 26 Mar 2018
*/
@SuppressWarnings({"UseOfSystemOutOrSystemErr", "CallToPrintStackTrace", "squid:S106", "squid:S1148"})
-public class HostAddress {
+public class HostAddressCache {
private static final long DEFAULT_REFRESH_INTERVAL = 60000L; // 1 min
private final long interval;
- private CacheEntry cachedAddress;
+ private volatile CacheEntry cachedAddress;
- public HostAddress() {
+ public HostAddressCache() {
this(DEFAULT_REFRESH_INTERVAL);
}
/**
* Creates a cache for host address with a custom refresh interval.
*/
- public HostAddress(long refreshInterval) {
+ public HostAddressCache(long refreshInterval) {
this.interval = refreshInterval;
this.cachedAddress = new CacheEntry(System.currentTimeMillis(), read());
}
import java.net.InetAddress;
import java.util.EnumMap;
import java.util.Map;
-import org.openecomp.sdc.logging.context.HostAddress;
+import org.openecomp.sdc.logging.context.HostAddressCache;
import org.openecomp.sdc.logging.context.InstanceId;
/**
*/
class GlobalContextProvider implements ContextProvider {
- private static final HostAddress HOST_ADDRESS_CACHE = new HostAddress();
+ private static final HostAddressCache HOST_ADDRESS_CACHE = new HostAddressCache();
@Override
public Map<ContextField, String> values() {
* @since 28 Mar 2018
*/
@PrepareForTest(InetAddress.class)
-public class HostAddressTest extends PowerMockTestCase {
+public class HostAddressCacheTest extends PowerMockTestCase {
@Test
public void hostAddressIsAlwaysPopulated() {
- assertNotNull(new HostAddress().get());
+ assertNotNull(new HostAddressCache().get());
}
@Test
public void cachedAddressRemainsTheSameWhenGotWithingRefreshInterval() throws UnknownHostException {
mockInetAddress(1);
- HostAddress addressCache = new HostAddress(1000);
+ HostAddressCache addressCache = new HostAddressCache(1000);
addressCache.get();
addressCache.get();
}
@Test
public void cachedAddressReplacedWhenGotAfterRefreshInterval() throws UnknownHostException {
mockInetAddress(2);
- HostAddress addressCache = new HostAddress(-1);
+ HostAddressCache addressCache = new HostAddressCache(-1);
addressCache.get();
addressCache.get();
}