lucenenet-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From synhers...@apache.org
Subject [23/49] lucenenet git commit: Ported Core.Util.TestWeakIdentityMap (partially)
Date Sun, 02 Oct 2016 10:16:54 GMT
Ported Core.Util.TestWeakIdentityMap (partially)


Project: http://git-wip-us.apache.org/repos/asf/lucenenet/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucenenet/commit/3b18a74a
Tree: http://git-wip-us.apache.org/repos/asf/lucenenet/tree/3b18a74a
Diff: http://git-wip-us.apache.org/repos/asf/lucenenet/diff/3b18a74a

Branch: refs/heads/master
Commit: 3b18a74a95c21ee4174fcd87c16bea5fe05d6752
Parents: 0c2f5ae
Author: Shad Storhaug <shad@shadstorhaug.com>
Authored: Sun Sep 11 02:22:30 2016 +0700
Committer: Shad Storhaug <shad@shadstorhaug.com>
Committed: Sun Sep 11 02:22:30 2016 +0700

----------------------------------------------------------------------
 src/Lucene.Net.Tests/Lucene.Net.Tests.csproj    |  1 +
 .../core/Util/TestWeakIdentityMap.cs            | 83 ++++++++++++--------
 2 files changed, 52 insertions(+), 32 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucenenet/blob/3b18a74a/src/Lucene.Net.Tests/Lucene.Net.Tests.csproj
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Tests/Lucene.Net.Tests.csproj b/src/Lucene.Net.Tests/Lucene.Net.Tests.csproj
index dc54705..32dea94 100644
--- a/src/Lucene.Net.Tests/Lucene.Net.Tests.csproj
+++ b/src/Lucene.Net.Tests/Lucene.Net.Tests.csproj
@@ -608,6 +608,7 @@
     <Compile Include="core\Util\TestWAH8DocIdSet.cs">
       <SubType>Code</SubType>
     </Compile>
+    <Compile Include="core\Util\TestWeakIdentityMap.cs" />
   </ItemGroup>
   <ItemGroup>
     <Folder Include="core\Util\Cache\" />

http://git-wip-us.apache.org/repos/asf/lucenenet/blob/3b18a74a/src/Lucene.Net.Tests/core/Util/TestWeakIdentityMap.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Tests/core/Util/TestWeakIdentityMap.cs b/src/Lucene.Net.Tests/core/Util/TestWeakIdentityMap.cs
index 19939c3..82c5c4c 100644
--- a/src/Lucene.Net.Tests/core/Util/TestWeakIdentityMap.cs
+++ b/src/Lucene.Net.Tests/core/Util/TestWeakIdentityMap.cs
@@ -1,28 +1,28 @@
-using System;
-using System.Collections.Generic;
-using System.Threading;
-/*
- * 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.
- */
 using Lucene.Net.Randomized.Generators;
 using Lucene.Net.Support;
 using NUnit.Framework;
+using System;
+using System.Collections.Generic;
+using System.Threading;
 
 namespace Lucene.Net.Util
 {
+    /*
+     * 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.
+     */
 
     [TestFixture]
     public class TestWeakIdentityMap : LuceneTestCase
@@ -35,8 +35,14 @@ namespace Lucene.Net.Util
             // we keep strong references to the keys,
             // so WeakIdentityMap will not forget about them:
             string key1 = "foo";
-            string key2 = "foo";
-            string key3 = "foo";
+            string key2 = "test1 foo".Split(' ')[1];
+            string key3 = "test2 foo".Split(' ')[1];
+
+            // LUCENENET NOTE: As per http://stackoverflow.com/a/543329/181087,
+            // the above hack is required in order to ensure the AreNotSame
+            // check will work. If you assign the same string to 3 different variables
+            // without doing some kind of manipulation from the original string, the
+            // AreNotSame test will fail because the references will be the same.
 
             Assert.AreNotSame(key1, key2);
             Assert.AreEqual(key1, key2);
@@ -102,7 +108,8 @@ namespace Lucene.Net.Util
                 //Assert.IsTrue(iter.hasNext()); // try again, should return same result!
                 string k = iter.Current;
                 Assert.IsTrue(k == key1 || k == key2 | k == key3);
-                keysAssigned += (k == key1) ? 1 : ((k == key2) ? 2 : 4);
+                // LUCENENET NOTE: Need object.ReferenceEquals here because the == operator
does more than check reference equality
+                keysAssigned += object.ReferenceEquals(k, key1) ? 1 : (object.ReferenceEquals(k,
key2) ? 2 : 4);
                 c++;
             }
             Assert.AreEqual(3, c);
@@ -126,12 +133,13 @@ namespace Lucene.Net.Util
             {
                 try
                 {
-                    System.RunFinalization();
-                    System.gc();
+                    //System.RunFinalization();
+                    //System.gc();
+                    GC.Collect();
                     int newSize = map.Size();
                     Assert.IsTrue(size >= newSize, "previousSize(" + size + ")>=newSize("
+ newSize + ")");
                     size = newSize;
-                    Thread.Sleep(new TimeSpan(100L));
+                    Thread.Sleep(new TimeSpan(0, 0, 1));
                     c = 0;
                     for (IEnumerator<string> iter = map.Keys.GetEnumerator(); iter.MoveNext();
)
                     {
@@ -163,8 +171,14 @@ namespace Lucene.Net.Util
             {
             }*/
 
-            key1 = "foo";
-            key2 = "foo";
+            // LUCENENET NOTE: As per http://stackoverflow.com/a/543329/181087,
+            // the following hack is required in order to ensure the string references
+            // are different. If you assign the same string to 2 different variables
+            // without doing some kind of manipulation from the original string, the
+            // references will be the same.
+
+            key1 = "test3 foo".Split(' ')[1];
+            key2 = "test4 foo".Split(' ')[1];
             map.Put(key1, "bar1");
             map.Put(key2, "bar2");
             Assert.AreEqual(2, map.Size());
@@ -174,12 +188,15 @@ namespace Lucene.Net.Util
             Assert.IsTrue(map.Empty);
         }
 
+        /* LUCENENET TODO: Compile issues
         [Test]
         public virtual void TestConcurrentHashMap()
         {
             // don't make threadCount and keyCount random, otherwise easily OOMs or fails
otherwise:
             const int threadCount = 8, keyCount = 1024;
-            ExecutorService exec = Executors.newFixedThreadPool(threadCount, new NamedThreadFactory("testConcurrentHashMap"));
+
+            System.Threading.ThreadPool.QueueUserWorkItem(;
+            //ExecutorService exec = Executors.newFixedThreadPool(threadCount, new NamedThreadFactory("testConcurrentHashMap"));
             WeakIdentityMap<object, int?> map = WeakIdentityMap<object, int?>.NewConcurrentHashMap(Random().NextBoolean());
             // we keep strong references to the keys,
             // so WeakIdentityMap will not forget about them:
@@ -215,14 +232,15 @@ namespace Lucene.Net.Util
             {
                 try
                 {
-                    System.runFinalization();
-                    System.gc();
+                    //System.runFinalization();
+                    //System.gc();
+                    GC.Collect();
                     int newSize = map.Size();
                     Assert.IsTrue(size >= newSize, "previousSize(" + size + ")>=newSize("
+ newSize + ")");
                     size = newSize;
                     Thread.Sleep(new TimeSpan(100L));
                     int c = 0;
-                    for (IEnumerator<object> it = map.Keys.GetEnumerator(); it.MoveNext();
)
+                    for (IEnumerator<object> it = map.Keys.GetEnumerator(); it.MoveNext();)
                     {
                         Assert.IsNotNull(it.Current);
                         c++;
@@ -283,7 +301,7 @@ namespace Lucene.Net.Util
                             break;
                         case 4:
                             // check iterator still working
-                            for (IEnumerator<object> it = Map.Keys.GetEnumerator();
it.MoveNext(); )
+                            for (IEnumerator<object> it = Map.Keys.GetEnumerator();
it.MoveNext();)
                             {
                                 Assert.IsNotNull(it.Current);
                             }
@@ -295,5 +313,6 @@ namespace Lucene.Net.Util
                 }
             }
         }
+        */
     }
 }
\ No newline at end of file


Mime
View raw message