Skip to content

Commit

Permalink
Merge pull request #99 from maurerle/ruff-fixup
Browse files Browse the repository at this point in the history
add ruff linting to github action
  • Loading branch information
rcschrg authored Sep 9, 2024
2 parents 37805ac + 01e1ed8 commit bd36252
Show file tree
Hide file tree
Showing 22 changed files with 85 additions and 37 deletions.
11 changes: 8 additions & 3 deletions .github/workflows/test-mango.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: Test mango

on:
on:
push:
branches:
- main
Expand Down Expand Up @@ -37,8 +37,13 @@ jobs:
sudo apt update
sudo apt install --assume-yes mosquitto
sudo service mosquitto start
pip3 install pytest
pip3 install coverage
pip3 install pytest coverage ruff
- name: Lint with ruff
run: |
# stop the build if there are Python syntax errors or undefined names
source venv/bin/activate
ruff check .
ruff format --check .
- name: Test+Coverage
run: |
source venv/bin/activate
Expand Down
1 change: 1 addition & 0 deletions examples/distributed_clock/clock_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from typing import TypedDict

import numpy as np

from mango import Role, RoleAgent, create_container
from mango.messages.message import Performatives
from mango.util.clock import ExternalClock
Expand Down
3 changes: 1 addition & 2 deletions examples/distributed_clock/clock_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from typing import TypedDict

import pandas as pd
from serializer import mango_codec_factory
from tqdm import tqdm

from mango import Role, RoleAgent, create_container
Expand Down Expand Up @@ -65,7 +64,7 @@ async def clear_market(self):
self.demands.append(self.demand)
else:
logger.info("First opening does not have anything to clear")
price=0
price = 0
acl_metadata = {
"performative": Performatives.inform,
"sender_id": self.context.aid,
Expand Down
1 change: 1 addition & 0 deletions examples/rrule_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from datetime import datetime

from dateutil import rrule

from mango import Agent, create_container
from mango.util.clock import ExternalClock

Expand Down
4 changes: 3 additions & 1 deletion mango/modules/mqtt_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,9 @@ def conn(self, client, userdata, flags, reason_code, properties):
:return: None
"""

def on_disconnect(self, client, userdata, disconnect_flags, reason_code, properties):
def on_disconnect(
self, client, userdata, disconnect_flags, reason_code, properties
):
# pylint: disable=unused-argument
"""
Callback method on broker disconnect on paho mqtt framework
Expand Down
13 changes: 6 additions & 7 deletions mango/util/distributed_clock.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ async def send_current_time(self, time=None):
Args:
time (number, optional): The current time which is set. Defaults to None.
"""
time = time or self._scheduler.clock.time
time = time or self._scheduler.clock.time
await self.broadcast(time, add_futures=False)

async def wait_for_futures(self):
Expand All @@ -87,7 +87,7 @@ async def wait_for_futures(self):
# waits forever if manager was started first
# as answer is never received
await fut

async def wait_all_online(self):
"""
sends a broadcast to ask for the next event to all expected addresses.
Expand All @@ -109,14 +109,13 @@ async def wait_all_online(self):
else:
all_online = True


async def get_next_event(self):
'''Get the next event from the scheduler by requesting all known clock agents'''
"""Get the next event from the scheduler by requesting all known clock agents"""
self.schedules = []
await self.broadcast("next_event")
await asyncio.sleep(0)
await self.wait_for_futures()

# wait for our container too
await self.wait_all_done()
next_activity = self._scheduler.clock.get_next_activity()
Expand All @@ -142,11 +141,10 @@ async def distribute_time(self, time=None):
Waits until the current container is done.
Brodcasts the new time to all the other clock agents.
Thn awaits until the work in the other agents is done and their next event is received.
Args:
time (number, optional): The new time which is set. Defaults to None.
Returns:
number or None: The time at which the next event happens
"""
Expand All @@ -170,6 +168,7 @@ def handle_message(self, content: float, meta):
if not self.stopped.done():
self.stopped.set_result(True)
elif content == "next_event":

async def wait_done():
await self.wait_all_done()

Expand Down
2 changes: 0 additions & 2 deletions ruff.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
target-version = "py38"

src = ["crawler"]

[lint]
# Enable Pyflakes (`F`) and a subset of the pycodestyle (`E`) codes by default.
# Unlike Flake8, Ruff doesn't enable pycodestyle warnings (`W`) or
Expand Down
4 changes: 2 additions & 2 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool:pytest]

markers =
markers =
mqtt

asyncio_default_fixture_loop_scope = function
asyncio_default_fixture_loop_scope = function
2 changes: 1 addition & 1 deletion tests/integration_tests/test_distributed_clock.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import asyncio

import pytest

from mango import create_container
from mango.messages.codecs import JSON
from mango.util.clock import ExternalClock
from mango.util.distributed_clock import DistributedClockAgent, DistributedClockManager
from mango.util.termination_detection import tasks_complete_or_sleeping

JSON_CODEC = JSON()

Expand Down
3 changes: 2 additions & 1 deletion tests/integration_tests/test_message_roundtrip.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import asyncio

import mango.container.factory as container_factory
import pytest

import mango.container.factory as container_factory
from mango.agent.core import Agent
from mango.messages.codecs import JSON, PROTOBUF, FastJSON

Expand Down
3 changes: 2 additions & 1 deletion tests/integration_tests/test_message_roundtrip_mp.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import asyncio

import mango.container.factory as container_factory
import pytest

import mango.container.factory as container_factory
from mango.agent.core import Agent


Expand Down
57 changes: 44 additions & 13 deletions tests/integration_tests/test_single_container_termination.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
import asyncio

import pytest

from mango import Agent, create_container
from mango.util.clock import ExternalClock
from mango.util.termination_detection import tasks_complete_or_sleeping
from mango.util.distributed_clock import DistributedClockAgent, DistributedClockManager
from mango.util.termination_detection import tasks_complete_or_sleeping


class Caller(Agent):
def __init__(self, container, receiver_addr, receiver_id, send_response_messages=False, max_count=100, schedule_timestamp=False):
def __init__(
self,
container,
receiver_addr,
receiver_id,
send_response_messages=False,
max_count=100,
schedule_timestamp=False,
):
super().__init__(container)
self.schedule_timestamp_task(
coroutine=self.send_hello_world(receiver_addr, receiver_id),
Expand All @@ -27,15 +36,18 @@ async def send_hello_world(self, receiver_addr, receiver_id):

async def send_ordered(self, meta):
await self.send_acl_message(
receiver_addr=meta["sender_addr"], receiver_id=meta["sender_id"], content=self.i
receiver_addr=meta["sender_addr"],
receiver_id=meta["sender_id"],
content=self.i,
)


def handle_message(self, content, meta):
self.i += 1
if self.i < self.max_count and self.send_response_messages:
if self.schedule_timestamp:
self.schedule_timestamp_task(self.send_ordered(meta), self.current_timestamp+5)
self.schedule_timestamp_task(
self.send_ordered(meta), self.current_timestamp + 5
)
else:
self.schedule_instant_task(self.send_ordered(meta))
elif not self.done.done():
Expand Down Expand Up @@ -122,9 +134,15 @@ async def distribute_ping_pong_test(connection_type, codec=None, max_count=100):
container_man, receiver_clock_addresses=[(repl_addr, "clock_agent")]
)
receiver = Receiver(container_ag, init_addr, "agent0")
caller = Caller(container_man, repl_addr, receiver.aid, send_response_messages=True, max_count=max_count)
caller = Caller(
container_man,
repl_addr,
receiver.aid,
send_response_messages=True,
max_count=max_count,
)

clock_man.set_time(clock_man.time+5)
clock_man.set_time(clock_man.time + 5)

# we do not have distributed termination detection yet in core
assert caller.i < caller.max_count
Expand All @@ -138,7 +156,10 @@ async def distribute_ping_pong_test(connection_type, codec=None, max_count=100):
container_ag.shutdown(),
)

async def distribute_ping_pong_test_timestamp(connection_type, codec=None, max_count=10):

async def distribute_ping_pong_test_timestamp(
connection_type, codec=None, max_count=10
):
init_addr = ("localhost", 1555) if connection_type == "tcp" else "c1"
repl_addr = ("localhost", 1556) if connection_type == "tcp" else "c2"

Expand Down Expand Up @@ -177,20 +198,28 @@ async def distribute_ping_pong_test_timestamp(connection_type, codec=None, max_c
container_man, receiver_clock_addresses=[(repl_addr, "clock_agent")]
)
receiver = Receiver(container_ag, init_addr, "agent0")
caller = Caller(container_man, repl_addr, receiver.aid, send_response_messages=True, max_count=max_count, schedule_timestamp=True)
caller = Caller(
container_man,
repl_addr,
receiver.aid,
send_response_messages=True,
max_count=max_count,
schedule_timestamp=True,
)

# we do not have distributed termination detection yet in core
assert caller.i < caller.max_count

import time

tt = 0
if isinstance(clock_man, ExternalClock):
for i in range(caller.max_count):
await tasks_complete_or_sleeping(container_man)
t = time.time()
await clock_manager.send_current_time()
next_event = await clock_manager.get_next_event()
tt += time.time()-t
tt += time.time() - t

clock_man.set_time(next_event)

Expand All @@ -209,21 +238,22 @@ async def distribute_ping_pong_test_timestamp(connection_type, codec=None, max_c
async def test_distribute_ping_pong_tcp():
await distribute_ping_pong_test("tcp")


@pytest.mark.asyncio
async def test_distribute_ping_pong_mqtt():
await distribute_ping_pong_test("mqtt")


@pytest.mark.asyncio
async def test_distribute_ping_pong_ts_tcp():
await distribute_ping_pong_test_timestamp("tcp")


@pytest.mark.asyncio
async def test_distribute_ping_pong_ts_mqtt():
await distribute_ping_pong_test_timestamp("mqtt")




async def distribute_time_test_case(connection_type, codec=None):
init_addr = ("localhost", 1555) if connection_type == "tcp" else "c1"
repl_addr = ("localhost", 1556) if connection_type == "tcp" else "c2"
Expand Down Expand Up @@ -265,7 +295,6 @@ async def distribute_time_test_case(connection_type, codec=None):
receiver = Receiver(container_ag, init_addr, "agent0")
caller = Caller(container_man, repl_addr, receiver.aid)


assert receiver._scheduler.clock.time == 0
# first synchronize the clock to the receiver
next_event = await clock_manager.distribute_time(clock_man.time)
Expand Down Expand Up @@ -302,6 +331,7 @@ async def distribute_time_test_case(connection_type, codec=None):
container_ag.shutdown(),
)


async def send_current_time_test_case(connection_type, codec=None):
init_addr = ("localhost", 1555) if connection_type == "tcp" else "c1"
repl_addr = ("localhost", 1556) if connection_type == "tcp" else "c2"
Expand Down Expand Up @@ -394,6 +424,7 @@ async def test_distribute_time_mqtt():
async def test_send_current_time_tcp():
await send_current_time_test_case("tcp")


@pytest.mark.asyncio
async def test_send_current_time_mqtt():
await send_current_time_test_case("mqtt")
1 change: 1 addition & 0 deletions tests/unit_tests/clock/test_external_clock.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import time

import pytest

from mango.util.clock import AsyncioClock, ExternalClock
from mango.util.scheduling import Scheduler

Expand Down
1 change: 1 addition & 0 deletions tests/unit_tests/container/test_mp.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import asyncio

import pytest

from mango import Agent, create_container


Expand Down
1 change: 1 addition & 0 deletions tests/unit_tests/container/test_tcp.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import asyncio

import pytest

from mango import create_container
from mango.container.protocol import ContainerProtocol
from mango.container.tcp import TCPConnectionPool
Expand Down
1 change: 1 addition & 0 deletions tests/unit_tests/core/test_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from typing import Any, Dict

import pytest

from mango import create_container
from mango.agent.core import Agent

Expand Down
3 changes: 2 additions & 1 deletion tests/unit_tests/core/test_container.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import mango.container.factory as container_factory
import pytest

import mango.container.factory as container_factory
from mango.agent.core import Agent


Expand Down
3 changes: 2 additions & 1 deletion tests/unit_tests/core/test_external_scheduling_container.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import asyncio
from typing import Any, Dict

import mango.container.factory as container_factory
import pytest

import mango.container.factory as container_factory
from mango.agent.core import Agent
from mango.container.external_coupling import ExternalAgentMessage
from mango.container.factory import EXTERNAL_CONNECTION
Expand Down
1 change: 1 addition & 0 deletions tests/unit_tests/messages/test_codecs.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from dataclasses import dataclass

import pytest

from mango.messages.codecs import (
JSON,
PROTOBUF,
Expand Down
Loading

0 comments on commit bd36252

Please sign in to comment.