Skip to content

Commit

Permalink
InvocationResult, send test (#78)
Browse files Browse the repository at this point in the history
  • Loading branch information
mandrewcito authored Feb 27, 2022
1 parent 22a8168 commit 4f7bab7
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 4 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ username, message - parameters of signalrmethod
```python
hub_connection.send("SendMessage", [username, message])
```

## Sending messages with callback
SendMessage - signalr method
username, message - parameters of signalrmethod
Expand All @@ -238,6 +239,7 @@ username, message - parameters of signalrmethod
if not send_callback_received.acquire(timeout=1):
raise ValueError("CALLBACK NOT RECEIVED")
```

## Requesting streaming (Server to client)
```python
hub_connection.stream(
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name="signalrcore",
version="0.9.2",
version="0.9.3",
author="mandrewcito",
author_email="anbaalo@gmail.com",
description="A Python SignalR Core client(json and messagepack), with invocation auth and two way streaming. Compatible with azure / serverless functions. Also with automatic reconnect and manually reconnect.",
Expand Down
18 changes: 16 additions & 2 deletions signalrcore/hub/base_hub_connection.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from operator import inv
import websocket
import threading
import requests
Expand All @@ -19,6 +20,11 @@
from ..subject import Subject
from ..messages.invocation_message import InvocationMessage

class InvocationResult(object):
def __init__(self, invocation_id) -> None:
self.invocation_id = invocation_id
self.message = None

class BaseHubConnection(object):
def __init__(
self,
Expand Down Expand Up @@ -90,7 +96,7 @@ def on(self, event, callback_function: Callable):
self.logger.debug("Handler registered started {0}".format(event))
self.handlers.append((event, callback_function))

def send(self, method, arguments, on_invocation=None, invocation_id=str(uuid.uuid4())):
def send(self, method, arguments, on_invocation=None, invocation_id=str(uuid.uuid4())) -> InvocationResult:
"""Sends a message
Args:
Expand All @@ -113,6 +119,8 @@ def send(self, method, arguments, on_invocation=None, invocation_id=str(uuid.uui
if type(arguments) is not list and type(arguments) is not Subject:
raise TypeError("Arguments of a message must be a list or subject")

result = InvocationResult(invocation_id)

if type(arguments) is list:
message = InvocationMessage(
invocation_id,
Expand All @@ -127,11 +135,17 @@ def send(self, method, arguments, on_invocation=None, invocation_id=str(uuid.uui
on_invocation))

self.transport.send(message)

result.message = message

if type(arguments) is Subject:
arguments.connection = self
arguments.target = method
arguments.start()
result.invocation_id = arguments.invocation_id
result.message = arguments


return result


def on_message(self, messages):
Expand Down
11 changes: 10 additions & 1 deletion test/send_test.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import re
import unittest
import logging
import time
Expand Down Expand Up @@ -103,10 +104,18 @@ def test_send_with_callback(self):
self.received = False
_lock = threading.Lock()
_lock.acquire()
uid = str(uuid.uuid4())

def release(m):
self.assertTrue(m.invocation_id, uid)
_lock.release()

self.connection.send(
"SendMessage",
[self.username, self.message],
lambda m: _lock.release())
release,
invocation_id=uid)

self.assertTrue(_lock.acquire(timeout=10))
del _lock

Expand Down

0 comments on commit 4f7bab7

Please sign in to comment.