mirror of
https://github.com/GNS3/gns3-server.git
synced 2026-11-03 14:01:15 +02:00
Extract the notification part of controller to a dedicated class
This commit is contained in:
10 files changed
+162
-106
No files matched your search
@@ -160,13 +160,13 @@ def test_connectNotification(compute, async_run):
|
||||
response.tp = aiohttp.MsgType.closed
|
||||
return response
|
||||
|
||||
compute._controller = MagicMock()
|
||||
compute._controller._notifications = MagicMock()
|
||||
compute._session = AsyncioMagicMock(return_value=ws_mock)
|
||||
compute._session.ws_connect = AsyncioMagicMock(return_value=ws_mock)
|
||||
ws_mock.receive = receive
|
||||
async_run(compute._connect_notification())
|
||||
|
||||
compute._controller.emit.assert_called_with('test', {'a': 1}, compute_id=compute.id, project_id='42')
|
||||
compute._controller.notification.emit.assert_called_with('test', {'a': 1}, compute_id=compute.id, project_id='42')
|
||||
assert compute._connected is False
|
||||
|
||||
|
||||
|
||||
@@ -157,47 +157,3 @@ def test_getProject(controller, async_run):
|
||||
with pytest.raises(aiohttp.web.HTTPNotFound):
|
||||
assert controller.get_project("dsdssd")
|
||||
|
||||
|
||||
def test_emit(controller, async_run):
|
||||
project1 = MagicMock()
|
||||
uuid1 = str(uuid.uuid4())
|
||||
controller._projects[uuid1] = project1
|
||||
|
||||
project2 = MagicMock()
|
||||
uuid2 = str(uuid.uuid4())
|
||||
controller._projects[uuid2] = project2
|
||||
|
||||
# Notif without project should be send to all projects
|
||||
controller.emit("test", {})
|
||||
assert project1.emit.called
|
||||
assert project2.emit.called
|
||||
|
||||
|
||||
def test_emit_to_project(controller, async_run):
|
||||
project1 = MagicMock()
|
||||
uuid1 = str(uuid.uuid4())
|
||||
controller._projects[uuid1] = project1
|
||||
|
||||
project2 = MagicMock()
|
||||
uuid2 = str(uuid.uuid4())
|
||||
controller._projects[uuid2] = project2
|
||||
|
||||
# Notif with project should be send to this project
|
||||
controller.emit("test", {}, project_id=uuid1)
|
||||
project1.emit.assert_called_with('test', {})
|
||||
assert not project2.emit.called
|
||||
|
||||
|
||||
def test_emit_to_project_not_exists(controller, async_run):
|
||||
project1 = MagicMock()
|
||||
uuid1 = str(uuid.uuid4())
|
||||
controller._projects[uuid1] = project1
|
||||
|
||||
project2 = MagicMock()
|
||||
uuid2 = str(uuid.uuid4())
|
||||
controller._projects[uuid2] = project2
|
||||
|
||||
# Notif with project should be send to this project
|
||||
controller.emit("test", {}, project_id="4444444")
|
||||
assert not project1.emit.called
|
||||
assert not project2.emit.called
|
||||
@@ -0,0 +1,55 @@
|
||||
#!/usr/bin/env python
|
||||
#
|
||||
# Copyright (C) 2016 GNS3 Technologies Inc.
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
import pytest
|
||||
|
||||
from gns3server.controller.notification import Notification
|
||||
from gns3server.controller.project import Project
|
||||
|
||||
|
||||
def test_emit_to_all(async_run, controller):
|
||||
"""
|
||||
Send an event to all if we don't have a project id in the event
|
||||
"""
|
||||
project = Project()
|
||||
notif = controller.notification
|
||||
with notif.queue(project) as queue:
|
||||
assert len(notif._listeners[project.id]) == 1
|
||||
async_run(queue.get(0.1)) # ping
|
||||
notif.emit('test', {})
|
||||
msg = async_run(queue.get(5))
|
||||
assert msg == ('test', {}, {})
|
||||
|
||||
assert len(notif._listeners[project.id]) == 0
|
||||
|
||||
|
||||
def test_emit_to_project(async_run, controller):
|
||||
"""
|
||||
Send an event to a project listeners
|
||||
"""
|
||||
project = Project()
|
||||
notif = controller.notification
|
||||
with notif.queue(project) as queue:
|
||||
assert len(notif._listeners[project.id]) == 1
|
||||
async_run(queue.get(0.1)) # ping
|
||||
# This event has not listener
|
||||
notif.emit('ignore', {}, project_id=42)
|
||||
notif.emit('test', {}, project_id=project.id)
|
||||
msg = async_run(queue.get(5))
|
||||
assert msg == ('test', {}, {})
|
||||
|
||||
assert len(notif._listeners[project.id]) == 0
|
||||
@@ -194,14 +194,3 @@ def test_getLink(async_run):
|
||||
with pytest.raises(aiohttp.web_exceptions.HTTPNotFound):
|
||||
project.get_link("test")
|
||||
|
||||
|
||||
def test_emit(async_run):
|
||||
project = Project()
|
||||
with project.queue() as queue:
|
||||
assert len(project._listeners) == 1
|
||||
async_run(queue.get(0.1)) # ping
|
||||
project.emit('test', {})
|
||||
notif = async_run(queue.get(5))
|
||||
assert notif == ('test', {}, {})
|
||||
|
||||
assert len(project._listeners) == 0
|
||||
Reference in new issue
Block a user