SIENTIAPDE-988

Refactor OpcManager initialization and unsubscribe method; enhance logging

- Removed the `initialize_from_config` method from `OpcManager` class and adjusted the constructor to handle configuration directly.
- Improved the `unsubscribe` method to include detailed logging for non-existent subscriptions.
- Updated tests in `test_opc_manager.py` to reflect changes in the `OpcManager` class.
- Commented out Docker-related fixtures in `conftest.py` for potential future use.
- Enhanced test coverage in `test_single_node.py` and `test_data_manager.py` with additional scenarios and assertions.
- Introduced new methods in `Ingestor` and `IngestorManager` classes to manage server subscriptions and leases more effectively.
- Added new tests for `Ingestor` class to validate initialization and slot management logic.
- Implemented logging improvements across various classes to ensure better traceability of actions and errors.
This commit is contained in:
vitor-aignosi
2025-04-23 18:24:49 -03:00
parent 4208412eca
commit b9ef69a865
13 changed files with 1128 additions and 294 deletions

View File

@@ -1,47 +1,47 @@
import subprocess
from time import sleep
from typing import Generator
import uuid
from kafka import KafkaConsumer
import pytest
from redis import Redis
# import subprocess
# from time import sleep
# from typing import Generator
# import uuid
# from kafka import KafkaConsumer
# import pytest
# from redis import Redis
@pytest.fixture(scope="session", autouse=True)
def docker_compose():
"""Sobe os containers antes dos testes e derruba depois."""
print("\n🚀 Subindo Docker Compose...")
subprocess.run(["docker", "compose", "up", "-d"], check=True)
# @pytest.fixture(scope="session", autouse=True)
# def docker_compose():
# """Sobe os containers antes dos testes e derruba depois."""
# print("\n🚀 Subindo Docker Compose...")
# subprocess.run(["docker", "compose", "up", "-d"], check=True)
print("⏳ Aguardando containers ficarem prontos...")
sleep(15) # ajuste conforme necessário
# print("⏳ Aguardando containers ficarem prontos...")
# sleep(15) # ajuste conforme necessário
yield # os testes rodam aqui
# yield # os testes rodam aqui
print("\n🧹 Derrubando Docker Compose...")
subprocess.run(["docker", "compose", "down"], check=True)
# print("\n🧹 Derrubando Docker Compose...")
# subprocess.run(["docker", "compose", "down"], check=True)
@pytest.fixture()
def redis_client():
redis = Redis(host="localhost", port=6379, decode_responses=True)
redis.flushdb()
# @pytest.fixture()
# def redis_client():
# redis = Redis(host="localhost", port=6379, decode_responses=True)
# redis.flushdb()
yield redis
# yield redis
# Limpa o banco de dados após os testes
redis.flushdb()
redis.close()
# # Limpa o banco de dados após os testes
# redis.flushdb()
# redis.close()
def kafka_searcher(topic) -> Generator[KafkaConsumer, None, None]:
consumer = KafkaConsumer(
topic,
bootstrap_servers="localhost:9092",
group_id=f"test-group-{uuid.uuid4()}",
auto_offset_reset="earliest", # Começa a consumir apenas mensagens novas
enable_auto_commit=True,
)
# def kafka_searcher(topic) -> Generator[KafkaConsumer, None, None]:
# consumer = KafkaConsumer(
# topic,
# bootstrap_servers="localhost:9092",
# group_id=f"test-group-{uuid.uuid4()}",
# auto_offset_reset="earliest", # Começa a consumir apenas mensagens novas
# enable_auto_commit=True,
# )
yield consumer
consumer.close()
# yield consumer
# consumer.close()

View File

@@ -1,118 +1,118 @@
import json
import subprocess
from time import sleep
# import json
# import subprocess
# from time import sleep
from tests.functional.conftest import kafka_searcher
# from tests.functional.conftest import kafka_searcher
new_data = {
"slot:opc_tags:1": {
"server1": {
"name": "server1",
"url": "opc.tcp://simulator:4840",
"server_uri": "http://opcua-server.simulator",
"tags": {
'ns=2;i=2': {
'tag_name': 'Counter',
'frequency': 1000,
'topics': [],
},
'ns=2;i=3': {
'tag_name': 'Rollout',
'frequency': 1000,
"topics": [],
},
'ns=2;i=4': {
'tag_name': 'Square',
'frequency': 1000,
"topics": [],
},
}
}
},
"slot:opc_tags:2": {
"server2": {
"name": "server2",
"url": "opc.tcp://simulator:4840",
"server_uri": "http://opcua-server.simulator",
"tags": {
'ns=2;i=2': {
'tag_name': 'Counter',
'frequency': 1000,
'topics': [],
},
'ns=2;i=3': {
'tag_name': 'Rollout',
'frequency': 1000,
"topics": [],
},
'ns=2;i=4': {
'tag_name': 'Square',
'frequency': 1000,
"topics": [],
},
}
}
}
}
# new_data = {
# "slot:opc_tags:1": {
# "server1": {
# "name": "server1",
# "url": "opc.tcp://simulator:4840",
# "server_uri": "http://opcua-server.simulator",
# "tags": {
# 'ns=2;i=2': {
# 'tag_name': 'Counter',
# 'frequency': 1000,
# 'topics': [],
# },
# 'ns=2;i=3': {
# 'tag_name': 'Rollout',
# 'frequency': 1000,
# "topics": [],
# },
# 'ns=2;i=4': {
# 'tag_name': 'Square',
# 'frequency': 1000,
# "topics": [],
# },
# }
# }
# },
# "slot:opc_tags:2": {
# "server2": {
# "name": "server2",
# "url": "opc.tcp://simulator:4840",
# "server_uri": "http://opcua-server.simulator",
# "tags": {
# 'ns=2;i=2': {
# 'tag_name': 'Counter',
# 'frequency': 1000,
# 'topics': [],
# },
# 'ns=2;i=3': {
# 'tag_name': 'Rollout',
# 'frequency': 1000,
# "topics": [],
# },
# 'ns=2;i=4': {
# 'tag_name': 'Square',
# 'frequency': 1000,
# "topics": [],
# },
# }
# }
# }
# }
def test_simple(redis_client):
new_data['slot:opc_tags:1']['server1']['tags']['ns=2;i=2']['topics'] = [
'test_topic_1']
redis_client.set("slot:opc_tags:1",
json.dumps(new_data['slot:opc_tags:1']))
# def test_simple(redis_client):
# new_data['slot:opc_tags:1']['server1']['tags']['ns=2;i=2']['topics'] = [
# 'test_topic_1']
# redis_client.set("slot:opc_tags:1",
# json.dumps(new_data['slot:opc_tags:1']))
sleep(20) # Espera o Ingestor processar os dados
# sleep(20) # Espera o Ingestor processar os dados
# Check if lease is in Redis
assert redis_client.get("lease:opc_tags:1") == 'ingestor'
assert redis_client.get("heartbeat:ingestor:ingestor") == '1'
# # Check if lease is in Redis
# assert redis_client.get("lease:opc_tags:1") == 'ingestor'
# assert redis_client.get("heartbeat:ingestor:ingestor") == '1'
# Check if data is in Kafka
# # Check if data is in Kafka
kafka = next(kafka_searcher('test_topic_1'))
sleep(1)
messages = kafka.poll(timeout_ms=10000)
# kafka = next(kafka_searcher('test_topic_1'))
# sleep(1)
# messages = kafka.poll(timeout_ms=10000)
assert messages, "Expected messages in Kafka, but got none."
# assert messages, "Expected messages in Kafka, but got none."
def test_simple_double_slot(redis_client):
# def test_simple_double_slot(redis_client):
new_data['slot:opc_tags:1']['server1']['tags']['ns=2;i=2']['topics'] = [
'test_topic_double_slot1']
redis_client.set("slot:opc_tags:1",
json.dumps(new_data['slot:opc_tags:1']))
# new_data['slot:opc_tags:1']['server1']['tags']['ns=2;i=2']['topics'] = [
# 'test_topic_double_slot1']
# redis_client.set("slot:opc_tags:1",
# json.dumps(new_data['slot:opc_tags:1']))
sleep(20) # Espera o Ingestor processar os dados
# sleep(20) # Espera o Ingestor processar os dados
assert redis_client.get("lease:opc_tags:1") == 'ingestor'
assert redis_client.get("heartbeat:ingestor:ingestor") == '1'
# assert redis_client.get("lease:opc_tags:1") == 'ingestor'
# assert redis_client.get("heartbeat:ingestor:ingestor") == '1'
# Check if data is in Kafka
kafka1 = next(kafka_searcher('test_topic_double_slot1'))
messages = kafka1.poll(timeout_ms=10000)
# # Check if data is in Kafka
# kafka1 = next(kafka_searcher('test_topic_double_slot1'))
# messages = kafka1.poll(timeout_ms=10000)
assert messages, "Expected messages in test_topic_double_slot1, but got none."
# assert messages, "Expected messages in test_topic_double_slot1, but got none."
new_data['slot:opc_tags:2']['server2']['tags']['ns=2;i=2']['topics'] = [
'test_topic_double_slot2']
redis_client.set("slot:opc_tags:2",
json.dumps(new_data['slot:opc_tags:2']))
# new_data['slot:opc_tags:2']['server2']['tags']['ns=2;i=2']['topics'] = [
# 'test_topic_double_slot2']
# redis_client.set("slot:opc_tags:2",
# json.dumps(new_data['slot:opc_tags:2']))
sleep(20) # Espera o Ingestor processar os dados
# sleep(20) # Espera o Ingestor processar os dados
# Check if lease is in Redis
assert redis_client.get("lease:opc_tags:2") == 'ingestor'
assert redis_client.get("lease:opc_tags:1") == 'ingestor'
assert redis_client.get("heartbeat:ingestor:ingestor") == '1'
# # Check if lease is in Redis
# assert redis_client.get("lease:opc_tags:2") == 'ingestor'
# assert redis_client.get("lease:opc_tags:1") == 'ingestor'
# assert redis_client.get("heartbeat:ingestor:ingestor") == '1'
# Check if data is in Kafka
kafka2 = next(kafka_searcher('test_topic_double_slot2'))
messages = kafka2.poll(timeout_ms=10000)
# # Check if data is in Kafka
# kafka2 = next(kafka_searcher('test_topic_double_slot2'))
# messages = kafka2.poll(timeout_ms=10000)
assert messages, "Expected messages in test_topic_double_slot2, but got none."
# assert messages, "Expected messages in test_topic_double_slot2, but got none."
messages = kafka1.poll(timeout_ms=10000)
assert messages, "Expected messages in test_topic_double_slot1, but got none."
# messages = kafka1.poll(timeout_ms=10000)
# assert messages, "Expected messages in test_topic_double_slot1, but got none."