Skip to content

Commit

Permalink
Merge pull request #11542 from bjester/multitudes-of-instances
Browse files Browse the repository at this point in the history
There can be more than one network location for an instance ID
  • Loading branch information
bjester authored Nov 17, 2023
2 parents f6e02e4 + a4d61a1 commit 6986711
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
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

0 comments on commit 6986711

Please sign in to comment.