Skip to content

Instantly share code, notes, and snippets.

@otykhonruk
Created August 13, 2016 23:08
Show Gist options
  • Save otykhonruk/3657355d65fc9eaf020ce6a1d2a0bcd2 to your computer and use it in GitHub Desktop.
Save otykhonruk/3657355d65fc9eaf020ce6a1d2a0bcd2 to your computer and use it in GitHub Desktop.
from django.db.models import F
from django.core.files.uploadedfile import SimpleUploadedFile
from django.test import TestCase
from Core.models import Picture
class RaceConditionDemo(TestCase):
def setUp(self):
fakepic = SimpleUploadedFile('sample_pic.jpg', b'__content__')
pic = Picture.objects.create(key='q1w2e3r4', picture=fakepic)
self.pk = pic.pk
def get_view_count(self):
return Picture.objects.values_list('viewCounter', flat=True)\
.get(pk=self.pk)
def test_simultaneous_access_incorrect(self):
# thread 1
ref1 = Picture.objects.get(pk=self.pk)
ref1.viewCounter += 1
# thread 2
ref2 = Picture.objects.get(pk=self.pk)
ref2.viewCounter += 1
# thread 1
ref1.save()
# update core_picture set viewCounter=1
# thread 2
ref2.save()
# update core_picture set viewCounter=1
nviews = self.get_view_count()
self.assertEquals(nviews, 2)
def test_simultaneous_access_correct(self):
# thread 1
ref1 = Picture.objects.get(pk=self.pk)
ref1.viewCounter = F('viewCounter') + 1
# thread 2
ref2 = Picture.objects.get(pk=self.pk)
ref2.viewCounter = F('viewCounter') + 1
# thread 1
ref1.save()
# update core_picture set viewCounter=viewCounter + 1
# thread 2
ref2.save()
# update core_picture set viewCounter=viewCounter + 1
nviews = self.get_view_count()
self.assertEquals(nviews, 2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment