Skip to content
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

There can be more than one network location for an instance ID #11542

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions kolibri/core/content/test/utils/test_content_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ def _facility(dataset_id=None):


class BaseTestCase(TestCase):
multi_db = True

def _create_sync_and_network_location(
self, sync_overrides=None, location_overrides=None
):
Expand Down Expand Up @@ -642,6 +644,25 @@ def test_multiple_peers__version_filter(self):
self.assertEqual(peers[0].id, network_location1.id)
self.assertEqual(peers[1].id, network_location2.id)

def test_multiple_locations__same_instance_id(self):
dynamic_location = self._create_network_location(
connection_status=ConnectionStatus.Unknown,
)
static_location = self._create_network_location(
location_type="static",
instance_id=dynamic_location.instance_id,
)
self.assertEqual(dynamic_location.instance_id, static_location.instance_id)

instance = PreferredDevices(
instance_ids=[
dynamic_location.instance_id,
],
)
peers = list(instance)
self.assertEqual(len(peers), 1)
self.assertEqual(peers[0].id, static_location.id)


class PreferredDevicesWithClientTestCase(BaseTestCase):
def setUp(self):
Expand Down
20 changes: 16 additions & 4 deletions kolibri/core/content/utils/content_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,11 +246,23 @@ def _get_and_validate_peer(self, instance_id):
:return: The NetworkLocation object, or None if it is not available or does not match the
validation conditions
"""
try:
peer = NetworkLocation.objects.get(
instance_id=_uuid_to_hex(instance_id), **self._filters
peer = (
NetworkLocation.objects.annotate(
okay=Case(
When(
connection_status=ConnectionStatus.Okay,
then=Value(True),
),
default=Value(False),
output_field=BooleanField(),
),
)
except NetworkLocation.DoesNotExist:
.order_by("-okay")
.filter(instance_id=_uuid_to_hex(instance_id), **self._filters)
.first()
)

if not peer:
return None

# if we're on a metered connection, we only want to download from local peers
Expand Down