Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add redis pub/sub handler #9

Merged
merged 7 commits into from
Jan 6, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Fix tests bug on RedisPubSubLogHandler
  • Loading branch information
Iglesys347 committed Jan 6, 2023
commit a2b93fd70708c0be0787076f412be3f892012964
40 changes: 40 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import os
import logging

import pytest
from redis import Redis

# default values for Redis
REDIS_HOST = os.environ.get("REDIS_HOST", "localhost")
REDIS_PORT = os.environ.get("REDIS_PORT", 6379)


@pytest.fixture
def redis_client():
return Redis(host=REDIS_HOST, port=REDIS_PORT, decode_responses=True)


@pytest.fixture
def redis_client_no_decode():
return Redis(host=REDIS_HOST, port=REDIS_PORT)


@pytest.fixture
def log_record():
return logging.LogRecord("", 0, "", 0, None, None, None)


@pytest.fixture
def logger():
logging.basicConfig()
log = logging.getLogger('test_rlh')
log.setLevel(logging.INFO)
return log


@pytest.fixture(autouse=True)
def clean_redis_test_keys():
yield
redis = Redis(host=REDIS_HOST, port=REDIS_PORT)
for key in redis.keys("*test*"):
redis.delete(key)
83 changes: 21 additions & 62 deletions tests/test_handlers.py
Original file line number Diff line number Diff line change
@@ -1,47 +1,11 @@
import os
import time

import pytest
import logging
import pickle
import json
import pickle

from redis import Redis
import pytest

from rlh import RedisLogHandler, RedisStreamLogHandler, RedisPubSubLogHandler
from rlh.handlers import DEFAULT_FIELDS

# default values for Redis
REDIS_HOST = os.environ.get("REDIS_HOST", "localhost")
REDIS_PORT = os.environ.get("REDIS_PORT", 6379)


@pytest.fixture
def redis_client():
client = Redis(host=REDIS_HOST, port=REDIS_PORT, decode_responses=True)
# Making sure the stream is empty
for key in client.scan_iter("test*"):
client.delete(key)
return client


@pytest.fixture
def log_record():
return logging.LogRecord("", 0, "", 0, None, None, None)


@pytest.fixture
def logger():
logging.basicConfig()
logger = logging.getLogger('test_rlh')
logger.setLevel(logging.INFO)
return logger

def new_logger():
logging.basicConfig()
logger = logging.getLogger('test_rlh')
logger.setLevel(logging.INFO)
return logger

class TestRedisLogHandler:

@@ -132,20 +96,18 @@ def test_emit_log_fields(self, redis_client, logger, input, expected):

assert list(data.keys()) == expected

def test_emit_as_pkl(self, logger):
# Define a Redis client with 'decode_responses=False'
redis_client = Redis(host=REDIS_HOST, port=REDIS_PORT)
def test_emit_as_pkl(self, redis_client_no_decode, logger):
# Create a RedisStreamLogHandler instance with as_pkl argument
handler = RedisStreamLogHandler(redis_client=redis_client, as_pkl=True,
stream_name="test_logs")
handler = RedisStreamLogHandler(redis_client=redis_client_no_decode,
as_pkl=True, stream_name="test_logs")

# Add the handler to the logger
logger.addHandler(handler)
# Testing log
logger.info('Testing my redis logger')

# Retrieve the last log saved in Redis
res = redis_client.xrange("test_logs", "-", "+")[-1]
res = redis_client_no_decode.xrange("test_logs", "-", "+")[-1]

# Retrieve the data stored in Redis
data = res[1]
@@ -213,52 +175,50 @@ def test_emit_log_fields(self, redis_client, logger, input, expected):

# Add the handler to the logger
logger.addHandler(handler)

p = redis_client.pubsub()
# Subscribe to channel
p.subscribe("test_logs")

assert p.get_message(ignore_subscribe_messages=True) is None

# Retrieving subscribe message
assert p.get_message(timeout=10)["type"] == "subscribe"

# Testing log
logger.info('Testing my redis logger')
# Removing the handler from the logger
logger.handlers.clear()

# Sleeping for 1 sec before retrieving the log
time.sleep(1)
# Retrieve the last log saved in Redis
mess = p.get_message(ignore_subscribe_messages=True, timeout=1)
mess = p.get_message(ignore_subscribe_messages=True, timeout=10)
# Retrieve the data stored in Redis
data = json.loads(mess["data"])

assert list(data.keys()) == expected

def test_emit_as_pkl(self, logger):
# Define a Redis client with 'decode_responses=False'
redis_client = Redis(host=REDIS_HOST, port=REDIS_PORT)
def test_emit_as_pkl(self, redis_client_no_decode, logger):
# Create a RedisPubSubLogHandler instance with as_pkl argument
handler = RedisPubSubLogHandler(redis_client=redis_client, as_pkl=True,
handler = RedisPubSubLogHandler(redis_client=redis_client_no_decode, as_pkl=True,
channel_name="test_logs")

# Add the handler to the logger
logger.addHandler(handler)
p = redis_client.pubsub()

p = redis_client_no_decode.pubsub()
# Subscribe to channel
p.subscribe("test_logs")

assert p.get_message(ignore_subscribe_messages=True) is None

# Retrieving subscribe message
assert p.get_message(timeout=10)["type"] == "subscribe"

# Testing log
logger.info('Testing my redis logger')
# Removing the handler from the logger
logger.handlers.clear()

# Sleeping for 1 sec before retrieving the log
time.sleep(1)
# time.sleep(1)
# Retrieve the last log saved in Redis
mess = p.get_message(ignore_subscribe_messages=True, timeout=1)
mess = p.get_message(ignore_subscribe_messages=True, timeout=10)
# Retrieve the data stored in Redis
data = mess["data"]

@@ -268,4 +228,3 @@ def test_emit_as_pkl(self, logger):
# We cannot perform a deep test as we cannot retrieve the LogRecord emitted
assert log.msg == 'Testing my redis logger'
assert log.levelname == 'INFO'