aboutsummaryrefslogtreecommitdiffhomepage
path: root/libs/test/send_test.py
blob: cff9eb5b5803e84d09b09bc3e603596397b27a40 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import os
import re
import unittest
import logging
import time
import uuid
import threading

from signalrcore.hub_connection_builder import HubConnectionBuilder
from signalrcore.hub.errors import HubError, HubConnectionError
from test.base_test_case import BaseTestCase, Urls

class TestSendException(BaseTestCase):
    def receive_message(self, _):
        raise Exception()

    def setUp(self):
        self.connection = self.get_connection()
        self.connection.start()
        while not self.connected:
            time.sleep(0.1)
    
    def test_send_exception(self):
        _lock = threading.Lock()
        _lock.acquire()
        self.connection.send("SendMessage", ["user", "msg"])
        del _lock

    def test_hub_error(self):
        _lock = threading.Lock()

        self.assertTrue(_lock.acquire(timeout=10))     
        self.connection.on_error(lambda _: _lock.release())

        def on_message(_):
            _lock.release()
            self.assertTrue(_lock.acquire(timeout=10)) 

        self.connection.on("ThrowExceptionCall", on_message)
        self.connection.send("ThrowException", ["msg"])

class TestSendExceptionMsgPack(TestSendException):
    def setUp(self):
        self.connection = self.get_connection(msgpack=True)
        self.connection.start()
        while not self.connected:
            time.sleep(0.1)
        
class TestSendWarning(BaseTestCase):
    def setUp(self):
        self.connection = self.get_connection()
        self.connection.start()
        while not self.connected:
            time.sleep(0.1)
    
    def test_send_warning(self):
        _lock = threading.Lock()
        _lock.acquire()
        self.connection.send("SendMessage", ["user", "msg"], lambda m: _lock.release())
        self.assertTrue(_lock.acquire(timeout=10))
        del _lock

class TestSendWarningMsgPack(TestSendWarning):
    def setUp(self):
        self.connection = super().get_connection(msgpack=True)
        self.connection.start()
        while not self.connected:
            time.sleep(0.1)
        

class TestSendMethod(BaseTestCase):
    received = False
    message = None
    def setUp(self):
        self.connection = self.get_connection()
        self.connection.on("ReceiveMessage", self.receive_message)
        self.connection.start()
        while not self.connected:
            time.sleep(0.1)

    def receive_message(self, args):
        self.assertEqual(args[1], self.message)
        self.received = True


    def test_send_bad_args(self):
        class A():
            pass

        self.assertRaises(TypeError, lambda : self.connection.send("SendMessage", A()))

    def test_send(self):
        self.message = "new message {0}".format(uuid.uuid4())
        self.username = "mandrewcito"
        self.received = False
        self.connection.send("SendMessage", [self.username, self.message])
        while not self.received:
            time.sleep(0.1)
        self.assertTrue(self.received)

    def test_send_with_callback(self):
        self.message = "new message {0}".format(uuid.uuid4())
        self.username = "mandrewcito"
        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],
            release,
            invocation_id=uid)

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

class TestSendNoSslMethod(TestSendMethod):
    server_url = Urls.server_url_no_ssl

class TestSendMethodMsgPack(TestSendMethod):
    def setUp(self):
        self.connection = super().get_connection(msgpack=True)
        self.connection.on("ReceiveMessage", super().receive_message)
        self.connection.start()
        while not self.connected:
            time.sleep(0.1)

class TestSendNoSslMethodMsgPack(TestSendMethodMsgPack):
    server_url = Urls.server_url_no_ssl    

class TestSendErrorMethod(BaseTestCase):
    received = False
    message = None
    def setUp(self):
        self.connection = self.get_connection()
        self.connection.on("ReceiveMessage", self.receive_message)

    def receive_message(self, args):
        self.assertEqual(args[1], self.message)
        self.received = True


    def test_send_with_error(self):
        self.message = "new message {0}".format(uuid.uuid4())
        self.username = "mandrewcito"

        self.assertRaises(HubConnectionError,lambda : self.connection.send("SendMessage", [self.username, self.message]))
        
        self.connection.start()
        while not self.connected:
            time.sleep(0.1)

        self.received = False
        self.connection.send("SendMessage", [self.username, self.message])        
        t0 = time.time()
        while not self.received:
            time.sleep(0.1)
        self.assertTrue(self.received)


class TestSendErrorNoSslMethod(TestSendErrorMethod):
    server_url = Urls.server_url_no_ssl


class TestSendErrorMethodMsgPack(TestSendErrorMethod):
    def get_connection(self):
        return super().get_connection(msgpack=True)

class TestSendErrorNoSslMethodMsgPack(TestSendErrorNoSslMethod):
    def get_connection(self):
        return super().get_connection(msgpack=True)