Skip to content

Commit c04c8b2

Browse files
committed
add unit test for stream generator
1 parent c7d7034 commit c04c8b2

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# Copyright 2020 The StackStorm Authors.
2+
# Copyright 2019 Extreme Networks, Inc.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
import mock
17+
import unittest2
18+
from st2common.stream import listener
19+
20+
21+
class MockBody(object):
22+
23+
def __init__(self, id):
24+
self.id = id
25+
self.status = "succeeded"
26+
27+
28+
INCLUDE = "test"
29+
END_EVENT = "test_end_event"
30+
END_ID = "test_end_id"
31+
EVENTS = [(INCLUDE, MockBody("notend")), (END_EVENT, MockBody(END_ID))]
32+
33+
34+
class MockQueue():
35+
36+
def __init__(self):
37+
self.items = EVENTS
38+
39+
def get(self, *args, **kwargs):
40+
if len(self.items) > 0:
41+
return self.items.pop(0)
42+
return None
43+
44+
def put(self, event):
45+
self.items.append(event)
46+
47+
48+
class MockListener(listener.BaseListener):
49+
50+
def __init__(self, *args, **kwargs):
51+
super(MockListener, self).__init__(*args, **kwargs)
52+
53+
def get_consumers(self, consumer, channel):
54+
pass
55+
56+
57+
class TestStream(unittest2.TestCase):
58+
59+
@mock.patch('st2common.stream.listener.BaseListener._get_action_ref_for_body')
60+
@mock.patch('eventlet.Queue')
61+
def test_generator(self, mock_queue,
62+
get_action_ref_for_body):
63+
get_action_ref_for_body.return_value = None
64+
mock_queue.return_value = MockQueue()
65+
mock_consumer = MockListener(connection=None)
66+
mock_consumer._stopped = False
67+
app_iter = mock_consumer.generator(events=INCLUDE,
68+
end_event=END_EVENT,
69+
end_statuses=["succeeded"],
70+
end_execution_id=END_ID)
71+
events = EVENTS.append('')
72+
for index, val in enumerate(app_iter):
73+
self.assertEquals(val, events[index])
74+
75+
76+
if __name__ == "__main__":
77+
unittest2.main()

0 commit comments

Comments
 (0)