Commit 5d54f74a authored by BO ZHANG's avatar BO ZHANG 🏀
Browse files

update Redis and tests

parent b0981e72
Loading
Loading
Loading
Loading
+17 −14
Original line number Diff line number Diff line
@@ -5,13 +5,25 @@ import os
redis_config_path = os.path.join(os.path.dirname(__file__), "redis_config.toml")

REDIS_CONFIG = toml.load(redis_config_path)
REDIS_QNAME = "csst_data_list"


class Redis(redis.Redis):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.name = REDIS_QNAME
    def __init__(self, location="naoc"):
        if location not in REDIS_CONFIG.keys():
            print("Available redis locations: ", list(REDIS_CONFIG.keys()))
            raise ValueError(f"Unknown redis location: {location}")
        super().__init__(
            host=REDIS_CONFIG[location]["host"],
            port=REDIS_CONFIG[location]["port"],
            db=REDIS_CONFIG[location]["db"],
            password=REDIS_CONFIG[location]["password"],
        )
        self.qname = password = REDIS_CONFIG[location]["qname"]
        self.config = REDIS_CONFIG[location]

        print("Setting redis config:")
        for k, v in self.config.items():
            print(f" - {k}: {v}")

    def push(self, msg):
        self.lpush(self.name, msg)
@@ -20,18 +32,10 @@ class Redis(redis.Redis):
        return self.rpop(self.name)

    def get_all(self):
        return self.lrange(self.name, 0, -1)


def get_redis(location="naoc"):
    print("Available redis locations: ", list(REDIS_CONFIG.keys()))
    if location not in REDIS_CONFIG.keys():
        raise ValueError(f"Unknown redis location: {location}")
    return Redis(**REDIS_CONFIG[location])
        return self.lrange(self.qname, 0, -1)


# msgs = r.lrange(name, 0, -1)
#
# for chipid in range(6, 26):
#     this_msg = gen_msg(
#         dag_id="csst-msc-l1-mbi", obsid="11009101682009", chipid=f"{chipid:02d}"
@@ -42,6 +46,5 @@ def get_redis(location="naoc"):
# msgs = r.lrange(name, 0, -1)
# print(msgs)
#
#
# msgs_later = r.lrange(name, 0, -1)
# print(msgs_later)
+2 −3
Original line number Diff line number Diff line
from csst_dag import get_redis
from csst_dag.redis import Redis


def test_redis_naoc():
    r = get_redis("naoc")
    r = Redis(location="naoc")
    print(r)
    assert r.name == "csst_data_list"
    assert len(r.get_all()) == 0