Skip to content

Commit

Permalink
autoscaler tests fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Kye committed Nov 15, 2023
1 parent e7e2004 commit 12970e5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 18 deletions.
9 changes: 0 additions & 9 deletions example.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from swarms.tools.tool import tool
from swarms.models import OpenAIChat
from swarms.structs import Flow

Expand All @@ -11,14 +10,6 @@
)


@tool
def search_api(query: str):
"""
This is a search API, you can search the web for information.
"""
pass


## Initialize the workflow
flow = Flow(
llm=llm,
Expand Down
28 changes: 19 additions & 9 deletions tests/swarms/autoscaler.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
from unittest.mock import patch
from swarms.swarms.autoscaler import AutoScaler, Worker
from swarms.swarms.autoscaler import AutoScaler
from swarms.models import OpenAIChat
from swarms.structs import Flow

llm = OpenAIChat()

flow = Flow(
llm=llm,
max_loops=2,
dashboard=True,
)


def test_autoscaler_initialization():
Expand All @@ -8,7 +18,7 @@ def test_autoscaler_initialization():
scale_up_factor=2,
idle_threshold=0.1,
busy_threshold=0.8,
agent=Worker,
agent=flow,
)
assert isinstance(autoscaler, AutoScaler)
assert autoscaler.scale_up_factor == 2
Expand All @@ -18,44 +28,44 @@ def test_autoscaler_initialization():


def test_autoscaler_add_task():
autoscaler = AutoScaler(agent=Worker)
autoscaler = AutoScaler(agent=flow)
autoscaler.add_task("task1")
assert autoscaler.task_queue.qsize() == 1


def test_autoscaler_scale_up():
autoscaler = AutoScaler(initial_agents=5, scale_up_factor=2, agent=Worker)
autoscaler = AutoScaler(initial_agents=5, scale_up_factor=2, agent=flow)
autoscaler.scale_up()
assert len(autoscaler.agents_pool) == 10


def test_autoscaler_scale_down():
autoscaler = AutoScaler(initial_agents=5, agent=Worker)
autoscaler = AutoScaler(initial_agents=5, agent=flow)
autoscaler.scale_down()
assert len(autoscaler.agents_pool) == 4


@patch("your_module.AutoScaler.scale_up")
@patch("your_module.AutoScaler.scale_down")
def test_autoscaler_monitor_and_scale(mock_scale_down, mock_scale_up):
autoscaler = AutoScaler(initial_agents=5, agent=Worker)
autoscaler = AutoScaler(initial_agents=5, agent=flow)
autoscaler.add_task("task1")
autoscaler.monitor_and_scale()
mock_scale_up.assert_called_once()
mock_scale_down.assert_called_once()


@patch("your_module.AutoScaler.monitor_and_scale")
@patch("your_module.Worker.run")
@patch("your_module.flow.run")
def test_autoscaler_start(mock_run, mock_monitor_and_scale):
autoscaler = AutoScaler(initial_agents=5, agent=Worker)
autoscaler = AutoScaler(initial_agents=5, agent=flow)
autoscaler.add_task("task1")
autoscaler.start()
mock_run.assert_called_once()
mock_monitor_and_scale.assert_called_once()


def test_autoscaler_del_agent():
autoscaler = AutoScaler(initial_agents=5, agent=Worker)
autoscaler = AutoScaler(initial_agents=5, agent=flow)
autoscaler.del_agent()
assert len(autoscaler.agents_pool) == 4

0 comments on commit 12970e5

Please sign in to comment.