[Python-checkins] CVS: python/dist/src/Lib/test test_threaded_import.py,NONE,1.1

Guido van Rossum guido@digicool.com
Tue, 22 May 2001 12:04:41 -0400


> ICK ALERT:  read the long comment block before run_the_test().  It was
> almost impossible to get this to run without instant deadlock, and the
> solution here sucks on several counts.  If you can dream up a better way,
> let me know!

> # Tricky, tricky, tricky.
> # When regrtest imports this module, the thread running regrtest grabs the
> # import lock and won't let go of it until this module returns.  All other
> # threads attempting an import hang for the duration.  So we have to spawn
> # a thread to run the test and return to regrtest.py right away, else the
> # test can't make progress.

I guess the only way around this is to design a protocol so that tests
may be imported and run in two steps:

    import test.test_foo
    test.test_foo.run()

I can imagine making regrtest look for a specific name in the imported
module (e.g. test_main()) and call it if it exists.  Then the test (if it
uses this feature) should end with the usual

    if __name__ == '__main__':
        test_main()

> # One miserable consequence:  This test can't wait to make sure all the
> # threads complete!
> #
> # Another:  If this test fails, the output may show up while running
> # some other test.
> #
> # Another:  If you run this test directly, the OS will probably kill
> # all the threads right away, because the program exits immediately
> # after spawning a thread to run the real test.

This one could be fixed by testing for __name__ == '__main__', and
should be unless we decide to do the above feature (which I like).

> # Another:  If this test ever does fail and you attempt to run it by
> # itself via regrtest, the same applies:  regrtest will get out so fast
> # the OS will kill all the threads here.

--Guido van Rossum (home page: http://www.python.org/~guido/)