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

Sync queue updates #8342

Merged
Merged
7 changes: 5 additions & 2 deletions kolibri/core/public/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,11 @@ def check_queue(self, request, pk=None):
DELAYED_SYNC / 2,
)
last_activity = timezone.now() - datetime.timedelta(minutes=5)
queue_object = SyncQueue.objects.filter(id=pk).first()

if pk is not None:
queue_object = SyncQueue.objects.filter(id=pk).first()
else:
queue_object = SyncQueue.objects.filter(user_id=user).first()

# Default the position to the total queue size, so that
# if the id does not exist, send them to the back of the queue
Expand All @@ -234,7 +238,6 @@ def check_queue(self, request, pk=None):
# that were made before this request. If pk is None or the queue
# has expired (3 minutes), we will set the position to the length of the
# queue.
queue_object = SyncQueue.objects.get(pk=pk)
before_client = SyncQueue.objects.filter(datetime__lt=queue_object.datetime)
pos = before_client.count()

Expand Down
27 changes: 27 additions & 0 deletions kolibri/core/public/test/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,33 @@ def test_create_full_queue_should_queue(self):
SyncQueue.objects.filter(id=queue_id, user_id=self.learner.id).exists()
)

def test_create_full_queue_user_already_queued_should_resume(self):
for i in range(0, MAX_CONCURRENT_SYNCS):
learner = FacilityUser.objects.create(
username="test{}".format(i),
password="***",
facility=self.facility,
)
SyncQueue.objects.create(
user_id=learner.id,
keep_alive=10,
)
queue = SyncQueue.objects.create(user_id=self.learner.id, keep_alive=10)
time.sleep(1)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

have I ever said how much I hate adding sleep to the tests?... I know this is a bad windows behaviour so one more reason not-to-love that OS
btw, this discussion is related https://stackoverflow.com/questions/85451/pythons-time-clock-vs-time-time-accuracy

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I am not a huge fan, but it seems like the only way :/

response = self.client.post(
reverse("kolibri:core:syncqueue-list"),
data={"user": self.learner.id},
format="json",
)
data = response.json()
self.assertEqual(response.status_code, 200)
self.assertEqual(data["action"], QUEUED)
self.assertTrue(
UserSyncStatus.objects.filter(user=self.learner, queued=True).exists()
)
queue_id = data["id"]
self.assertEqual(queue_id, queue.id)

def test_update_soud(self):
settings = DeviceSettings.objects.get()
settings.subset_of_users_device = True
Expand Down