-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_streaming.py
219 lines (188 loc) · 7.9 KB
/
test_streaming.py
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
from hkube_python_wrapper.communication.streaming.StreamingManager import StreamingManager
from hkube_python_wrapper.communication.streaming.MessageListener import MessageListener
from hkube_python_wrapper.communication.streaming.MessageProducer import MessageProducer
import time
parsedFlows = {
'analyze': [{'source': 'A', 'next': ['B']}, {'source': 'B', 'next': ['C']}, {'source': 'C', 'next': ['D']}],
'master': [{'source': 'B', 'next': ['A', 'C']}, {'source': 'C', 'next': ['D']}, {'source': 'D', 'next': ['E']}]
}
def test_streaming_flow():
messages = []
def onMessage(msg, origin):
messages.append(msg)
def statsInvoked(args):
print('stats')
port = 9340
nodeName = 'B'
parents = [{'nodeName': 'A', 'address': {'host': '127.0.0.1', 'port': port}, 'type': 'Add'}]
producer_config = {'port': port, 'messagesMemoryBuff': 5000, 'encoding': 'msgpack', 'statisticsInterval': 1}
listen_config = {'encoding': 'msgpack'}
streamingManager = StreamingManager()
streamingManager.setParsedFlows(parsedFlows, 'analyze')
streamingManager.setupStreamingProducer(statsInvoked, producer_config, [nodeName], 'A')
streamingManager.setupStreamingListeners(listen_config, parents, nodeName)
streamingManager.registerInputListener(onMessage)
streamingManager.startMessageListening()
streamingManager.sendMessage({'msg': '1'})
streamingManager.sendMessage({'msg': '2'})
streamingManager.sendMessage({'msg': '3'})
streamingManager.sendMessage({'msg': '4'})
time.sleep(1)
assert len(messages) == 4
assert messages[0] == {'msg': '1'}
assert messages[1] == {'msg': '2'}
assert messages[2] == {'msg': '3'}
assert messages[3] == {'msg': '4'}
streamingManager.stopStreaming()
def test_streaming_manager():
resultsAtB = {}
resultsAtC = {}
def onMessageAtC(msg, origin):
resultsAtC['data'] = msg
resultsAtC['origin'] = origin
def onMessageAtB(msg, origin):
resultsAtB['data'] = msg
resultsAtB['origin'] = origin
streamingManagerB.sendMessage('msg from B')
def statsInvoked(args):
print('stats')
port1 = 9341
port2 = 9342
parents1 = [{'nodeName': 'A', 'address': {'host': '127.0.0.1', 'port': port1}, 'type': 'Add'}]
parents2 = [{'nodeName': 'B', 'address': {'host': '127.0.0.1', 'port': port2}, 'type': 'Add'}]
producer_configA = {'port': port1, 'messagesMemoryBuff': 5000, 'encoding': 'msgpack', 'statisticsInterval': 1}
producer_configB = {'port': port2, 'messagesMemoryBuff': 5000, 'encoding': 'msgpack', 'statisticsInterval': 1}
listen_config = {'encoding': 'msgpack'}
streamingManagerA = StreamingManager()
streamingManagerA.setParsedFlows(parsedFlows, 'analyze')
streamingManagerA.setupStreamingProducer(statsInvoked, producer_configA, ['B'], 'A')
streamingManagerB = StreamingManager()
streamingManagerB.setParsedFlows(parsedFlows, 'analyze')
streamingManagerB.setupStreamingProducer(statsInvoked, producer_configB, ['C'], 'B')
streamingManagerB.setupStreamingListeners(listen_config, parents1, 'B')
streamingManagerB.registerInputListener(onMessageAtB)
streamingManagerB.startMessageListening()
streamingManagerC = StreamingManager()
streamingManagerC.setupStreamingListeners(listen_config, parents2, 'C')
streamingManagerC.registerInputListener(onMessageAtC)
streamingManagerC.startMessageListening()
streamingManagerA.sendMessage('msg from A')
time.sleep(1)
assert resultsAtB['data'] == 'msg from A'
assert resultsAtB['origin'] == 'A'
assert resultsAtC['data'] == 'msg from B'
assert resultsAtC['origin'] == 'B'
streamingManagerA.stopStreaming()
streamingManagerB.stopStreaming()
def xtest_messaging():
producer_config = {'port': 9536, 'messagesMemoryBuff': 5000, 'encoding': 'msgpack', 'statisticsInterval': 1}
listenr_config = {'remoteAddress': 'tcp://localhost:9536', 'encoding': 'msgpack', 'messageOriginNodeName': 'b'}
asserts = {}
asserts['responses'] = 0
messageProducer = MessageProducer(producer_config, ['a'], 'b')
def onStatistics(statistics):
asserts['stats'] = statistics
asserts['responses'] = int(statistics[0]['responses'])
messageProducer.registerStatisticsListener(onStatistics)
time.sleep(3)
def onMessage(envelope, msg, origin):
if (type(msg) == type(dict())):
asserts['field1'] = msg['field1']
time.sleep(0.1)
messageProducer.start()
env = [{
"source": "b",
"next": [
"a"
]
}, {
"source": "a",
"next": [
"c"
]
}]
messageProducer.produce(env, {'field1': 'value1'})
messageProducer.produce(env, {'field1': 'value1'})
messageProducer.produce(env, {'field1': 'value1'})
time.sleep(2)
assert asserts['stats'][0]['queueSize'] == 3
assert asserts['stats'][0]['sent'] == 0
messageListener = MessageListener(listenr_config, receiverNode='a')
messageListener.registerMessageListener(onMessage)
for _ in range(1,5):
if (asserts.get('field1')):
break
time.sleep(1)
time.sleep(4)
assert asserts['field1'] == 'value1'
assert asserts['stats'][0]['queueSize'] == 0
assert asserts['stats'][0]['sent'] == 3
assert asserts['responses'] == 3
def getHello(envelope, msg, origin):
if (msg == b'Hello'):
asserts['gotHello'] = True
asserts['envelope'] = envelope
messageListener.registerMessageListener(getHello)
messageProducer.produce(env, b'Hello')
time.sleep(3)
assert asserts['gotHello']
assert len(asserts['envelope']) == 1
assert asserts['envelope'][0]['source'] == 'a'
def xtest_messaging_split():
producer_config = {'port': 9536, 'messagesMemoryBuff': 5000, 'encoding': 'msgpack', 'statisticsInterval': 1}
listenr_config_c = {'remoteAddress': 'tcp://localhost:9536', 'encoding': 'msgpack', 'messageOriginNodeName': 'c'}
asserts = {}
asserts['responses'] = 0
messageProducer = MessageProducer(producer_config, ['a','b'], 'c')
def onStatistics(statistics):
asserts['stats'] = statistics
asserts['responses'] = int(statistics[0]['responses'])
messageProducer.registerStatisticsListener(onStatistics)
time.sleep(3)
def onMessage(envelope, msg, origin):
# pylint: disable=unused-argument
if (type(msg) == type(dict())):
asserts['field1'] = msg['field1']
time.sleep(0.1)
messageProducer.start()
env = [{
"source": "c",
"next": [
"a"
]
}]
env2 = [{
"source": "c",
"next": [
"b"
]
}]
messageProducer.produce(env, {'field1': 'value1'})
messageProducer.produce(env2, {'field1': 'value2'})
messageProducer.produce(env, {'field1': 'value1'})
messageProducer.produce(env2, {'field1': 'value2'})
messageProducer.produce(env2, {'field1': 'value2'})
messageProducer.produce(env, {'field1': 'value1'})
time.sleep(2)
assert asserts['stats'][0]['queueSize'] == 3
assert asserts['stats'][0]['sent'] == 0
messageListener = MessageListener(listenr_config_c, receiverNode='a')
messageListener.registerMessageListener(onMessage)
for _ in range(1,5):
if ( asserts.get('field1')):
break
time.sleep(1)
time.sleep(2)
assert asserts['field1'] == 'value1'
assert asserts['stats'][0]['queueSize'] == 0
assert asserts['stats'][0]['sent'] == 3
assert asserts['stats'][1]['queueSize'] == 3
assert asserts['stats'][1]['sent'] == 0
assert asserts['responses'] == 3
messageListener2 = MessageListener(listenr_config_c, receiverNode='b')
messageListener2.registerMessageListener(onMessage)
messageListener2.start()
time.sleep(20)
assert asserts['stats'][1]['queueSize'] == 0
assert asserts['stats'][1]['sent'] == 3
assert len(messageProducer.adapter.messageQueue.queue) == 0