|
| 1 | +import pytest |
| 2 | +from contextlib import contextmanager |
| 3 | + |
| 4 | +from godot import bindings |
| 5 | +from godot.bindings import OS |
| 6 | + |
| 7 | + |
| 8 | +@contextmanager |
| 9 | +def check_memory_leak(): |
| 10 | + dynamic_mem_start = OS.get_dynamic_memory_usage() |
| 11 | + static_mem_start = OS.get_static_memory_usage() |
| 12 | + |
| 13 | + yield |
| 14 | + |
| 15 | + # TODO: force garbage collection on pypy |
| 16 | + |
| 17 | + static_mem_end = OS.get_static_memory_usage() |
| 18 | + dynamic_mem_end = OS.get_dynamic_memory_usage() |
| 19 | + |
| 20 | + static_leak = static_mem_end - static_mem_start |
| 21 | + dynamic_leak = dynamic_mem_end - dynamic_mem_start |
| 22 | + assert not static_leak |
| 23 | + assert not dynamic_leak |
| 24 | + |
| 25 | + |
| 26 | +def test_base_static_memory_leak_check(): |
| 27 | + # Make sure calling this monitoring function doesn't cause a memory |
| 28 | + # leak on it own |
| 29 | + static_mem = OS.get_static_memory_usage() |
| 30 | + static_mem2 = OS.get_static_memory_usage() |
| 31 | + |
| 32 | + static_leak = static_mem2 - static_mem |
| 33 | + assert not static_leak |
| 34 | + |
| 35 | + |
| 36 | +def test_base_dynamic_memory_leak_check(): |
| 37 | + # Make sure calling this monitoring function doesn't cause a memory |
| 38 | + # leak on it own |
| 39 | + dynamic_mem = OS.get_dynamic_memory_usage() |
| 40 | + dynamic_mem2 = OS.get_dynamic_memory_usage() |
| 41 | + |
| 42 | + dynamic_leak = dynamic_mem2 - dynamic_mem |
| 43 | + assert not dynamic_leak |
| 44 | + |
| 45 | + |
| 46 | +def test_base_builtin_memory_leak(): |
| 47 | + with check_memory_leak(): |
| 48 | + |
| 49 | + v = bindings.Vector3() |
| 50 | + v.x = 42 |
| 51 | + v.y |
| 52 | + |
| 53 | + |
| 54 | +def test_dictionary_memory_leak(): |
| 55 | + with check_memory_leak(): |
| 56 | + |
| 57 | + v = bindings.Dictionary() |
| 58 | + v['foo'] = 42 |
| 59 | + v.update({'a': 1, 'b': 2.0, 'c': 'three'}) |
| 60 | + v['foo'] |
| 61 | + [x for x in v] |
| 62 | + del v['a'] |
| 63 | + |
| 64 | + |
| 65 | +def test_array_memory_leak(): |
| 66 | + with check_memory_leak(): |
| 67 | + |
| 68 | + v = bindings.Array() |
| 69 | + v.append('x') |
| 70 | + v += [1, 2, 3] |
| 71 | + v[0] |
| 72 | + [x for x in v] |
| 73 | + |
| 74 | + |
| 75 | +def test_pool_int_array_memory_leak(): |
| 76 | + with check_memory_leak(): |
| 77 | + |
| 78 | + v = bindings.PoolIntArray() |
| 79 | + v.append(42) |
| 80 | + v.resize(1000) |
| 81 | + v.pop() |
| 82 | + |
| 83 | + |
| 84 | +def test_pool_string_array_memory_leak(): |
| 85 | + with check_memory_leak(): |
| 86 | + |
| 87 | + v = bindings.PoolStringArray() |
| 88 | + v.append("fooo") |
| 89 | + v.resize(1000) |
| 90 | + v.pop() |
| 91 | + |
| 92 | + |
| 93 | +def test_object_memory_leak(): |
| 94 | + with check_memory_leak(): |
| 95 | + |
| 96 | + v = bindings.Node() |
| 97 | + v.free() |
0 commit comments