Skip to content

Commit fe033e0

Browse files
committed
fixed listener bug and added tests
1 parent a140cf5 commit fe033e0

File tree

3 files changed

+122
-9
lines changed

3 files changed

+122
-9
lines changed

aries_cloudcontroller/aries_controller_base.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,25 @@ 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+
152+
def is_subscribed(self, listener):
153+
"""Check if listener is subscribed
154+
155+
Args:
156+
----
157+
listener : dict
158+
A dictionary comprised of "handler": handler (fct) and
159+
"topic":"topicname" key-value pairs
160+
"""
161+
try:
162+
pub_topic_path = listener["topic"]
163+
pub_hanlder = listener["handler"]
164+
return pub.isSubscribed(pub_hanlder, pub_topic_path)
165+
except Exception as exc:
166+
logger.warning(f"Unable to check if listener subscribed {exc!r} occurred.")
167+
168+
169+
151170
def add_listener(self, listener):
152171
"""Subscribe to a listeners for a topic
153172
@@ -159,7 +178,7 @@ def add_listener(self, listener):
159178
"""
160179
try:
161180
pub_topic_path = listener["topic"]
162-
logger.INFO("Subscribing too: " + pub_topic_path)
181+
logger.info("Subscribing too: " + pub_topic_path)
163182
pub.subscribe(listener["handler"], pub_topic_path)
164183
logger.debug("Lister added for topic : ", pub_topic_path)
165184
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.5",
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: 101 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -101,23 +101,117 @@ 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+
print("Connections Handler")
113+
114+
connection_listener = {
115+
"handler": connections_handler,
116+
"topic": "connections"
117+
}
118+
119+
listeners.append(connection_listener)
120+
121+
def issuer_handler(payload):
122+
print("Issuer handler")
123+
124+
issuer_listener = {
125+
"topic": "issue_credential",
126+
"handler": issuer_handler
127+
}
128+
129+
listeners.append(issuer_listener)
130+
131+
ac.register_listeners(listeners)
132+
133+
for listener in listeners:
134+
assert ac.is_subscribed(listener)
135+
136+
await ac.terminate()
137+
109138

110139
@pytest.mark.asyncio
111140
async def test_add_listener(self):
112-
pass
141+
ac = AriesAgentControllerBase(admin_url="0.0.0.0")
142+
143+
# Receive connection messages
144+
def connections_handler(payload):
145+
print("Connections Handler")
146+
147+
connection_listener = {
148+
"handler": connections_handler,
149+
"topic": "connections"
150+
}
151+
152+
ac.add_listener(connection_listener)
153+
154+
assert ac.is_subscribed(connection_listener)
155+
await ac.terminate()
113156

114157
@pytest.mark.asyncio
115158
async def test_remove_listener(self):
116-
pass
159+
ac = AriesAgentControllerBase(admin_url="0.0.0.0")
160+
161+
# Receive connection messages
162+
def connections_handler(payload):
163+
print("Connections Handler")
164+
165+
connection_listener = {
166+
"handler": connections_handler,
167+
"topic": "connections"
168+
}
169+
170+
ac.add_listener(connection_listener)
171+
172+
ac.remove_listener(connection_listener)
173+
174+
assert not ac.is_subscribed(connection_listener)
175+
await ac.terminate()
117176

118177
@pytest.mark.asyncio
119178
async def test_remove_all_listeners(self):
120-
pass
179+
ac = AriesAgentControllerBase(admin_url="0.0.0.0")
180+
181+
listeners = []
182+
183+
# Receive connection messages
184+
def connections_handler(payload):
185+
print("Connections Handler")
186+
187+
connection_listener = {
188+
"handler": connections_handler,
189+
"topic": "connections"
190+
}
191+
192+
listeners.append(connection_listener)
193+
194+
def issuer_handler(payload):
195+
print("Issuer handler")
196+
197+
issuer_listener = {
198+
"topic": "issue_credential",
199+
"handler": issuer_handler
200+
}
201+
202+
listeners.append(issuer_listener)
203+
204+
ac.register_listeners(listeners)
205+
206+
ac.remove_all_listeners("issue_credential")
207+
208+
assert not ac.is_subscribed(issuer_listener)
209+
assert ac.is_subscribed(connection_listener)
210+
211+
ac.remove_all_listeners()
212+
213+
assert not ac.is_subscribed(connection_listener)
214+
await ac.terminate()
121215

122216
@pytest.mark.asyncio
123217
async def test_listen_webhooks(self):

0 commit comments

Comments
 (0)