Skip to content

Commit

Permalink
fix fabric8io#3272: addressing npe and index logic in general
Browse files Browse the repository at this point in the history
  • Loading branch information
shawkins committed Jun 25, 2021
1 parent 1f5061f commit d73b51c
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
* Fix #3216: made the mock server aware of apiVersions
* Fix #3225: Pod metric does not have corresponding label selector variant
* Fix #3243: pipes provided to exec command are no longer closed on connection close, so that client can fully read the buffer after the command finishes.
* Fix #3272: prevent index npe after informer sees an empty list

#### Improvements
* Fix #3078: adding javadocs to further clarify patch, edit, replace, etc. and note the possibility of items being modified.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,12 @@ public void setIsRunning(BooleanSupplier isRunning) {
* @return registered indexers
*/
@Override
public Map<String, Function<T, List<String>>> getIndexers() {
return indexers;
public synchronized Map<String, Function<T, List<String>>> getIndexers() {
return Collections.unmodifiableMap(indexers);
}

@Override
public void addIndexers(Map<String, Function<T, List<String>>> indexersNew) {
public synchronized void addIndexers(Map<String, Function<T, List<String>>> indexersNew) {
if (isRunning.getAsBoolean()) {
throw new IllegalStateException("Cannot add indexers to a running informer.");
}
Expand Down Expand Up @@ -147,7 +147,7 @@ public synchronized Map<String, T> replace(List<T> list) {
this.items = newItems;

// rebuild any index
this.indices = new HashMap<>();
this.indices.values().stream().forEach(m -> m.clear());
for (Map.Entry<String, T> itemEntry : items.entrySet()) {
this.updateIndices(null, itemEntry.getValue(), itemEntry.getKey());
}
Expand Down Expand Up @@ -294,7 +294,7 @@ public synchronized List<T> byIndex(String indexName, String indexKey) {
* @param newObj new object
* @param key the key
*/
public void updateIndices(T oldObj, T newObj, String key) {
void updateIndices(T oldObj, T newObj, String key) {
if (oldObj != null) {
deleteFromIndices(oldObj, key);
}
Expand All @@ -307,7 +307,7 @@ public void updateIndices(T oldObj, T newObj, String key) {
continue;
}

Map<String, Set<String>> index = this.indices.computeIfAbsent(indexName, k -> new HashMap<>());
Map<String, Set<String>> index = this.indices.get(indexName);
for (String indexValue : indexValues) {
Set<String> indexSet = index.computeIfAbsent(indexValue, k -> new HashSet<>());
indexSet.add(key);
Expand Down Expand Up @@ -350,7 +350,7 @@ private void deleteFromIndices(T oldObj, String key) {
* @param indexName the index name
* @param indexFunc the index func
*/
public void addIndexFunc(String indexName, Function<T, List<String>> indexFunc) {
public synchronized void addIndexFunc(String indexName, Function<T, List<String>> indexFunc) {
this.indices.put(indexName, new HashMap<>());
this.indexers.put(indexName, indexFunc);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@
import org.junit.jupiter.api.Test;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;

class CacheTest {
private static Cache cache = new Cache("mock", CacheTest::mockIndexFunction, CacheTest::mockKeyFunction);
Expand All @@ -49,6 +51,9 @@ void testCacheIndex() {
List<String> allExistingKeys = cache.listKeys();
assertEquals(1, allExistingKeys.size());
assertEquals(key, allExistingKeys.get(0));

cache.replace(Collections.emptyList());
assertEquals(0, cache.byIndex("mock", "y").size());
}

@Test
Expand Down

0 comments on commit d73b51c

Please sign in to comment.