1
0
mirror of https://github.com/GNS3/gns3-server synced 2024-11-28 11:18:11 +00:00

Avoid too much ressources usage during tests

This commit is contained in:
Julien Duponchelle 2016-08-30 09:58:37 +02:00
parent 919355abd8
commit e0c5fdcf1c
No known key found for this signature in database
GPG Key ID: CE8B29639E07F5E8
3 changed files with 24 additions and 18 deletions

View File

@ -208,13 +208,14 @@ class Controller:
return Config.instance().get_section_config("Server").getboolean("controller") return Config.instance().get_section_config("Server").getboolean("controller")
@asyncio.coroutine @asyncio.coroutine
def add_compute(self, compute_id=None, name=None, force=False, **kwargs): def add_compute(self, compute_id=None, name=None, force=False, connect=True, **kwargs):
""" """
Add a server to the dictionary of compute servers controlled by this controller Add a server to the dictionary of compute servers controlled by this controller
:param compute_id: Compute server identifier :param compute_id: Compute server identifier
:param name: Compute name :param name: Compute name
:param force: True skip security check :param force: True skip security check
:param connect: True connect to the compute immediately
:param kwargs: See the documentation of Compute :param kwargs: See the documentation of Compute
""" """
if compute_id not in self._computes: if compute_id not in self._computes:
@ -230,11 +231,13 @@ class Controller:
compute = Compute(compute_id=compute_id, controller=self, name=name, **kwargs) compute = Compute(compute_id=compute_id, controller=self, name=name, **kwargs)
self._computes[compute.id] = compute self._computes[compute.id] = compute
self.save() self.save()
yield from compute.connect() if connect:
yield from compute.connect()
self.notification.emit("compute.created", compute.__json__()) self.notification.emit("compute.created", compute.__json__())
return compute return compute
else: else:
yield from self._computes[compute_id].connect() if connect:
yield from self._computes[compute_id].connect()
self.notification.emit("compute.updated", self._computes[compute_id].__json__()) self.notification.emit("compute.updated", self._computes[compute_id].__json__())
return self._computes[compute_id] return self._computes[compute_id]

View File

@ -21,6 +21,7 @@ import asyncio
import socket import socket
import json import json
import uuid import uuid
import sys
import os import os
import io import io
@ -348,8 +349,9 @@ class Compute:
try: try:
response = yield from self._run_http_query("GET", "/capabilities") response = yield from self._run_http_query("GET", "/capabilities")
except (aiohttp.errors.ClientOSError, aiohttp.errors.ClientRequestError, aiohttp.ClientResponseError): except (aiohttp.errors.ClientOSError, aiohttp.errors.ClientRequestError, aiohttp.ClientResponseError):
# Try to reconnect after 2 seconds if server unavailable # Try to reconnect after 2 seconds if server unavailable only if not during tests (otherwise we create a ressources usage bomb)
asyncio.get_event_loop().call_later(2, lambda: asyncio.async(self.connect())) if not hasattr(sys, "_called_from_test") or not sys._called_from_test:
asyncio.get_event_loop().call_later(2, lambda: asyncio.async(self.connect()))
return return
if "version" not in response.json: if "version" not in response.json:
@ -392,8 +394,9 @@ class Compute:
if self._ws: if self._ws:
yield from self._ws.close() yield from self._ws.close()
# Try to reconnect after 1 seconds if server unavailable # Try to reconnect after 1 seconds if server unavailable only if not during tests (otherwise we create a ressources usage bomb)
asyncio.get_event_loop().call_later(1, lambda: asyncio.async(self.connect())) if not hasattr(sys, "_called_from_test") or not sys._called_from_test:
asyncio.get_event_loop().call_later(1, lambda: asyncio.async(self.connect()))
self._ws = None self._ws = None
self._cpu_usage_percent = None self._cpu_usage_percent = None
self._memory_usage_percent = None self._memory_usage_percent = None

View File

@ -141,26 +141,26 @@ def test_isEnabled(controller):
def test_add_compute(controller, controller_config_path, async_run): def test_add_compute(controller, controller_config_path, async_run):
controller._notification = MagicMock() controller._notification = MagicMock()
c = async_run(controller.add_compute(compute_id="test1")) c = async_run(controller.add_compute(compute_id="test1", connect=False))
controller._notification.emit.assert_called_with("compute.created", c.__json__()) controller._notification.emit.assert_called_with("compute.created", c.__json__())
assert len(controller.computes) == 1 assert len(controller.computes) == 1
async_run(controller.add_compute(compute_id="test1")) async_run(controller.add_compute(compute_id="test1", connect=False))
controller._notification.emit.assert_called_with("compute.updated", c.__json__()) controller._notification.emit.assert_called_with("compute.updated", c.__json__())
assert len(controller.computes) == 1 assert len(controller.computes) == 1
async_run(controller.add_compute(compute_id="test2")) async_run(controller.add_compute(compute_id="test2", connect=False))
assert len(controller.computes) == 2 assert len(controller.computes) == 2
def test_addDuplicateCompute(controller, controller_config_path, async_run): def test_addDuplicateCompute(controller, controller_config_path, async_run):
controller._notification = MagicMock() controller._notification = MagicMock()
c = async_run(controller.add_compute(compute_id="test1", name="Test")) c = async_run(controller.add_compute(compute_id="test1", name="Test", connect=False))
assert len(controller.computes) == 1 assert len(controller.computes) == 1
with pytest.raises(aiohttp.web.HTTPConflict): with pytest.raises(aiohttp.web.HTTPConflict):
async_run(controller.add_compute(compute_id="test2", name="Test")) async_run(controller.add_compute(compute_id="test2", name="Test", connect=False))
def test_deleteCompute(controller, controller_config_path, async_run): def test_deleteCompute(controller, controller_config_path, async_run):
c = async_run(controller.add_compute(compute_id="test1")) c = async_run(controller.add_compute(compute_id="test1", connect=False))
assert len(controller.computes) == 1 assert len(controller.computes) == 1
controller._notification = MagicMock() controller._notification = MagicMock()
c._connected = True c._connected = True
@ -177,7 +177,7 @@ def test_deleteComputeProjectOpened(controller, controller_config_path, async_ru
""" """
When you delete a compute the project using it are close When you delete a compute the project using it are close
""" """
c = async_run(controller.add_compute(compute_id="test1")) c = async_run(controller.add_compute(compute_id="test1", connect=False))
c.post = AsyncioMagicMock() c.post = AsyncioMagicMock()
assert len(controller.computes) == 1 assert len(controller.computes) == 1
@ -205,7 +205,7 @@ def test_deleteComputeProjectOpened(controller, controller_config_path, async_ru
def test_addComputeConfigFile(controller, controller_config_path, async_run): def test_addComputeConfigFile(controller, controller_config_path, async_run):
async_run(controller.add_compute(compute_id="test1", name="Test")) async_run(controller.add_compute(compute_id="test1", name="Test", connect=False))
assert len(controller.computes) == 1 assert len(controller.computes) == 1
with open(controller_config_path) as f: with open(controller_config_path) as f:
data = json.load(f) data = json.load(f)
@ -223,7 +223,7 @@ def test_addComputeConfigFile(controller, controller_config_path, async_run):
def test_getCompute(controller, async_run): def test_getCompute(controller, async_run):
compute = async_run(controller.add_compute(compute_id="test1")) compute = async_run(controller.add_compute(compute_id="test1", connect=False))
assert controller.get_compute("test1") == compute assert controller.get_compute("test1") == compute
with pytest.raises(aiohttp.web.HTTPNotFound): with pytest.raises(aiohttp.web.HTTPNotFound):
@ -231,7 +231,7 @@ def test_getCompute(controller, async_run):
def test_has_compute(controller, async_run): def test_has_compute(controller, async_run):
compute = async_run(controller.add_compute(compute_id="test1")) compute = async_run(controller.add_compute(compute_id="test1", connect=False))
assert controller.has_compute("test1") assert controller.has_compute("test1")
assert not controller.has_compute("test2") assert not controller.has_compute("test2")
@ -311,7 +311,7 @@ def test_start_vm(controller, async_run):
def test_stop(controller, async_run): def test_stop(controller, async_run):
c = async_run(controller.add_compute(compute_id="test1")) c = async_run(controller.add_compute(compute_id="test1", connect=False))
c._connected = True c._connected = True
async_run(controller.stop()) async_run(controller.stop())
assert c.connected is False assert c.connected is False