Java/C# smart pointer tests: Give GC more time to collect objects

This commit is contained in:
William S Fulton 2014-03-15 23:39:37 +00:00
commit 87963d2e68
6 changed files with 137 additions and 67 deletions

View file

@ -6,6 +6,13 @@ public class runme
// Debugging flag // Debugging flag
public static bool debug = false; public static bool debug = false;
private static void WaitForGC()
{
System.GC.Collect();
System.GC.WaitForPendingFinalizers();
System.Threading.Thread.Sleep(10);
}
static void Main() static void Main()
{ {
if (debug) if (debug)
@ -27,22 +34,24 @@ public class runme
if (debug) if (debug)
Console.WriteLine("Nearly finished"); Console.WriteLine("Nearly finished");
int countdown = 100; {
while (true) { int countdown = 500;
System.GC.Collect(); int expectedCount = 1;
System.GC.WaitForPendingFinalizers(); while (true) {
System.Threading.Thread.Sleep(10); WaitForGC();
if (--countdown == 0) if (--countdown == 0)
break; break;
if (Klass.getTotal_count() == 1) // Expect 1 instance - the one global variable (GlobalValue) if (Klass.getTotal_count() == expectedCount) // Expect the one global variable (GlobalValue)
break; break;
}; }
if (Klass.getTotal_count() != 1) int actualCount = Klass.getTotal_count();
throw new ApplicationException("Klass.total_count=" + Klass.getTotal_count()); if (actualCount != expectedCount)
throw new ApplicationException("Expected count: " + expectedCount + " Actual count: " + actualCount);
}
int wrapper_count = li_boost_shared_ptr.shared_ptr_wrapper_count(); int wrapper_count = li_boost_shared_ptr.shared_ptr_wrapper_count();
if (wrapper_count != li_boost_shared_ptr.NOT_COUNTING) if (wrapper_count != li_boost_shared_ptr.NOT_COUNTING)
if (wrapper_count != 1) // Expect 1 instance - the one global variable (GlobalSmartValue) if (wrapper_count != 1) // Expect the one global variable (GlobalSmartValue)
throw new ApplicationException("shared_ptr wrapper count=" + wrapper_count); throw new ApplicationException("shared_ptr wrapper count=" + wrapper_count);
if (debug) if (debug)

View file

@ -20,18 +20,38 @@ public class li_std_auto_ptr_runme {
throw new Exception("number of objects should be 2"); throw new Exception("number of objects should be 2");
k1 = null; k1 = null;
WaitForGC(); {
int countdown = 500;
if (Klass.getTotal_count() != 1) int expectedCount = 1;
throw new Exception("number of objects should be 1"); while (true) {
WaitForGC();
if (--countdown == 0)
break;
if (Klass.getTotal_count() == expectedCount)
break;
};
int actualCount = Klass.getTotal_count();
if (actualCount != expectedCount)
throw new ApplicationException("Expected count: " + expectedCount + " Actual count: " + actualCount);
}
if (k2.getLabel() != "second") if (k2.getLabel() != "second")
throw new Exception("wrong object label"); throw new Exception("wrong object label");
k2 = null; k2 = null;
WaitForGC(); {
int countdown = 500;
if (Klass.getTotal_count() != 0) int expectedCount = 0;
throw new Exception("no objects should be left"); while (true) {
WaitForGC();
if (--countdown == 0)
break;
if (Klass.getTotal_count() == expectedCount)
break;
}
int actualCount = Klass.getTotal_count();
if (actualCount != expectedCount)
throw new ApplicationException("Expected count: " + expectedCount + " Actual count: " + actualCount);
}
} }
} }

View file

@ -13,6 +13,16 @@ public class java_director_runme {
} }
} }
private static void WaitForGC()
{
System.gc();
System.runFinalization();
try {
java.lang.Thread.sleep(10);
} catch (java.lang.InterruptedException e) {
}
}
public static void main(String argv[]) { public static void main(String argv[]) {
QuuxContainer qc = createContainer(); QuuxContainer qc = createContainer();
@ -31,24 +41,21 @@ public class java_director_runme {
qc = null; qc = null;
/* Watch qc get reaped, which causes the C++ object to delete /* Watch qc get reaped, which causes the C++ object to delete
objects from the internal vector */ objects from the internal vector */
System.gc(); {
System.runFinalization(); int countdown = 500;
int expectedCount = 0;
// Give the finalizers a chance to run while (true) {
try { WaitForGC();
Thread.sleep(50); if (--countdown == 0)
} catch (InterruptedException e) { break;
if (Quux.instances() == expectedCount)
break;
};
int actualCount = Quux.instances();
if (actualCount != expectedCount)
throw new RuntimeException("Expected count: " + expectedCount + " Actual count: " + actualCount);
} }
/* Watch the Quux objects formerly in the QuuxContainer object
get reaped */
System.gc();
System.runFinalization();
instances = Quux.instances();
if (instances != 0)
throw new RuntimeException("Quux instances should be 0, actually " + instances);
/* Test Quux1's director disconnect method rename */ /* Test Quux1's director disconnect method rename */
Quux1 quux1 = new Quux1("quux1"); Quux1 quux1 = new Quux1("quux1");
if (quux1.disconnectMethodCalled) if (quux1.disconnectMethodCalled)

View file

@ -13,6 +13,16 @@ public class li_boost_intrusive_ptr_runme {
// Debugging flag // Debugging flag
public final static boolean debug = false; public final static boolean debug = false;
private static void WaitForGC()
{
System.gc();
System.runFinalization();
try {
java.lang.Thread.sleep(10);
} catch (java.lang.InterruptedException e) {
}
}
public static void main(String argv[]) public static void main(String argv[])
{ {
if (debug) if (debug)
@ -39,12 +49,7 @@ public class li_boost_intrusive_ptr_runme {
int countdown = 50; int countdown = 50;
while (true) { while (true) {
System.gc(); WaitForGC();
System.runFinalization();
try {
java.lang.Thread.sleep(100);
} catch (java.lang.InterruptedException e) {
}
if (--countdown == 0) if (--countdown == 0)
break; break;
if (Klass.getTotal_count() == 1 && KlassWithoutRefCount.getTotal_count() == 0 && if (Klass.getTotal_count() == 1 && KlassWithoutRefCount.getTotal_count() == 0 &&
@ -52,7 +57,7 @@ public class li_boost_intrusive_ptr_runme {
KlassDerived.getTotal_count() == 0 && KlassDerivedDerived.getTotal_count() == 1) KlassDerived.getTotal_count() == 0 && KlassDerivedDerived.getTotal_count() == 1)
// Expect 1 Klass instance - the one global variable (GlobalValue) // Expect 1 Klass instance - the one global variable (GlobalValue)
break; break;
}; }
if (Klass.getTotal_count() != 1) if (Klass.getTotal_count() != 1)
throw new RuntimeException("Klass.total_count=" + Klass.getTotal_count()); throw new RuntimeException("Klass.total_count=" + Klass.getTotal_count());
if (KlassWithoutRefCount.getTotal_count() != 0) if (KlassWithoutRefCount.getTotal_count() != 0)

View file

@ -13,6 +13,16 @@ public class li_boost_shared_ptr_runme {
// Debugging flag // Debugging flag
public final static boolean debug = false; public final static boolean debug = false;
private static void WaitForGC()
{
System.gc();
System.runFinalization();
try {
java.lang.Thread.sleep(10);
} catch (java.lang.InterruptedException e) {
}
}
public static void main(String argv[]) public static void main(String argv[])
{ {
if (debug) if (debug)
@ -37,21 +47,20 @@ public class li_boost_shared_ptr_runme {
if (debug) if (debug)
System.out.println("Nearly finished"); System.out.println("Nearly finished");
int countdown = 100; {
while (true) { int countdown = 500;
System.gc(); int expectedCount = 1;
System.runFinalization(); while (true) {
try { WaitForGC();
java.lang.Thread.sleep(10); if (--countdown == 0)
} catch (java.lang.InterruptedException e) { break;
if (Klass.getTotal_count() == expectedCount) // Expect the one global variable (GlobalValue)
break;
} }
if (--countdown == 0) int actualCount = Klass.getTotal_count();
break; if (actualCount != expectedCount)
if (Klass.getTotal_count() == 1) // Expect 1 instance - the one global variable (GlobalValue) throw new RuntimeException("Expected count: " + expectedCount + " Actual count: " + actualCount);
break; }
};
if (Klass.getTotal_count() != 1)
throw new RuntimeException("Klass.total_count=" + Klass.getTotal_count());
int wrapper_count = li_boost_shared_ptr.shared_ptr_wrapper_count(); int wrapper_count = li_boost_shared_ptr.shared_ptr_wrapper_count();
if (wrapper_count != li_boost_shared_ptr.getNOT_COUNTING()) if (wrapper_count != li_boost_shared_ptr.getNOT_COUNTING())

View file

@ -15,7 +15,7 @@ public class li_std_auto_ptr_runme {
System.gc(); System.gc();
System.runFinalization(); System.runFinalization();
try { try {
java.lang.Thread.sleep(1); java.lang.Thread.sleep(10);
} catch (java.lang.InterruptedException e) { } catch (java.lang.InterruptedException e) {
} }
} }
@ -31,18 +31,38 @@ public class li_std_auto_ptr_runme {
throw new RuntimeException("number of objects should be 2"); throw new RuntimeException("number of objects should be 2");
k1 = null; k1 = null;
WaitForGC(); {
int countdown = 500;
if (Klass.getTotal_count() != 1) int expectedCount = 1;
throw new RuntimeException("number of objects should be 1"); while (true) {
WaitForGC();
if (--countdown == 0)
break;
if (Klass.getTotal_count() == expectedCount)
break;
}
int actualCount = Klass.getTotal_count();
if (actualCount != expectedCount)
throw new RuntimeException("Expected count: " + expectedCount + " Actual count: " + actualCount);
}
if (!k2.getLabel().equals("second")) if (!k2.getLabel().equals("second"))
throw new RuntimeException("wrong object label"); throw new RuntimeException("wrong object label");
k2 = null; k2 = null;
WaitForGC(); {
int countdown = 500;
if (Klass.getTotal_count() != 0) int expectedCount = 0;
throw new RuntimeException("no objects should be left"); while (true) {
WaitForGC();
if (--countdown == 0)
break;
if (Klass.getTotal_count() == expectedCount)
break;
};
int actualCount = Klass.getTotal_count();
if (actualCount != expectedCount)
throw new RuntimeException("Expected count: " + expectedCount + " Actual count: " + actualCount);
}
} }
} }