Added/update docstring

This commit is contained in:
Scharnitzky Donát 2020-04-06 20:48:36 +02:00
parent 64f7d9b83e
commit 9040cf898b
5 changed files with 115 additions and 43 deletions

7
app.py
View File

@ -11,7 +11,7 @@ from consumerlocator import ConsumerLocator
from messagesender import MessageSender from messagesender import MessageSender
""" """
Main entrypoint Main entry point, This module builds the producer from the submodules.
""" """
__author__ = "@tormakris" __author__ = "@tormakris"
@ -29,6 +29,11 @@ LOGGER = logging.getLogger(__name__)
KNOWNCONSUMER = os.getenv("PRODUCER_KNOWNCONSUMER",'10.69.42.1') KNOWNCONSUMER = os.getenv("PRODUCER_KNOWNCONSUMER",'10.69.42.1')
"""
This is the producers entry point, initializes all the components (:class:'communicator.Communicator',
:class:'consumerlocator.ConsumerLocator' and :class:'messagesender.MessageSender') and sends infinite random
messages.
"""
if __name__ == "__main__": if __name__ == "__main__":
LOGGER.info("Producer started") LOGGER.info("Producer started")
generateduuid = str(uuid.uuid4()) generateduuid = str(uuid.uuid4())

View File

@ -20,19 +20,17 @@ class Communicator:
""" """
def __init__(self, currentconsumer: str, uuid: str): def __init__(self, currentconsumer: str, uuid: str):
""" """Initialize object
Initialize object :param consumerlocator: the current consumer's IP address as a string
:param consumerlocator: :param uuid: string typed UUID.
:param uuid:
""" """
self.currenctconsumer=currentconsumer self.currenctconsumer=currentconsumer
self.uuid = uuid self.uuid = uuid
def sendmessage(self, message: str) -> None: def sendmessage(self, message: str) -> None:
""" """Send message to the current consumer. Logs the process.
Send message to consumer. :param message: the message of type string that will be sent.
:param message: :return: None
:return: none
""" """
currentconsumer=self.currenctconsumer currentconsumer=self.currenctconsumer
LOGGER.info(f"Sending message to {currentconsumer}") LOGGER.info(f"Sending message to {currentconsumer}")
@ -40,9 +38,8 @@ class Communicator:
LOGGER.debug(f"Message status code is:{postresponse.status_code}") LOGGER.debug(f"Message status code is:{postresponse.status_code}")
def discoveravailableconsumers(self) -> list: def discoveravailableconsumers(self) -> list:
""" """Get the list of available consumer from the current primary consumer. Logs the received list.
Get the list of available consumer from the current primary consumer. :return: list of consumers' IP addresses
:return:
""" """
try: try:
currentconsumer = self.currenctconsumer currentconsumer = self.currenctconsumer
@ -55,9 +52,8 @@ class Communicator:
return [] return []
def isconsumeravailable(self) -> bool: def isconsumeravailable(self) -> bool:
""" """Readiness probe current consumer. Logs the result.
Readiness probe primary consumer. :return: True if available, False otherwise
:return:
""" """
currentconsumer = self.currenctconsumer currentconsumer = self.currenctconsumer
try: try:
@ -70,10 +66,9 @@ class Communicator:
return isavailable return isavailable
def checkconsumer(self, consumer: str) -> bool: def checkconsumer(self, consumer: str) -> bool:
""" """Readiness probe of a particular consumer. Logs the result.
Readiness probe of a prticular consumer. :param consumer: the consumer's IP address
:param consumer: :return: True if available, False otherwise
:return:
""" """
try: try:
response = requests.get(f'http://{consumer}/consumers') response = requests.get(f'http://{consumer}/consumers')
@ -84,10 +79,9 @@ class Communicator:
LOGGER.info(f"Consumer {consumer} availability: {isavailable}") LOGGER.info(f"Consumer {consumer} availability: {isavailable}")
return isavailable return isavailable
def set_currentconsumer(self,currenctconsumer): def set_currentconsumer(self,currenctconsumer) -> None:
""" """Set current consumer
Set current consumer :param currenctconsumer: the consumer's IP address
:param currenctconsumer: :return: None
:return:
""" """
self.currenctconsumer=currenctconsumer self.currenctconsumer=currenctconsumer

View File

@ -18,20 +18,25 @@ KNOWNCONSUMER = os.getenv("PRODUCER_KNOWNCONSUMER",'10.69.42.1')
class ConsumerLocator: class ConsumerLocator:
""" """
Manages the list of consumers. Component responsible for managing the list of consumers. Requires an instance of :class:'communicator.Communicator'
""" """
def __init__(self, uuid: str, communicator: Communicator): def __init__(self, uuid: str, communicator: Communicator):
""" """Initializes the object.
Initialize class. Gets the known consumer's IP address from the PRODUCER_KNOWNCONSUMER envar.
:param: uuid: Not used
:param: communicator: the :class:'communicator.Communicator' instance that will be used for the low level
communication.
""" """
self.consumerlist = [{"Host": KNOWNCONSUMER, "State": True, "LastOk": datetime.datetime.now()}] self.consumerlist = [{"Host": KNOWNCONSUMER, "State": True, "LastOk": datetime.datetime.now()}]
self.currentconsumer = self.consumerlist[0] self.currentconsumer = self.consumerlist[0]
self.communicator = communicator self.communicator = communicator
def learnconsumerlist(self) -> None: def learnconsumerlist(self) -> None:
"""" """"Learns the list of consumers from the current consumer.
Learns the list of consumers. Calls :func:'~communicator.Communicator.didiscoveravailableconsumers', adds the learned consumers to the list
if are not present, and then calls :func:'~consumerlocator.ConsumerLocator.updateconsumerlist'
:return: None
""" """
recievedconsumerlist = self.communicator.discoveravailableconsumers() recievedconsumerlist = self.communicator.discoveravailableconsumers()
if recievedconsumerlist is None: if recievedconsumerlist is None:
@ -48,8 +53,10 @@ class ConsumerLocator:
self.updateconsumerlist() self.updateconsumerlist()
def updateconsumerlist(self) -> None: def updateconsumerlist(self) -> None:
""" """ Updates the consumer list based on their availability.
Updates the consumer list based on their availability. Marks for each consumer if they are available or not. If a consumer is not available for some time (1 hour),
the it will be deleted from the list.
:return: None
""" """
removelist = [] removelist = []
for consumer in self.consumerlist: for consumer in self.consumerlist:
@ -64,8 +71,10 @@ class ConsumerLocator:
self.consumerlist.remove(rem) self.consumerlist.remove(rem)
def updateconsumer(self): def updateconsumer(self):
""" """If the current consumer is not available, checks all the consumers in the list and updates the current one.
If the current consumer is not available, checks all the consumers in the list and updates the current one. Calls :func:'~consumerlocator.ConsumerLocator.checkcurrentconsumer' and if needed
:func:'~consumerlocator.ConsumerLocator.updateconsumerlist'. Sets the :class:'communicator.Communicator'
current instance with :func:'~communicator.Communicator.set_currentconsumer'.
:return: the current consumer or None if there are no available customers at the moment. :return: the current consumer or None if there are no available customers at the moment.
""" """
@ -90,14 +99,14 @@ class ConsumerLocator:
def getcurrentconsumer(self) -> str: def getcurrentconsumer(self) -> str:
""" """
Returns the currently selected consumer. Returns the currently selected consumer's IP address.
:return: the current consumer :return: the current consumer
""" """
return self.currentconsumer["Host"] return self.currentconsumer["Host"]
def checkcurrentconsumer(self) -> bool: def checkcurrentconsumer(self) -> bool:
""" """
Check the consumers health. Check the current consumer's health.
:return: True if OK, False if fail :return: True if OK, False if fail
""" """
if self.currentconsumer is None: if self.currentconsumer is None:

View File

@ -19,25 +19,31 @@ LOGGER = logging.getLogger(__name__)
class MessageSender: class MessageSender:
""" """
Üzenetek küldéséért felelős komponens. Component responsible for sending the messages. Requires an instance of :class:'communicator.Communicator'
""" """
def __init__(self, communicator: Communicator): def __init__(self, communicator: Communicator):
""" """Initializes the object.
Inicializálja az osztályt. :param: communicator: an instance of :class:'communicator.Communicator'
""" """
self.communicator = communicator self.communicator = communicator
def randomstring(self, stringlength: int) -> str: def randomstring(self, stringlength: int) -> str:
"""Generate a random string of fixed length """ """
Generate a random string of fixed length
:param stringlength: the length of the string
:return: the generated string
"""
letters = string.ascii_lowercase letters = string.ascii_lowercase
return ''.join(random.choice(letters) for i in range(stringlength)) return ''.join(random.choice(letters) for i in range(stringlength))
def sendmessage(self, message: str = "") -> None: def sendmessage(self, message: str = "") -> None:
""" """Sends the given message.
Uzenet letrehozasa If the message is omitted (empty), then a random message will be generated with length 23 (with
:param message: :func:'~messagesender.MessageSender.randomstring'. Calls :func:'~communicator.Communicator.sendmessage'
:return: str tipus to send the message.
:param message: the message of type string that will be sent
:return: None
""" """
if not message: if not message:
data = self.randomstring(32) data = self.randomstring(32)

58
test.py
View File

@ -18,6 +18,10 @@ generateduuid = 'c959ad81-58f9-4445-aab4-8f3d68aee1ad'
def test_generate_string(mocker): def test_generate_string(mocker):
"""
Tests :func:'~messagesender.MessageSender.randomstring'.
:param: mocker:
"""
mocker.patch('communicator.Communicator') mocker.patch('communicator.Communicator')
comm = communicator.Communicator( comm = communicator.Communicator(
currentconsumer="localhost", currentconsumer="localhost",
@ -29,6 +33,11 @@ def test_generate_string(mocker):
def test_sendmessage(httpserver): def test_sendmessage(httpserver):
"""
Tests :func:'~communicator.Communicator.sendmessage'.
:param httpserver: simple HTTP server
:return: None
"""
httpserver.expect_oneshot_request( httpserver.expect_oneshot_request(
uri="/log", uri="/log",
method='POST', method='POST',
@ -46,6 +55,11 @@ def test_sendmessage(httpserver):
def test_send_message(mocker): def test_send_message(mocker):
"""
Tests :func:'~messagesender.MessageSender.sendmessage'.
:param mocker:
:return: None
"""
mocker.patch('communicator.Communicator') mocker.patch('communicator.Communicator')
comm = communicator.Communicator( comm = communicator.Communicator(
currentconsumer="127.0.0.1", currentconsumer="127.0.0.1",
@ -57,6 +71,11 @@ def test_send_message(mocker):
def test_discoveravailableconsumers(httpserver): def test_discoveravailableconsumers(httpserver):
"""
Tests :func:'~communicator.Communicator.discoveravailableconsumers'
:param httpserver: simple HTTP server
:return: None
"""
httpserver.expect_oneshot_request( httpserver.expect_oneshot_request(
uri="/consumers", uri="/consumers",
method='GET', method='GET',
@ -73,6 +92,11 @@ def test_discoveravailableconsumers(httpserver):
def test_isconsumeravailable(httpserver): def test_isconsumeravailable(httpserver):
"""
Tests :func:'~communicator.Communicator.isconsumeravailable'
:param httpserver: simple HTTP server
:return: None
"""
httpserver.expect_oneshot_request( httpserver.expect_oneshot_request(
uri="/consumers", uri="/consumers",
method='GET', method='GET',
@ -101,6 +125,11 @@ def test_isconsumeravailable(httpserver):
def test_checkconsumer(httpserver): def test_checkconsumer(httpserver):
"""
Tests :func:'~communicator.Communicator.checkconsumer'
:param httpserver: simple HTTP server
:return: None
"""
httpserver.expect_oneshot_request( httpserver.expect_oneshot_request(
uri="/consumers", uri="/consumers",
method='GET', method='GET',
@ -129,6 +158,10 @@ def test_checkconsumer(httpserver):
def test_setcurrentconsumer(): def test_setcurrentconsumer():
"""
Tests :func:'~communicator.Communicator.set_currentconsumer'
:return: None
"""
comm = communicator.Communicator( comm = communicator.Communicator(
currentconsumer="127.0.0.1", currentconsumer="127.0.0.1",
uuid=generateduuid) uuid=generateduuid)
@ -137,6 +170,11 @@ def test_setcurrentconsumer():
def test_learnconsumerlist(httpserver): def test_learnconsumerlist(httpserver):
"""
Tests :func:'~consumerlocator.ConsumerLocator.learnconsumerlist'
:param httpserver: simple HTTP server
:return: None
"""
httpserver.expect_request( httpserver.expect_request(
uri="/consumers", uri="/consumers",
method='GET', method='GET',
@ -155,6 +193,11 @@ def test_learnconsumerlist(httpserver):
def test_getcurrentconsumer(mocker): def test_getcurrentconsumer(mocker):
"""
Tests :func:'~consumerlocator.ConsumerLocator.getcurrentconsumer'
:param mocker:
:return: None
"""
mocker.patch('communicator.Communicator') mocker.patch('communicator.Communicator')
comm = communicator.Communicator( comm = communicator.Communicator(
currentconsumer="127.0.0.1", currentconsumer="127.0.0.1",
@ -165,6 +208,11 @@ def test_getcurrentconsumer(mocker):
def test_checkcurrentconsumer(httpserver): def test_checkcurrentconsumer(httpserver):
"""
Tests :func:'~consumerlocator.ConsumerLocator.checkcurrentconsumer'
:param httpserver: simple HTTP server
:return: None
"""
httpserver.expect_oneshot_request( httpserver.expect_oneshot_request(
uri="/consumers", uri="/consumers",
method='GET', method='GET',
@ -183,6 +231,11 @@ def test_checkcurrentconsumer(httpserver):
def test_updateconsumer(httpserver): def test_updateconsumer(httpserver):
"""
Tests :func:'~consumerlocator.ConsumerLocator.updateconsumer'
:param httpserver: simple HTTP server
:return: None
"""
httpserver.expect_oneshot_request( httpserver.expect_oneshot_request(
uri="/consumers", uri="/consumers",
method='GET', method='GET',
@ -202,6 +255,11 @@ def test_updateconsumer(httpserver):
def test_updateconsumerlist(httpserver): def test_updateconsumerlist(httpserver):
"""
Tests :func:'~consumerlocator.ConsumerLocator.updateconsumerlist'
:param httpserver: simple HTTP server
:return: None
"""
httpserver.expect_oneshot_request( httpserver.expect_oneshot_request(
uri="/consumers", uri="/consumers",
method='GET', method='GET',