diff --git a/store/stresstest.py b/store/stresstest.py index 84875603a85bab68be9fc176d5f4f5378255ecac..c72a58dd288465683dce71c410bbc9ecc50aef1f 100644 --- a/store/stresstest.py +++ b/store/stresstest.py @@ -31,23 +31,29 @@ class StressTester(Thread): product = self.products[randint(0, len(self.products) - 1)] product = list(Product.objects.filter(id=product))[0] try: - self.buy(product.id) + for i in range(0, randint(0, 10)): + self.buy(product.id) except UserNotEnoughMoney: charge_amount = Decimal(str(randint(ceil(product.price), 50)) + "." + str(randint(0, 99))) self.charge(charge_amount) self.buy(product.id) actions.append('charged {}'.format(charge_amount)) + except IntegrityError: + pass actions.append('bought {} for {}'.format(product.name, product.price)) else: receiver = self.users[randint(0, len(self.users) - 1)] transfer_amount = randint(1, floor(user.money)) try: - self.transfer(receiver, transfer_amount) + for i in range(0, randint(0, 10)): + self.transfer(receiver, transfer_amount) except UserNotEnoughMoney: charge_amount = Decimal(str(randint(ceil(transfer_amount), 50)) + "." + str(randint(0, 99))) self.charge(charge_amount) self.transfer(receiver, randint(1, floor(user.money))) actions.append('charged {}'.format(charge_amount)) + except IntegrityError: + pass actions.append('transfered {} to {}'.format(transfer_amount, receiver)) self.lock.acquire() print("Thread {} running for user '{}' (id: {}):".format(current_thread().ident, user.nickname, user.id), actions) diff --git a/store/views.py b/store/views.py index 558f7d42255530036f7df825d68c9f9fb45bcdd2..da7a55a0ccef65cca299f69b22db92a75d3e0265 100644 --- a/store/views.py +++ b/store/views.py @@ -121,6 +121,8 @@ def revert_charge(request): ChargeLogic.annullateCharge(charge_id, token) except ChargeNotAnnullable as exc: return JsonResponse({'error': str(exc)}, status=400) + except UserNotEnoughMoney as exc: + return JsonResponse({'error': str(exc)}, status=400) return HttpResponse(status=200) @@ -141,6 +143,8 @@ def revert_transfer(request): TransferLogic.annullateTransfer(transfer_id, token) except TransferNotAnnullable as exc: return JsonResponse({'error': str(exc)}, status=400) + except UserNotEnoughMoney as exc: + return JsonResponse({'error': str(exc)}, status=400) return HttpResponse(status=200)