Skip to content

ConcurrentReferenceHashMap's entrySet violates the Map contract #27454

@ben-manes

Description

@ben-manes

A quick sanity test of this map using Guava's testlib found some simple violations. For example entrySet().iterator() does not throw an exception for the sequence [hasNext, hasNext, next, remove, remove]. In this case Iterator.remove() failed to null out the last property after the first call, so a subsequent call does not throw an IllegalStateException. As the keySet and values views delegate to entrySet, this error is found multiple times in Guava's suite. You might consider using this suite on other custom collections.

Unit Tests
import java.util.Map;

import org.springframework.util.ConcurrentReferenceHashMap;

import com.google.common.collect.testing.ConcurrentMapTestSuiteBuilder;
import com.google.common.collect.testing.TestStringMapGenerator;
import com.google.common.collect.testing.features.CollectionFeature;
import com.google.common.collect.testing.features.CollectionSize;
import com.google.common.collect.testing.features.MapFeature;

import junit.framework.Test;
import junit.framework.TestCase;

/** Guava testlib map tests. */
public final class ConcurrentReferenceHashMapTests extends TestCase {

  public static Test suite() {
    var suite = ConcurrentMapTestSuiteBuilder
        .using(new TestStringMapGenerator() {
          @Override protected Map<String, String> create(Map.Entry<String, String>[] entries) {
            var map = new ConcurrentReferenceHashMap<String, String>();
            for (var entry : entries) {
              map.put(entry.getKey(), entry.getValue());
            }
            return map;
          }
        })
        .named("ConcurrentReferenceHashMap")
        .withFeatures(
            MapFeature.GENERAL_PURPOSE,
            MapFeature.ALLOWS_ANY_NULL_QUERIES,
            CollectionFeature.SUPPORTS_ITERATOR_REMOVE,
            CollectionSize.ANY);
    return suite.createTestSuite();
  }
}

Metadata

Metadata

Assignees

Labels

in: coreIssues in core modules (aop, beans, core, context, expression)status: backportedAn issue that has been backported to maintenance branchestype: bugA general bug

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions