forked from kljohann/genpybind
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeep-alive_test.py
186 lines (138 loc) · 4.98 KB
/
keep-alive_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# SPDX-FileCopyrightText: 2024 Johann Klähn <[email protected]>
#
# SPDX-License-Identifier: MIT
from dataclasses import dataclass
import keep_alive as m
import pytest
@dataclass
class Snapshot:
created: int
destroyed: int
alive: int
@classmethod
def create(cls, src):
return cls(created=src.created, destroyed=src.destroyed, alive=src.alive)
@pytest.mark.parametrize("thing", [m.Resource, m.Container])
def test_thing_counts_number_of_instances(thing):
before = Snapshot.create(thing)
thing()
now = Snapshot.create(thing)
assert before.alive == 0
assert now.alive == 0
assert now.created == before.created + 1
assert now.destroyed == before.destroyed + 1
def test_constructor_can_keep_resource_alive():
assert m.Container.alive == 0
before = Snapshot.create(m.Resource)
container = m.Container(m.Resource())
now = Snapshot.create(m.Resource)
assert m.Container.alive == 1
assert before.alive == 0
assert now.alive == 1
assert now.created == before.created + 1
assert now.destroyed == before.destroyed
del container
now = Snapshot.create(m.Resource)
assert m.Container.alive == 0
assert before.alive == 0
assert now.alive == 0
assert now.created == before.created + 1
assert now.destroyed == before.destroyed + 1
def test_unannotated_sink_method_has_no_effect_on_lifetime():
before = Snapshot.create(m.Resource)
m.Resource()
now = Snapshot.create(m.Resource)
assert before.alive == 0
assert now.alive == 0
assert now.created == before.created + 1
assert now.destroyed == before.destroyed + 1
container = m.Container()
before = Snapshot.create(m.Resource)
container.unannotated_sink(m.Resource())
now = Snapshot.create(m.Resource)
assert before.alive == 0
assert now.alive == 0
assert now.created == before.created + 1
assert now.destroyed == before.destroyed + 1
def test_sink_method_can_keep_resource_alive():
assert m.Container.alive == 0
container = m.Container()
assert m.Container.alive == 1
before = Snapshot.create(m.Resource)
container.keep_alive_sink(m.Resource())
now = Snapshot.create(m.Resource)
assert before.alive == 0
assert now.alive == 1
assert now.created == before.created + 1
assert now.destroyed == before.destroyed
del container
now = Snapshot.create(m.Resource)
assert m.Container.alive == 0
assert before.alive == 0
assert now.alive == 0
assert now.created == before.created + 1
assert now.destroyed == before.destroyed + 1
def test_unannotated_source_method_has_no_effect_on_lifetime():
container = m.Container()
before = Snapshot.create(m.Resource)
container.unannotated_source()
now = Snapshot.create(m.Resource)
assert before.alive == 0
assert now.alive == 0
assert now.created == before.created + 1
assert now.destroyed == before.destroyed + 1
def test_source_method_can_keep_resource_alive():
assert m.Container.alive == 0
container = m.Container()
assert m.Container.alive == 1
before = Snapshot.create(m.Resource)
container.keep_alive_source()
now = Snapshot.create(m.Resource)
assert before.alive == 0
assert now.alive == 1
assert now.created == before.created + 1
assert now.destroyed == before.destroyed
del container
now = Snapshot.create(m.Resource)
assert m.Container.alive == 0
assert before.alive == 0
assert now.alive == 0
assert now.created == before.created + 1
assert now.destroyed == before.destroyed + 1
def test_source_method_can_keep_container_alive():
container_before = Snapshot.create(m.Container)
container = m.Container()
container_alive = Snapshot.create(m.Container)
assert container_alive != container_before
assert container_before.alive == 0
assert container_alive.alive == 1
assert m.Resource.alive == 0
resource = container.reverse_keep_alive_source()
assert m.Resource.alive == 1
del container
container_now = Snapshot.create(m.Container)
assert container_now == container_alive
del resource
container_now = Snapshot.create(m.Container)
assert m.Resource.alive == 0
assert container_now.alive == 0
assert container_now.created == container_before.created + 1
assert container_now.destroyed == container_before.destroyed + 1
def test_free_function_can_keep_resource_alive():
assert m.Container.alive == 0
container = m.Container()
assert m.Container.alive == 1
before = Snapshot.create(m.Resource)
m.link(container, m.Resource())
now = Snapshot.create(m.Resource)
assert before.alive == 0
assert now.alive == 1
assert now.created == before.created + 1
assert now.destroyed == before.destroyed
del container
now = Snapshot.create(m.Resource)
assert m.Container.alive == 0
assert before.alive == 0
assert now.alive == 0
assert now.created == before.created + 1
assert now.destroyed == before.destroyed + 1