1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-12-02 17:06:09 +00:00
webvirtcloud/computes/tests/test_list_hosts.py
catborise e264104e85 fix(computes): the Computes page reads its hosts side by side, within one wait
The page connected to its hosts one after another: each host that did not
answer added its connect timeout, so a few such hosts made the page take
half a minute, and a host that hung while answering held it until the
worker was killed. The hosts are now read on their own threads, and a host
not read within ten seconds is shown as not answering.
2026-11-12 09:19:23 +03:00

45 lines
2.1 KiB
Python

"""The Computes page reads its hosts side by side: a host that does not
answer holds back only itself, and only up to the page's wait."""
import threading
import time
from unittest.mock import patch
from django.contrib.auth import get_user_model
from django.test import TestCase
from django.urls import reverse
from computes.models import Compute
class ListHostsTests(TestCase):
def setUp(self):
self.client.force_login(get_user_model().objects.create_superuser("lh-admin", "lh@example.com", "pw"))
for number, name in enumerate(("fast", "slow-1", "slow-2")):
Compute.objects.create(name=name, hostname="192.0.2.1", login="wvc", password="", type=1,
host_uuid=f"{number}" * 8 + "-0000-0000-0000-000000000000")
self.release = threading.Event()
self.addCleanup(self.release.set)
def read(self, compute):
if compute.name.startswith("slow"):
self.release.wait(5)
return True, None, None
def test_hosts_that_do_not_answer_are_waited_for_together_and_shown_so(self):
with patch("computes.views._read_host", side_effect=self.read), patch("computes.views.LIST_WAIT", 0.5):
started = time.monotonic()
response = self.client.get(reverse("computes"))
took = time.monotonic() - started
self.assertLess(took, 2) # one wait for both, not one each
shown = {compute.name: (compute.status, compute.connection_error) for compute in response.context["computes"]}
self.assertEqual(shown["fast"], (True, None))
self.assertEqual(shown["slow-1"], (False, "The host did not answer within 0.5 seconds"))
self.assertEqual(response.context["totals"]["connected"], 1)
def test_a_host_still_being_read_is_not_read_again(self):
with patch("computes.views._read_host", side_effect=self.read) as read, patch("computes.views.LIST_WAIT", 0.2):
self.client.get(reverse("computes"))
self.client.get(reverse("computes"))
self.assertEqual(sorted(call.args[0].name for call in read.call_args_list),
["fast", "fast", "slow-1", "slow-2"])