salt/tests/support/events.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

77 lines
2 KiB
Python
Raw Normal View History

2020-04-02 20:10:20 -05:00
"""
2019-01-16 14:33:50 +00:00
tests.support.events
~~~~~~~~~~~~~~~~~~~~
2020-04-02 20:10:20 -05:00
"""
2019-01-16 14:33:50 +00:00
import multiprocessing
import os
import time
from contextlib import contextmanager
import salt.utils.event
2023-06-26 22:01:43 -07:00
from salt.utils.process import Process, clean_proc
2019-01-16 14:33:50 +00:00
@contextmanager
def eventpublisher_process(sock_dir):
2023-06-15 23:54:49 -07:00
opts = {
"sock_dir": sock_dir,
"interface": "127.0.0.1",
"publish_port": 4506,
"ipv6": None,
"zmq_filtering": None,
}
2023-07-01 00:47:57 -07:00
ipc_publisher = salt.transport.publish_server(
opts,
pub_path=os.path.join(opts["sock_dir"], "master_event_pub.ipc"),
pull_path=os.path.join(opts["sock_dir"], "master_event_pull.ipc"),
transport="tcp",
2023-06-15 23:54:49 -07:00
)
proc = Process(
target=ipc_publisher.publish_daemon,
args=[
ipc_publisher.publish_payload,
],
)
2023-06-26 22:01:43 -07:00
# proc = salt.utils.event.EventPublisher({"sock_dir": sock_dir})
2019-01-16 14:33:50 +00:00
proc.start()
try:
if os.environ.get("TRAVIS_PYTHON_VERSION", None) is not None:
# Travis is slow
time.sleep(10)
else:
time.sleep(8)
2019-01-16 14:33:50 +00:00
yield
finally:
clean_proc(proc)
class EventSender(multiprocessing.Process):
def __init__(self, data, tag, wait, sock_dir):
2020-09-28 15:33:05 -04:00
super().__init__()
2019-01-16 14:33:50 +00:00
self.data = data
self.tag = tag
self.wait = wait
self.sock_dir = sock_dir
def run(self):
with salt.utils.event.MasterEvent(self.sock_dir, listen=False) as me:
time.sleep(self.wait)
me.fire_event(self.data, self.tag)
# Wait a few seconds before tearing down the zmq context
if os.environ.get("TRAVIS_PYTHON_VERSION", None) is not None:
# Travis is slow
time.sleep(10)
else:
time.sleep(2)
2019-01-16 14:33:50 +00:00
@contextmanager
def eventsender_process(data, tag, sock_dir, wait=0):
proc = EventSender(data, tag, wait, sock_dir=sock_dir)
proc.start()
try:
yield
finally:
clean_proc(proc)