• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

zopefoundation / transaction / 16399678488

18 Sep 2024 07:25AM UTC coverage: 99.793% (+0.1%) from 99.696%
16399678488

push

github

dataflake
- vb [ci skip]

299 of 306 branches covered (97.71%)

Branch coverage included in aggregate %.

3083 of 3083 relevant lines covered (100.0%)

1.0 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/src/transaction/tests/test_weakset.py
1
##############################################################################
2
#
3
# Copyright (c) 2007 Zope Foundation and Contributors.
4
# All Rights Reserved.
5
#
6
# This software is subject to the provisions of the Zope Public License,
7
# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
8
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
9
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
10
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
11
# FOR A PARTICULAR PURPOSE
12
#
13
##############################################################################
14
import sys
1✔
15
import unittest
1✔
16

17

18
class WeakSetTests(unittest.TestCase):
1✔
19
    def test_contains(self):
1✔
20
        from transaction.weakset import WeakSet
1✔
21
        w = WeakSet()
1✔
22
        dummy = Dummy()
1✔
23
        w.add(dummy)
1✔
24
        self.assertEqual(dummy in w, True)
1✔
25
        dummy2 = Dummy()
1✔
26
        self.assertEqual(dummy2 in w, False)
1✔
27

28
    def test_len(self):
1✔
29
        import gc
1✔
30

31
        from transaction.weakset import WeakSet
1✔
32
        w = WeakSet()
1✔
33
        d1 = Dummy()
1✔
34
        d2 = Dummy()
1✔
35
        w.add(d1)
1✔
36
        w.add(d2)
1✔
37
        self.assertEqual(len(w), 2)
1✔
38
        del d1
1✔
39
        gc.collect()
1✔
40
        if sys.platform.startswith('java'):
1✔
41
            # The Jython GC is non deterministic
42
            pass  # pragma: no cover
43
        else:
44
            self.assertEqual(len(w), 1)
1✔
45

46
    def test_remove(self):
1✔
47
        from transaction.weakset import WeakSet
1✔
48
        w = WeakSet()
1✔
49
        dummy = Dummy()
1✔
50
        w.add(dummy)
1✔
51
        self.assertEqual(dummy in w, True)
1✔
52
        w.remove(dummy)
1✔
53
        self.assertEqual(dummy in w, False)
1✔
54

55
    def test_clear(self):
1✔
56
        from transaction.weakset import WeakSet
1✔
57
        w = WeakSet()
1✔
58
        dummy = Dummy()
1✔
59
        w.add(dummy)
1✔
60
        dummy2 = Dummy()
1✔
61
        w.add(dummy2)
1✔
62
        self.assertEqual(dummy in w, True)
1✔
63
        self.assertEqual(dummy2 in w, True)
1✔
64
        w.clear()
1✔
65
        self.assertEqual(dummy in w, False)
1✔
66
        self.assertEqual(dummy2 in w, False)
1✔
67

68
    def test_as_weakref_list(self):
1✔
69
        import gc
1✔
70

71
        from transaction.weakset import WeakSet
1✔
72
        w = WeakSet()
1✔
73
        dummy = Dummy()
1✔
74
        dummy2 = Dummy()
1✔
75
        dummy3 = Dummy()
1✔
76
        w.add(dummy)
1✔
77
        w.add(dummy2)
1✔
78
        w.add(dummy3)
1✔
79
        del dummy3
1✔
80
        gc.collect()
1✔
81
        refs = w.as_weakref_list()
1✔
82
        self.assertIsInstance(refs, list)
1✔
83
        L = [x() for x in refs]
1✔
84
        # L is a list, but it does not have a guaranteed order.
85
        self.assertTrue(list, type(L))
1✔
86
        self.assertEqual(set(L), {dummy, dummy2})
1✔
87

88
    def test_map(self):
1✔
89
        from transaction.weakset import WeakSet
1✔
90
        w = WeakSet()
1✔
91
        dummy = Dummy()
1✔
92
        dummy2 = Dummy()
1✔
93
        dummy3 = Dummy()
1✔
94
        w.add(dummy)
1✔
95
        w.add(dummy2)
1✔
96
        w.add(dummy3)
1✔
97

98
        def poker(x):
1✔
99
            x.poked = 1
1✔
100
        w.map(poker)
1✔
101
        for thing in dummy, dummy2, dummy3:
1✔
102
            self.assertEqual(thing.poked, 1)
1✔
103

104
    def test_map_w_gced_element(self):
1✔
105
        import gc
1✔
106

107
        from transaction.weakset import WeakSet
1✔
108
        w = WeakSet()
1✔
109
        dummy = Dummy()
1✔
110
        dummy2 = Dummy()
1✔
111
        dummy3 = [Dummy()]
1✔
112
        w.add(dummy)
1✔
113
        w.add(dummy2)
1✔
114
        w.add(dummy3[0])
1✔
115

116
        _orig = w.as_weakref_list
1✔
117

118
        def _as_weakref_list():
1✔
119
            # simulate race condition during iteration of list
120
            # object is collected after being iterated.
121
            result = _orig()
1✔
122
            del dummy3[:]
1✔
123
            gc.collect()
1✔
124
            return result
1✔
125
        w.as_weakref_list = _as_weakref_list
1✔
126

127
        def poker(x):
1✔
128
            x.poked = 1
1✔
129
        w.map(poker)
1✔
130
        for thing in dummy, dummy2:
1✔
131
            self.assertEqual(thing.poked, 1)
1✔
132

133

134
class Dummy:
1✔
135
    pass
1✔
136

137

138
def test_suite():
1✔
139
    return unittest.defaultTestLoader.loadTestsFromTestCase(WeakSetTests)
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc