|
| 1 | +# Copyright 2021 The StackStorm Authors. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from __future__ import absolute_import |
| 16 | + |
| 17 | +import random |
| 18 | +import eventlet |
| 19 | + |
| 20 | +from kombu import Exchange |
| 21 | +from kombu import Queue |
| 22 | +from unittest2 import TestCase |
| 23 | + |
| 24 | +from st2common.transport.consumers import ActionsQueueConsumer |
| 25 | +from st2common.transport.publishers import PoolPublisher |
| 26 | +from st2common.transport import utils as transport_utils |
| 27 | +from st2common.models.db.liveaction import LiveActionDB |
| 28 | + |
| 29 | +__all__ = ["ActionsQueueConsumerTestCase"] |
| 30 | + |
| 31 | + |
| 32 | +class ActionsQueueConsumerTestCase(TestCase): |
| 33 | + message_count = 0 |
| 34 | + message_type = LiveActionDB |
| 35 | + |
| 36 | + def test_stop_consumption_on_shutdown(self): |
| 37 | + exchange = Exchange("st2.execution.test", type="topic") |
| 38 | + queue_name = "test-" + str(random.randint(1, 10000)) |
| 39 | + queue = Queue( |
| 40 | + name=queue_name, exchange=exchange, routing_key="#", auto_delete=True |
| 41 | + ) |
| 42 | + publisher = PoolPublisher() |
| 43 | + with transport_utils.get_connection() as connection: |
| 44 | + connection.connect() |
| 45 | + watcher = ActionsQueueConsumer( |
| 46 | + connection=connection, queues=queue, handler=self |
| 47 | + ) |
| 48 | + watcher_thread = eventlet.greenthread.spawn(watcher.run) |
| 49 | + |
| 50 | + # Give it some time to start up since we are publishing on a new queue |
| 51 | + eventlet.sleep(0.5) |
| 52 | + body = LiveActionDB( |
| 53 | + status="scheduled", action="core.local", action_is_workflow=False |
| 54 | + ) |
| 55 | + publisher.publish(payload=body, exchange=exchange) |
| 56 | + eventlet.sleep(0.2) |
| 57 | + self.assertEqual(self.message_count, 1) |
| 58 | + body = LiveActionDB( |
| 59 | + status="scheduled", action="core.local", action_is_workflow=True |
| 60 | + ) |
| 61 | + watcher.shutdown() |
| 62 | + eventlet.sleep(1) |
| 63 | + publisher.publish(payload=body, exchange=exchange) |
| 64 | + # Second published message won't be consumed. |
| 65 | + self.assertEqual(self.message_count, 1) |
| 66 | + watcher_thread.kill() |
| 67 | + |
| 68 | + def process(self, liveaction): |
| 69 | + self.message_count = self.message_count + 1 |
0 commit comments