Skip to content

Commit 78cfc94

Browse files
authored
Merge pull request #39 from didx-xyz/bug/add_listener
fixed listener bug and added tests version bump to 0.2.6
2 parents c2ee16e + 53d2322 commit 78cfc94

File tree

3 files changed

+100
-9
lines changed

3 files changed

+100
-9
lines changed

aries_cloudcontroller/aries_controller_base.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,22 @@ def register_listeners(self, listeners, defaults=True):
148148
except Exception as exc:
149149
logger.warning(f"Register webhooks listeners failed! {exc!r} occurred.")
150150

151+
def is_subscribed(self, listener):
152+
"""Check if listener is subscribed
153+
154+
Args:
155+
----
156+
listener : dict
157+
A dictionary comprised of "handler": handler (fct) and
158+
"topic":"topicname" key-value pairs
159+
"""
160+
try:
161+
pub_topic_path = listener["topic"]
162+
pub_hanlder = listener["handler"]
163+
return pub.isSubscribed(pub_hanlder, pub_topic_path)
164+
except Exception as exc:
165+
logger.warning(f"Unable to check if listener subscribed {exc!r} occurred.")
166+
151167
def add_listener(self, listener):
152168
"""Subscribe to a listeners for a topic
153169
@@ -159,7 +175,7 @@ def add_listener(self, listener):
159175
"""
160176
try:
161177
pub_topic_path = listener["topic"]
162-
logger.INFO("Subscribing too: " + pub_topic_path)
178+
logger.info("Subscribing too: " + pub_topic_path)
163179
pub.subscribe(listener["handler"], pub_topic_path)
164180
logger.debug("Lister added for topic : ", pub_topic_path)
165181
except Exception as exc:

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def parse_requirements(filename):
2222
if __name__ == "__main__":
2323
setup(
2424
name=PACKAGE_NAME,
25-
version="0.2.4",
25+
version="0.2.6",
2626
description="A simple python package for controlling an aries agent through the admin-api interface",
2727
long_description=long_description,
2828
long_description_content_type="text/markdown",

tests/test_aries_controller_base.py

Lines changed: 82 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -101,23 +101,98 @@ async def test_remove_api_key(self):
101101

102102
await ac.terminate()
103103

104-
# TODO create mock for pubsub listening webhooks
105-
# Maybe this makes more sense in aries_controller
104+
# TODO Maybe this makes more sense in aries_controller
106105
@pytest.mark.asyncio
107-
async def test_register_listners(self):
108-
pass
106+
async def test_register_listeners(self):
107+
ac = AriesAgentControllerBase(admin_url="0.0.0.0")
108+
109+
listeners = []
110+
# Receive connection messages
111+
def connections_handler(payload):
112+
LOGGER.info("Connections Handler")
113+
114+
connection_listener = {"handler": connections_handler, "topic": "connections"}
115+
116+
listeners.append(connection_listener)
117+
118+
def issuer_handler(payload):
119+
LOGGER.info("Issuer handler")
120+
121+
issuer_listener = {"topic": "issue_credential", "handler": issuer_handler}
122+
123+
listeners.append(issuer_listener)
124+
125+
ac.register_listeners(listeners)
126+
127+
for listener in listeners:
128+
assert ac.is_subscribed(listener)
129+
130+
await ac.terminate()
109131

110132
@pytest.mark.asyncio
111133
async def test_add_listener(self):
112-
pass
134+
ac = AriesAgentControllerBase(admin_url="0.0.0.0")
135+
136+
# Receive connection messages
137+
def connections_handler(payload):
138+
print("Connections Handler")
139+
140+
connection_listener = {"handler": connections_handler, "topic": "connections"}
141+
142+
ac.add_listener(connection_listener)
143+
144+
assert ac.is_subscribed(connection_listener)
145+
await ac.terminate()
113146

114147
@pytest.mark.asyncio
115148
async def test_remove_listener(self):
116-
pass
149+
ac = AriesAgentControllerBase(admin_url="0.0.0.0")
150+
151+
# Receive connection messages
152+
def connections_handler(payload):
153+
print("Connections Handler")
154+
155+
connection_listener = {"handler": connections_handler, "topic": "connections"}
156+
157+
ac.add_listener(connection_listener)
158+
159+
ac.remove_listener(connection_listener)
160+
161+
assert not ac.is_subscribed(connection_listener)
162+
await ac.terminate()
117163

118164
@pytest.mark.asyncio
119165
async def test_remove_all_listeners(self):
120-
pass
166+
ac = AriesAgentControllerBase(admin_url="0.0.0.0")
167+
168+
listeners = []
169+
170+
# Receive connection messages
171+
def connections_handler(payload):
172+
print("Connections Handler")
173+
174+
connection_listener = {"handler": connections_handler, "topic": "connections"}
175+
176+
listeners.append(connection_listener)
177+
178+
def issuer_handler(payload):
179+
print("Issuer handler")
180+
181+
issuer_listener = {"topic": "issue_credential", "handler": issuer_handler}
182+
183+
listeners.append(issuer_listener)
184+
185+
ac.register_listeners(listeners)
186+
187+
ac.remove_all_listeners("issue_credential")
188+
189+
assert not ac.is_subscribed(issuer_listener)
190+
assert ac.is_subscribed(connection_listener)
191+
192+
ac.remove_all_listeners()
193+
194+
assert not ac.is_subscribed(connection_listener)
195+
await ac.terminate()
121196

122197
@pytest.mark.asyncio
123198
async def test_listen_webhooks(self):

0 commit comments

Comments
 (0)