Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests to verify autoproxy server object lifetime [currently failing] #93

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 100 additions & 0 deletions tests/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
Pyro - Python Remote Objects. Copyright by Irmen de Jong ([email protected]).
"""

from functools import lru_cache
import time
import threading
import serpent
Expand Down Expand Up @@ -185,6 +186,105 @@ def testDaemonConnectFail(self):
assert "rigged connection failure" in message


@lru_cache(maxsize=None)
class LiveObjects:
def __init__(self):
self.__objs: dict[int, str] = {}

def add(self, obj):
self.__objs[id(obj)] = str(obj)

def remove(self, obj):
self.__objs.pop(id(obj), None)

def clear(self):
self.__objs.clear()

def get_alive_names(self):
return self.__objs.values()


@Pyro5.server.expose
class Child:
def __init__(self, name: str):
self.__name = name
LiveObjects().add(self)

def __del__(self):
LiveObjects().remove(self)

def __repr__(self):
return self.__name

def ping(self):
return f"I am {self.__name}"


@Pyro5.server.expose
class Parent:
def __init__(self):
self.__name = "outer"
LiveObjects().add(self)

def __del__(self):
LiveObjects().remove(self)

def __repr__(self):
return self.__name

def ping(self):
return f"I am {self.__name}"

def createAutoproxyInstance(self):
i = Child(name="single instance")
self._pyroDaemon.register(i)
return i

def createAutoproxyGenerator(self):
ii = (Child(name=f"iterator instance {i}") for i in range(4))
for i in ii:
self._pyroDaemon.register(i)
yield i


class TestServerAutoproxyLifetime:
"""tests to verify that autoproxy server objects are released after use"""

def setup_method(self):
LiveObjects().clear()
self.daemon = Pyro5.server.Daemon(port=0)
uri = self.daemon.register(Parent, force=True)
self.objectUri = uri
self.daemonthread = DaemonLoopThread(self.daemon)
self.daemonthread.start()
self.daemonthread.running.wait()
time.sleep(0.05)

def teardown_method(self):
time.sleep(0.05)
if self.daemon is not None:
self.daemon.shutdown()
self.daemonthread.join()
assert not LiveObjects().get_alive_names(), "server autoproxy object(s) still alive"

def testSession(self):
with Pyro5.client.Proxy(self.objectUri) as p1, Pyro5.client.Proxy(self.objectUri) as p2:
assert "outer" in p1.ping()
assert "outer" in p2.ping()

@pytest.mark.xfail
def testAutoproxyInstance(self):
with Pyro5.client.Proxy(self.objectUri) as p:
inst = p.createAutoproxyInstance()
assert "single instance" in inst.ping()

@pytest.mark.xfail
def testAutoproxyGenerator(self):
with Pyro5.client.Proxy(self.objectUri) as p:
for inner in p.createAutoproxyGenerator():
assert "iterator instance" in inner.ping()


class TestServerOnce:
"""tests that are fine to run with just a single server type"""

Expand Down