Repository: abhinavsingh/proxy.py
Pull request #239 (2019-12-21 00:59:24)

Initialize manager in main thread for event queue

codecov[bot]: # [Codecov](https://codecov.io/gh/abhinavsingh/proxy.py/pull/239?src=pr&el=h1) Report
> Merging [#239](https://codecov.io/gh/abhinavsingh/proxy.py/pull/239?src=pr&el=desc) into [develop](https://codecov.io/gh/abhinavsingh/proxy.py/commit/c6c09395f9d3f177d78049c48b522f29bb6ae32a?src=pr&el=desc) will **decrease** coverage by `0.03%`.
> The diff coverage is `66.66%`.

[![Impacted file tree graph](https://codecov.io/gh/abhinavsingh/proxy.py/pull/239/graphs/tree.svg?width=650&token=Zh9J7b4la2&height=150&src=pr)](https://codecov.io/gh/abhinavsingh/proxy.py/pull/239?src=pr&el=tree)

```diff
@@             Coverage Diff             @@
##           develop     #239      +/-   ##
===========================================
- Coverage    73.13%   73.09%   -0.04%     
===========================================
  Files           70       70              
  Lines         2788     2788              
===========================================
- Hits          2039     2038       -1     
- Misses         749      750       +1
```


| [Impacted Files](https://codecov.io/gh/abhinavsingh/proxy.py/pull/239?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [proxy/core/event.py](https://codecov.io/gh/abhinavsingh/proxy.py/pull/239/diff?src=pr&el=tree#diff-cHJveHkvY29yZS9ldmVudC5weQ==) | `79% <100%> (-0.42%)` | :arrow_down: |
| [proxy/common/constants.py](https://codecov.io/gh/abhinavsingh/proxy.py/pull/239/diff?src=pr&el=tree#diff-cHJveHkvY29tbW9uL2NvbnN0YW50cy5weQ==) | `100% <100%> (ø)` | :arrow_up: |
| [proxy/core/acceptor/pool.py](https://codecov.io/gh/abhinavsingh/proxy.py/pull/239/diff?src=pr&el=tree#diff-cHJveHkvY29yZS9hY2NlcHRvci9wb29sLnB5) | `79.22% <33.33%> (-0.78%)` | :arrow_down: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/abhinavsingh/proxy.py/pull/239?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/abhinavsingh/proxy.py/pull/239?src=pr&el=footer). Last update [c6c0939...eb468c4](https://codecov.io/gh/abhinavsingh/proxy.py/pull/239?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/.gitignore b/.gitignore
index 404007f..45423f1 100644
--- a/.gitignore
+++ b/.gitignore
@@ -12,16 +12,16 @@
 
 coverage.xml
 proxy.py.iml
-*.pyc
 
+*.pyc
+*.egg-info
 *.csr
 *.crt
 *.key
-
 venv*
+
 cover
 htmlcov
 dist
 build
-
-*.egg-info
+proxy/public
diff --git a/proxy/common/constants.py b/proxy/common/constants.py
index d2ec48d..72efbce 100644
--- a/proxy/common/constants.py
+++ b/proxy/common/constants.py
@@ -68,7 +68,7 @@ DEFAULT_PID_FILE = None
 DEFAULT_PLUGINS = ''
 DEFAULT_PORT = 8899
 DEFAULT_SERVER_RECVBUF_SIZE = DEFAULT_BUFFER_SIZE
-DEFAULT_STATIC_SERVER_DIR = PROXY_PY_DIR
+DEFAULT_STATIC_SERVER_DIR = os.path.join(PROXY_PY_DIR, "public")
 DEFAULT_THREADLESS = False
 DEFAULT_TIMEOUT = 10
 DEFAULT_VERSION = False
diff --git a/proxy/core/acceptor/pool.py b/proxy/core/acceptor/pool.py
index 87d7158..48cedaf 100644
--- a/proxy/core/acceptor/pool.py
+++ b/proxy/core/acceptor/pool.py
@@ -46,8 +46,11 @@ class AcceptorPool:
         self.event_dispatcher: Optional[EventDispatcher] = None
         self.event_dispatcher_thread: Optional[threading.Thread] = None
         self.event_dispatcher_shutdown: Optional[threading.Event] = None
+        self.manager: Optional[multiprocessing.managers.SyncManager] = None
+
         if self.flags.enable_events:
-            self.event_queue = EventQueue()
+            self.manager = multiprocessing.Manager()
+            self.event_queue = EventQueue(self.manager.Queue())
 
     def listen(self) -> None:
         self.socket = socket.socket(self.flags.family, socket.SOCK_STREAM)
diff --git a/proxy/core/event.py b/proxy/core/event.py
index cca9ce3..00fec14 100644
--- a/proxy/core/event.py
+++ b/proxy/core/event.py
@@ -56,10 +56,8 @@ class EventQueue:
     7. Publisher ID (optional)  - Optionally, publishing entity unique name / ID
     """
 
-    def __init__(self) -> None:
-        super().__init__()
-        self.manager = multiprocessing.Manager()
-        self.queue = self.manager.Queue()
+    def __init__(self, queue: DictQueueType) -> None:
+        self.queue = queue
 
     def publish(
         self,


Test patch:
diff --git a/tests/core/test_event_dispatcher.py b/tests/core/test_event_dispatcher.py
index 72930b8..bb17a70 100644
--- a/tests/core/test_event_dispatcher.py
+++ b/tests/core/test_event_dispatcher.py
@@ -24,7 +24,7 @@ class TestEventDispatcher(unittest.TestCase):
 
     def setUp(self) -> None:
         self.dispatcher_shutdown = threading.Event()
-        self.event_queue = EventQueue()
+        self.event_queue = EventQueue(multiprocessing.Manager().Queue())
         self.dispatcher = EventDispatcher(
             shutdown=self.dispatcher_shutdown,
             event_queue=self.event_queue)
diff --git a/tests/core/test_event_queue.py b/tests/core/test_event_queue.py
index 09e57bc..18f7527 100644
--- a/tests/core/test_event_queue.py
+++ b/tests/core/test_event_queue.py
@@ -17,13 +17,15 @@ from unittest import mock
 
 from proxy.core.event import EventQueue, eventNames
 
+MANAGER = multiprocessing.Manager()
+
 
 class TestCoreEvent(unittest.TestCase):
 
     @mock.patch('time.time')
     def test_publish(self, mock_time: mock.Mock) -> None:
         mock_time.return_value = 1234567
-        evq = EventQueue()
+        evq = EventQueue(MANAGER.Queue())
         evq.publish(
             request_id='1234',
             event_name=eventNames.WORK_STARTED,
@@ -41,7 +43,7 @@ class TestCoreEvent(unittest.TestCase):
         })
 
     def test_subscribe(self) -> None:
-        evq = EventQueue()
+        evq = EventQueue(MANAGER.Queue())
         q = multiprocessing.Manager().Queue()
         evq.subscribe('1234', q)
         ev = evq.queue.get()
@@ -49,7 +51,7 @@ class TestCoreEvent(unittest.TestCase):
         self.assertEqual(ev['event_payload']['sub_id'], '1234')
 
     def test_unsubscribe(self) -> None:
-        evq = EventQueue()
+        evq = EventQueue(MANAGER.Queue())
         evq.unsubscribe('1234')
         ev = evq.queue.get()
         self.assertEqual(ev['event_name'], eventNames.UNSUBSCRIBE)
diff --git a/tests/core/test_event_subscriber.py b/tests/core/test_event_subscriber.py
index 902f2f3..30e67b3 100644
--- a/tests/core/test_event_subscriber.py
+++ b/tests/core/test_event_subscriber.py
@@ -11,6 +11,7 @@
 import os
 import threading
 import unittest
+import multiprocessing
 from typing import Dict, Any
 
 from unittest import mock
@@ -26,7 +27,7 @@ class TestEventSubscriber(unittest.TestCase):
     def test_event_subscriber(self, mock_time: mock.Mock) -> None:
         mock_time.return_value = 1234567
         self.dispatcher_shutdown = threading.Event()
-        self.event_queue = EventQueue()
+        self.event_queue = EventQueue(multiprocessing.Manager().Queue())
         self.dispatcher = EventDispatcher(
             shutdown=self.dispatcher_shutdown,
             event_queue=self.event_queue)