2019-12-01 06:04:43 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
"""
|
|
|
|
proxy.py
|
|
|
|
~~~~~~~~
|
|
|
|
⚡⚡⚡ Fast, Lightweight, Pluggable, TLS interception capable proxy server focused on
|
|
|
|
Network monitoring, controls & Application development, testing, debugging.
|
|
|
|
|
|
|
|
:copyright: (c) 2013-present by Abhinav Singh and contributors.
|
|
|
|
:license: BSD, see LICENSE for more details.
|
|
|
|
"""
|
|
|
|
import os
|
2021-11-10 15:17:28 +00:00
|
|
|
import queue
|
2019-12-01 06:04:43 +00:00
|
|
|
import threading
|
2019-12-21 01:10:35 +00:00
|
|
|
import multiprocessing
|
2022-01-10 07:58:22 +00:00
|
|
|
from typing import Any, Dict
|
2019-12-01 06:04:43 +00:00
|
|
|
|
2022-01-10 07:58:22 +00:00
|
|
|
import unittest
|
2019-12-01 06:04:43 +00:00
|
|
|
from unittest import mock
|
|
|
|
|
2022-01-10 07:58:22 +00:00
|
|
|
from proxy.core.event import (
|
|
|
|
EventQueue, EventDispatcher, EventSubscriber, eventNames,
|
|
|
|
)
|
|
|
|
|
2019-12-01 06:04:43 +00:00
|
|
|
|
|
|
|
PUBLISHER_ID = threading.get_ident()
|
|
|
|
|
|
|
|
|
|
|
|
class TestEventSubscriber(unittest.TestCase):
|
|
|
|
|
2021-11-08 20:04:17 +00:00
|
|
|
def setUp(self) -> None:
|
|
|
|
self.manager = multiprocessing.Manager()
|
2021-11-10 15:17:28 +00:00
|
|
|
self.event_queue = EventQueue(self.manager.Queue())
|
2021-11-08 20:04:17 +00:00
|
|
|
|
|
|
|
def tearDown(self) -> None:
|
|
|
|
self.manager.shutdown()
|
|
|
|
|
2019-12-01 06:04:43 +00:00
|
|
|
@mock.patch('time.time')
|
|
|
|
def test_event_subscriber(self, mock_time: mock.Mock) -> None:
|
|
|
|
mock_time.return_value = 1234567
|
|
|
|
self.dispatcher_shutdown = threading.Event()
|
|
|
|
self.dispatcher = EventDispatcher(
|
|
|
|
shutdown=self.dispatcher_shutdown,
|
2021-11-04 11:28:36 +00:00
|
|
|
event_queue=self.event_queue,
|
|
|
|
)
|
2021-11-10 15:17:28 +00:00
|
|
|
self.subscriber = EventSubscriber(self.event_queue, self.callback)
|
|
|
|
self.subscriber.setup()
|
2019-12-01 06:04:43 +00:00
|
|
|
self.dispatcher.run_once()
|
|
|
|
|
|
|
|
self.event_queue.publish(
|
|
|
|
request_id='1234',
|
|
|
|
event_name=eventNames.WORK_STARTED,
|
|
|
|
event_payload={'hello': 'events'},
|
2023-03-14 14:30:07 +00:00
|
|
|
publisher_id=self.__class__.__qualname__,
|
2019-12-01 06:04:43 +00:00
|
|
|
)
|
|
|
|
self.dispatcher.run_once()
|
|
|
|
self.subscriber.unsubscribe()
|
|
|
|
self.dispatcher.run_once()
|
2021-11-10 15:17:28 +00:00
|
|
|
self.subscriber.shutdown(do_unsubscribe=False)
|
|
|
|
with self.assertRaises(queue.Empty):
|
|
|
|
self.dispatcher.run_once()
|
2019-12-01 06:04:43 +00:00
|
|
|
self.dispatcher_shutdown.set()
|
|
|
|
|
|
|
|
def callback(self, ev: Dict[str, Any]) -> None:
|
2021-11-04 11:28:36 +00:00
|
|
|
self.assertEqual(
|
|
|
|
ev, {
|
|
|
|
'request_id': '1234',
|
|
|
|
'process_id': os.getpid(),
|
|
|
|
'thread_id': PUBLISHER_ID,
|
|
|
|
'event_timestamp': 1234567,
|
|
|
|
'event_name': eventNames.WORK_STARTED,
|
|
|
|
'event_payload': {'hello': 'events'},
|
2023-03-14 14:30:07 +00:00
|
|
|
'publisher_id': self.__class__.__qualname__,
|
2021-11-04 11:28:36 +00:00
|
|
|
},
|
|
|
|
)
|