Skip to content

Commit

Permalink
tests: add test_allocation_cleanup and test_core_foundation_types
Browse files Browse the repository at this point in the history
  • Loading branch information
doronz88 committed Feb 18, 2022
1 parent 7fea82c commit 4bd22c2
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/rpcclient/rpcclient/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,10 @@ def environ(self) -> typing.List[str]:
i += 1
return result

@property
def pid(self):
return int(self.symbols.getpid())

@contextlib.contextmanager
def safe_calloc(self, size: int):
with self.safe_malloc(size) as x:
Expand Down
43 changes: 43 additions & 0 deletions src/rpcclient/tests/test_allocation_cleanup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import gc


def test_allocate_file_fd_context_manager(client, tmp_path):
# make sure when the test starts, all previous Allocated references are freed
gc.collect()
fds_count = len(client.processes.get_fds(client.pid))
with client.fs.open(tmp_path / 'test', 'w'):
assert fds_count + 1 == len(client.processes.get_fds(client.pid))
assert fds_count == len(client.processes.get_fds(client.pid))


def test_allocate_file_fd_gc(client, tmp_path):
# make sure when the test starts, all previous Allocated references are freed
gc.collect()
fds_count = len(client.processes.get_fds(client.pid))

# create a new fd with zero references, so it should be free immediately
client.fs.open(tmp_path / 'test', 'w')

# make sure python's GC had a chance to free the newly created fd
gc.collect()
assert fds_count == len(client.processes.get_fds(client.pid))


def test_allocate_file_fd_explicit_del(client, tmp_path):
# make sure when the test starts, all previous Allocated references are freed
gc.collect()
fds_count = len(client.processes.get_fds(client.pid))
fd = client.fs.open(tmp_path / 'test', 'w')
assert fds_count + 1 == len(client.processes.get_fds(client.pid))
del fd
assert fds_count == len(client.processes.get_fds(client.pid))


def test_allocate_file_fd_explicit_deallocate(client, tmp_path):
# make sure when the test starts, all previous Allocated references are freed
gc.collect()
fds_count = len(client.processes.get_fds(client.pid))
fd = client.fs.open(tmp_path / 'test', 'w')
assert fds_count + 1 == len(client.processes.get_fds(client.pid))
fd.deallocate()
assert fds_count >= len(client.processes.get_fds(client.pid))
12 changes: 12 additions & 0 deletions src/rpcclient/tests/test_core_foundation_types.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import pytest


@pytest.mark.parametrize('data', ['string',
b'bytes',
123,
0.1,
[0, 1, 'abc'],
{'key': 'value'},
[{'key': 'value'}, [1, 2]]])
def test_serialization(client, data):
assert client.cf(data).py == data

0 comments on commit 4bd22c2

Please sign in to comment.