Commit 4ec684c1 authored by Robin Obůrka's avatar Robin Obůrka

tests: Add test for monitoring

parent 00023336
import pytest
from unittest.mock import patch
import zmq
@pytest.fixture
def monitoring_socket():
ctx = zmq.Context.instance()
return ctx.socket(zmq.PUSH)
@pytest.fixture
def send_multipart_mock():
with patch("zmq.Socket.send_multipart") as m:
yield m
import sn
from unittest.mock import Mock
def test_sentinel_backend(monitoring_socket, send_multipart_mock):
b = sn.monitoring.Monitoring("test", monitoring_socket)
b.message("test", {"some": "data"})
assert send_multipart_mock.called
assert send_multipart_mock.call_args[0][0][0] == b"sentinel/monitoring/test"
def test_log_backend():
b = sn.monitoring.Monitoring("test")
# OK, This is stupid and ugly, but I need to test that the backend does something
b.logger = Mock()
b.message("test", {"some": "data"})
assert b.logger.debug.called
assert b.logger.debug.call_args[0][1] == "sentinel/monitoring/test"
import threading
import sn
def test_counter():
c = sn.monitoring.Counter("test", threading.Lock())
assert c.value == 0
c.count()
assert c.value == 1
c.reset()
assert c.value == 0
import pytest
import sn
def test_managing_multiple_counters(monitoring_socket, send_multipart_mock):
m = sn.monitoring.Monitoring("test", monitoring_socket)
c1 = m.get_counter("counter1")
c2 = m.get_counter("counter2")
counters = m._get_counters()
assert counters["counter1"] == 0
assert counters["counter2"] == 0
c1.count()
c2.count()
counters = m._get_counters()
assert counters["counter1"] == 1
assert counters["counter2"] == 1
m._reset_counters()
counters = m._get_counters()
assert counters["counter1"] == 0
assert counters["counter2"] == 0
def test_unique_names(monitoring_socket, send_multipart_mock):
m = sn.monitoring.Monitoring("test", monitoring_socket)
m.get_counter("counter")
with pytest.raises(ValueError):
m.get_counter("counter")
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment