Added a test case for BlockingGetRecordsCache. Updated the comments on BlockingGetRecords cache. Synchronized the added and removed methods instead of using volatile variables.

This commit is contained in:
Sahil Palvia 2017-09-21 11:50:53 -07:00
parent 3404ddfcf4
commit ce8dd88846
3 changed files with 26 additions and 9 deletions

View file

@ -36,7 +36,9 @@ public class BlockingGetRecordsCache implements GetRecordsCache {
@Override
public void start() {
// Do nothing, this behavior is not supported by this cache.
//
// Nothing to do here
//
}
@Override
@ -50,6 +52,8 @@ public class BlockingGetRecordsCache implements GetRecordsCache {
@Override
public void shutdown() {
//
// Nothing to do here.
//
}
}

View file

@ -116,15 +116,15 @@ public class PrefetchGetRecordsCache implements GetRecordsCache {
}
private class PrefetchCounters {
private volatile long size = 0;
private volatile long byteSize = 0;
private long size = 0;
private long byteSize = 0;
public void added(final ProcessRecordsInput result) {
public synchronized void added(final ProcessRecordsInput result) {
size += getSize(result);
byteSize += getByteSize(result);
}
public void removed(final ProcessRecordsInput result) {
public synchronized void removed(final ProcessRecordsInput result) {
size -= getSize(result);
byteSize -= getByteSize(result);
}

View file

@ -21,6 +21,7 @@ import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.when;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import org.junit.Before;
@ -44,9 +45,8 @@ public class BlockingGetRecordsCacheTest {
private GetRecordsRetrievalStrategy getRecordsRetrievalStrategy;
@Mock
private GetRecordsResult getRecordsResult;
@Mock
private List<Record> records;
private List<Record> records = new ArrayList<>();
private BlockingGetRecordsCache blockingGetRecordsCache;
@Before
@ -58,7 +58,7 @@ public class BlockingGetRecordsCacheTest {
}
@Test
public void testGetNextRecords() {
public void testGetNextRecordsWithNoRecords() {
ProcessRecordsInput result = blockingGetRecordsCache.getNextResult();
assertEquals(result.getRecords(), records);
@ -66,4 +66,17 @@ public class BlockingGetRecordsCacheTest {
assertNull(result.getCacheExitTime());
assertEquals(result.getTimeSpentInCache(), Duration.ZERO);
}
@Test
public void testGetNextRecordsWithRecords() {
Record record = new Record();
records.add(record);
records.add(record);
records.add(record);
records.add(record);
ProcessRecordsInput result = blockingGetRecordsCache.getNextResult();
assertEquals(result.getRecords(), records);
}
}