NightOwl888 commented on code in PR #1129: URL: https://github.com/apache/lucenenet/pull/1129#discussion_r1957248409
########## src/Lucene.Net.Tests/Support/TestConcurrentSetBase.cs: ########## @@ -0,0 +1,456 @@ +// Some tests adapted from Apache Harmony: +// https://github.com/apache/harmony/blob/02970cb7227a335edd2c8457ebdde0195a735733/classlib/modules/concurrent/src/test/java/ConcurrentHashMapTest.java +// https://github.com/apache/harmony/blob/02970cb7227a335edd2c8457ebdde0195a735733/classlib/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/util/CollectionsTest.java + +using J2N.Threading; +using Lucene.Net.Attributes; +using Lucene.Net.Support; +using Lucene.Net.Support.Threading; +using NUnit.Framework; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; +#nullable enable + +namespace Lucene.Net +{ + /* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + /// <summary> + /// Base class for tests for <see cref="TestConcurrentHashSet"/> and <see cref="TestConcurrentSet"/>. + /// </summary> + /// <remarks> + /// Some tests (those not marked with <see cref="LuceneNetSpecificAttribute"/>) + /// are adapted from Apache Harmony's ConcurrentHashMapTest class. This class + /// tests ConcurrentHashMap, which is a dictionary, but the key behavior is + /// similar to <see cref="ConcurrentHashSet{T}"/> and <see cref="ConcurrentSet{T}"/>. + /// </remarks> + public abstract class TestConcurrentSetBase : JSR166TestCase Review Comment: For consistency, let's name this `BaseConcurrentSetTestCase`. ########## src/Lucene.Net.Tests/Support/TestConcurrentSet.cs: ########## @@ -0,0 +1,42 @@ +// Some tests adapted from Apache Harmony: +// https://github.com/apache/harmony/blob/02970cb7227a335edd2c8457ebdde0195a735733/classlib/modules/concurrent/src/test/java/ConcurrentHashMapTest.java +// https://github.com/apache/harmony/blob/02970cb7227a335edd2c8457ebdde0195a735733/classlib/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/util/CollectionsTest.java + +using Lucene.Net.Support; +using System.Collections.Generic; + +namespace Lucene.Net +{ + /* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + /// <summary> + /// Tests for <see cref="ConcurrentSet{T}"/>. + /// </summary> + /// <remarks> + /// See the <see cref="TestConcurrentSetBase"/> class for most of the test cases. + /// This class specializes the tests for the <see cref="ConcurrentSet{T}"/> class. + /// </remarks> + public class TestConcurrentSet : TestConcurrentSetBase + { + protected override ISet<T> NewSet<T>() + => new ConcurrentSet<T>(new HashSet<T>()); Review Comment: There are 2 main benefits for having `ConcurrentSet<T>`: 1. It enables concurrency on alternative set implementations, such as `LinkedHashSet<T>` or `OrderedHashSet<T>`. 2. It exposes the shared lock (through the `SyncRoot` property), so external operations (such as atomic enumeration) can be synchronized with the rest of the set. > NOTE: Something smells funny about the implementation of the `ConcurentSet<T>.SyncRoot` property. It seems like the `System.Threading.Interlocked.CompareExchange<object?>(ref syncRoot, new object(), null);` line shouldn't be there or it should be setup to correctly update the value of `syncRoot`. The `ConcurrentHashSet<T>` has the advantage of not locking the entire set so in theory, will perform better. All of that said, it seems like it would be a more useful test if the [`OrderedHashSet<T>`](https://github.com/NightOwl888/J2N/issues/95) were tested here. But since it doesn't yet exist, perhaps we could settle for `LinkedHashSet<T>` for now. Better still, create another base class to provide the `ISet<T>` implementation to wrap with multiple subclasses to test each wrapped instance separately. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: dev-unsubscr...@lucenenet.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org