为什么在此单元测试中未调用finalize函数? [英] Why does the finalize function not get called in this unit test?

查看:89
本文介绍了为什么在此单元测试中未调用finalize函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写Java单元测试,以测试对对象上的终结器的调用的效果.

I'm trying to write a Java unit test that tests the effects of a call to a finalizer on an object.

为了确保终结器被调用,我正在使用我在Stackoverflow上其他地方看到的WeakReference方法.

In order to be sure the finalizer gets called I'm using a WeakReference method I saw elsewhere on stackoverflow.

我的问题是,在该测试中,即使WeakReference在一次迭代后就为null,也永远不会调用TestFinalizer的finalize方法:

My problem is that in this test the finalize method of TestFinalizer never gets called even though the WeakReference comes up null after just one iteration:

public class FinalizerTest {    
    private static class TestFinalizer {
        public static class Callback {
            public int NumFinalize = 0;

            public void finalized(){
                NumFinalize++;
            }
        }
        private Callback callback;

        public TestFinalizer(Callback callback){
            this.callback = callback;
        }

        @Override
        public void finalize() throws Throwable {
            callback.finalized();
            super.finalize();
        }
    }

    @Test
    public void testForceFinalizer(){
        TestFinalizer.Callback callback = new TestFinalizer.Callback();
        TestFinalizer testFinalizer = new TestFinalizer(callback); 
        // Try to force finalizer to be called
        WeakReference<Object> ref = new WeakReference<Object>(testFinalizer);
        testFinalizer = null;
        int maxTries = 10000, i=0;
        while (ref.get() != null && i<maxTries) {
            ++i;
            System.gc();
        }
        if ( ref.get() != null )
            fail("testFinalizer didn't get cleaned up within maxTries");

        // Last line passes, next fails!
        assertEquals("Should be exactly one call to finalizer", 1, callback.NumFinalize);
    }
}

推荐答案

在单元测试中添加Thread.sleep(3000)可以在我的计算机上解决此问题:

Adding a Thread.sleep(3000) in the unit test fixed this issue on my machine:

 @Test
    public void testForceFinalizer() throws InterruptedException
    {
    FinalizerTest.TestFinalizer f = new FinalizerTest.TestFinalizer(null);
    FinalizerTest.TestFinalizer.Callback callback =  f.new Callback();
    TestFinalizer testFinalizer = new TestFinalizer(callback); // Try to
                                   // force
                                   // finalizer
                                   // to be
                                   // called
    WeakReference<Object> ref = new WeakReference<Object>(testFinalizer);
    testFinalizer = null;
    int maxTries = 10000, i = 0;
    while (ref.get() != null && i < maxTries)
    {
        ++i;
        System.gc();
    }
    if (ref.get() != null)
        fail("testFinalizer didn't get cleaned up within maxTries"); // Last
                                     // line
                                     // passes,
                                     // next
                                     // fails!
    System.out.println("Value: " + callback.NumFinalize);
    Thread.sleep(3000);
    assertEquals("Should be exactly one call to finalizer", 1,
        callback.NumFinalize);

    System.out.println("Value after: " + callback.NumFinalize);

    }

这是在assertEquals调用之前执行的.正如其他人所说,调用System.gc()是一个建议,如果系统选择这样做,则系统可以忽略您.另外,我还确保没有什么是静态的,不确定是否重要.

This is performed right before the assertEquals call . As others have stated calling System.gc() is a suggestion and the system can ignore you if it so chooses. As an added, I also made sure nothing was static, not sure if that matters.

这篇关于为什么在此单元测试中未调用finalize函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆