1 | # ***** BEGIN LICENSE BLOCK *****
|
---|
2 | # Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
---|
3 | #
|
---|
4 | # The contents of this file are subject to the Mozilla Public License Version
|
---|
5 | # 1.1 (the "License"); you may not use this file except in compliance with
|
---|
6 | # the License. You may obtain a copy of the License at
|
---|
7 | # http://www.mozilla.org/MPL/
|
---|
8 | #
|
---|
9 | # Software distributed under the License is distributed on an "AS IS" basis,
|
---|
10 | # WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
---|
11 | # for the specific language governing rights and limitations under the
|
---|
12 | # License.
|
---|
13 | #
|
---|
14 | # The Original Code is the Python XPCOM language bindings.
|
---|
15 | #
|
---|
16 | # The Initial Developer of the Original Code is
|
---|
17 | # ActiveState Tool Corp.
|
---|
18 | # Portions created by the Initial Developer are Copyright (C) 2000, 2001
|
---|
19 | # the Initial Developer. All Rights Reserved.
|
---|
20 | #
|
---|
21 | # Contributor(s):
|
---|
22 | # Mark Hammond <markh@activestate.com> (original author)
|
---|
23 | #
|
---|
24 | # Alternatively, the contents of this file may be used under the terms of
|
---|
25 | # either the GNU General Public License Version 2 or later (the "GPL"), or
|
---|
26 | # the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
---|
27 | # in which case the provisions of the GPL or the LGPL are applicable instead
|
---|
28 | # of those above. If you wish to allow use of your version of this file only
|
---|
29 | # under the terms of either the GPL or the LGPL, and not to allow others to
|
---|
30 | # use your version of this file under the terms of the MPL, indicate your
|
---|
31 | # decision by deleting the provisions above and replace them with the notice
|
---|
32 | # and other provisions required by the GPL or the LGPL. If you do not delete
|
---|
33 | # the provisions above, a recipient may use your version of this file under
|
---|
34 | # the terms of any one of the MPL, the GPL or the LGPL.
|
---|
35 | #
|
---|
36 | # ***** END LICENSE BLOCK *****
|
---|
37 |
|
---|
38 | # test_weakreferences.py - Test our weak reference implementation.
|
---|
39 | from xpcom import components, _xpcom
|
---|
40 | import xpcom.server, xpcom.client
|
---|
41 | from pyxpcom_test_tools import suite_from_functions, testmain
|
---|
42 |
|
---|
43 | try:
|
---|
44 | from sys import gettotalrefcount
|
---|
45 | except ImportError:
|
---|
46 | # Not a Debug build - assume no references (can't be leaks then :-)
|
---|
47 | gettotalrefcount = lambda: 0
|
---|
48 |
|
---|
49 | num_alive = 0
|
---|
50 |
|
---|
51 | class koTestSimple:
|
---|
52 | _com_interfaces_ = [components.interfaces.nsIInputStream]
|
---|
53 | def __init__(self):
|
---|
54 | global num_alive
|
---|
55 | num_alive += 1
|
---|
56 | def __del__(self):
|
---|
57 | global num_alive
|
---|
58 | num_alive -= 1
|
---|
59 | def close( self ):
|
---|
60 | pass
|
---|
61 |
|
---|
62 | def test():
|
---|
63 | ob = xpcom.server.WrapObject( koTestSimple(), components.interfaces.nsIInputStream)
|
---|
64 |
|
---|
65 | if num_alive != 1: raise RuntimeError, "Eeek - there are %d objects alive" % (num_alive,)
|
---|
66 |
|
---|
67 | # Check we can create a weak reference to our object.
|
---|
68 | wr = xpcom.client.WeakReference(ob)
|
---|
69 | if num_alive != 1: raise RuntimeError, "Eeek - there are %d objects alive" % (num_alive,)
|
---|
70 |
|
---|
71 | # Check we can call methods via the weak reference.
|
---|
72 | if wr() is None: raise RuntimeError, "Our weak-reference is returning None before it should!"
|
---|
73 | wr().close()
|
---|
74 |
|
---|
75 | ob = None # This should kill the object.
|
---|
76 | if num_alive != 0: raise RuntimeError, "Eeek - there are %d objects alive" % (num_alive,)
|
---|
77 | if wr() is not None: raise RuntimeError, "Our weak-reference is not returning None when it should!"
|
---|
78 |
|
---|
79 | # Now a test that we can get a _new_ interface from the weak reference - ie,
|
---|
80 | # an IID the real object has never previously been queried for
|
---|
81 | # (this behaviour previously caused a bug - never again ;-)
|
---|
82 | ob = xpcom.server.WrapObject( koTestSimple(), components.interfaces.nsISupports)
|
---|
83 | if num_alive != 1: raise RuntimeError, "Eeek - there are %d objects alive" % (num_alive,)
|
---|
84 | wr = xpcom.client.WeakReference(ob, components.interfaces.nsIInputStream)
|
---|
85 | if num_alive != 1: raise RuntimeError, "Eeek - there are %d objects alive" % (num_alive,)
|
---|
86 | wr() # This would die once upon a time ;-)
|
---|
87 | ob = None # This should kill the object.
|
---|
88 | if num_alive != 0: raise RuntimeError, "Eeek - there are %d objects alive" % (num_alive,)
|
---|
89 | if wr() is not None: raise RuntimeError, "Our weak-reference is not returning None when it should!"
|
---|
90 |
|
---|
91 | def test_refcount(num_loops=-1):
|
---|
92 | # Do the test lots of times - can help shake-out ref-count bugs.
|
---|
93 | if num_loops == -1: num_loops = 10
|
---|
94 | for i in xrange(num_loops):
|
---|
95 | test()
|
---|
96 |
|
---|
97 | if i==0:
|
---|
98 | # First loop is likely to "leak" as we cache things.
|
---|
99 | # Leaking after that is a problem.
|
---|
100 | num_refs = gettotalrefcount()
|
---|
101 |
|
---|
102 | lost = gettotalrefcount() - num_refs
|
---|
103 | # Sometimes we get spurious counts off by 1 or 2.
|
---|
104 | # This can't indicate a real leak, as we have looped
|
---|
105 | # more than twice!
|
---|
106 | if abs(lost)>2:
|
---|
107 | print "*** Lost %d references" % (lost,)
|
---|
108 |
|
---|
109 | # Make this test run under our std test suite
|
---|
110 | def suite():
|
---|
111 | return suite_from_functions(test_refcount)
|
---|
112 |
|
---|
113 | if __name__=='__main__':
|
---|
114 | testmain()
|
---|