-
Notifications
You must be signed in to change notification settings - Fork 753
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Decrease time-to-sync for learn-only devices joining the network #11271
Merged
bjester
merged 6 commits into
learningequality:release-v0.16.x
from
vkWeb:time-decrease
Sep 26, 2023
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
8338d8b
Decrease time to sync
vkWeb 0243928
Add another field to check on to
vkWeb 0fbacd7
Don't access db
vkWeb f425a79
Fix outdated test
vkWeb ab03b39
Rectify update_instance as well with tests
vkWeb 481c37b
Add tests and a bit clean
vkWeb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,19 @@ | ||
import mock | ||
from django.test import TransactionTestCase | ||
|
||
from ..utils.network.broadcast import KolibriBroadcast | ||
from ..utils.network.broadcast import KolibriInstance | ||
from ..utils.network.search import NetworkLocationListener | ||
|
||
from kolibri.core.tasks.job import Priority | ||
|
||
MOCK_INTERFACE_IP = "111.222.111.222" | ||
MOCK_PORT = 555 | ||
MOCK_ID = "abba" | ||
SEARCH_MODULE = "kolibri.core.discovery.utils.network.search." | ||
DYNAMIC_NETWORK_LOCATION_TASK_PRIORITY_METHOD = ( | ||
SEARCH_MODULE | ||
+ "NetworkLocationListener._get_dynamic_network_location_task_priority" | ||
) | ||
|
||
|
||
class NetworkLocationListenerTestCase(TransactionTestCase): | ||
|
@@ -24,34 +29,82 @@ def setUp(self): | |
"instance_id": MOCK_ID, | ||
}, | ||
) | ||
self.mock_broadcast = mock.MagicMock(id="abc123") | ||
self.listener = NetworkLocationListener(self.mock_broadcast) | ||
self.broadcast_instance = KolibriInstance( | ||
"abcd", | ||
ip=MOCK_INTERFACE_IP, | ||
port=MOCK_PORT, | ||
device_info={ | ||
"instance_id": "abcd", | ||
}, | ||
) | ||
|
||
self.broadcast = KolibriBroadcast(instance=self.broadcast_instance) | ||
self.broadcast.id = "abc123" | ||
|
||
self.listener = NetworkLocationListener(self.broadcast) | ||
|
||
@mock.patch(SEARCH_MODULE + "reset_connection_states.enqueue") | ||
def test_register_instance(self, mock_enqueue): | ||
self.listener.register_instance(self.instance) | ||
mock_enqueue.assert_called_once_with(args=(self.mock_broadcast.id,)) | ||
mock_enqueue.assert_called_once_with(args=(self.broadcast.id,)) | ||
|
||
def test_dynamic_network_location_task_priority_self_no_lod(self): | ||
# The current device is not a LOD. | ||
self.broadcast_instance.device_info["subset_of_users_device"] = False | ||
|
||
priority = self.listener._get_dynamic_network_location_task_priority( | ||
self.instance | ||
) | ||
self.assertEqual(priority, Priority.HIGH) | ||
|
||
def test_dynamic_network_location_task_priority_self_discovered_both_lod(self): | ||
# The current device is a LOD. | ||
self.broadcast_instance.device_info["subset_of_users_device"] = True | ||
# The discovered device is LOD as well. | ||
self.instance.device_info["subset_of_users_device"] = True | ||
|
||
priority = self.listener._get_dynamic_network_location_task_priority( | ||
self.instance | ||
) | ||
self.assertEqual(priority, Priority.REGULAR) | ||
|
||
def test_dynamic_network_location_task_priority_self_lod_discovered_not_lod(self): | ||
# The current device is a LOD. | ||
self.broadcast_instance.device_info["subset_of_users_device"] = True | ||
# The discovered device is not a LOD. | ||
self.instance.device_info["subset_of_users_device"] = False | ||
|
||
priority = self.listener._get_dynamic_network_location_task_priority( | ||
self.instance | ||
) | ||
self.assertEqual(priority, Priority.HIGH) | ||
|
||
@mock.patch(SEARCH_MODULE + "add_dynamic_network_location.enqueue") | ||
def test_add_instance(self, mock_enqueue): | ||
@mock.patch(DYNAMIC_NETWORK_LOCATION_TASK_PRIORITY_METHOD) | ||
def test_add_instance(self, mock_priority_method, mock_enqueue): | ||
self.listener.add_instance(self.instance) | ||
mock_priority_method.assert_called_once_with(self.instance) | ||
mock_enqueue.assert_called_once_with( | ||
job_id="9e89d3ea5256721c9cd631eac36feafe", | ||
args=(self.mock_broadcast.id, self.instance.to_dict()), | ||
args=(self.broadcast.id, self.instance.to_dict()), | ||
priority=mock_priority_method(), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. using |
||
) | ||
|
||
@mock.patch(SEARCH_MODULE + "add_dynamic_network_location.enqueue") | ||
def test_update_instance(self, mock_enqueue): | ||
@mock.patch(DYNAMIC_NETWORK_LOCATION_TASK_PRIORITY_METHOD) | ||
def test_update_instance(self, mock_priority_method, mock_enqueue): | ||
self.listener.update_instance(self.instance) | ||
mock_priority_method.assert_called_once_with(self.instance) | ||
mock_enqueue.assert_called_once_with( | ||
job_id="9e89d3ea5256721c9cd631eac36feafe", | ||
args=(self.mock_broadcast.id, self.instance.to_dict()), | ||
args=(self.broadcast.id, self.instance.to_dict()), | ||
priority=mock_priority_method(), | ||
) | ||
|
||
vkWeb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
@mock.patch(SEARCH_MODULE + "remove_dynamic_network_location.enqueue") | ||
def test_remove_instance(self, mock_enqueue): | ||
self.listener.remove_instance(self.instance) | ||
mock_enqueue.assert_called_once_with( | ||
job_id="c5e88d1cb4a342ad3d23081022248fbc", | ||
args=(self.mock_broadcast.id, self.instance.to_dict()), | ||
args=(self.broadcast.id, self.instance.to_dict()), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would prefer to keep using the mock, to keep the unit testing scope limited to the listeners. You can use
spec
orspec_set
on the mock to constrain its usage to align with a particular class, in this caseKolibriBroadcast
. All that said, the broadcast class doesn't do much withoutstart_broadcast
called.