# Path: app/models.py
# class App(models.Model):
#     """
#     Model that contains information about the supported apps by the middleware.
#     """
#     platform = models.CharField(choices=PLATFORM_CHOICES, max_length=10)
#     app_id = models.CharField(max_length=255)
# 
#     push_key = models.CharField(max_length=255)
#     logentries_token = models.CharField(max_length=255, blank=False, null=False, default='')
#     partner_logentries_token = models.CharField(max_length=255, blank=True, null=True, default='')
# 
#     def __str__(self):
#         return '{0} for {1}'.format(self.app_id, self.platform)
# 
#     class Meta:
#         unique_together = ('app_id', 'platform')
# 
# class Device(models.Model):
#     """
#     Model for all device who register at the middleware.
#     """
#     # FIXME: We need this to be backwards compatible for one release.
#     id = models.CharField(max_length=255, unique=True)
# 
#     name = models.CharField(max_length=255, blank=True, null=True)
#     sip_user_id = models.CharField(max_length=255, unique=True, primary_key=True)
#     os_version = models.CharField(max_length=255, blank=True, null=True)
#     client_version = models.CharField(max_length=255, blank=True, null=True)
#     token = models.CharField(max_length=250)
#     sandbox = models.BooleanField(default=False)
#     last_seen = models.DateTimeField(blank=True, null=True)
#     app = models.ForeignKey(App)
#     remote_logging_id = models.CharField(max_length=255, blank=True, null=True)
# 
#     def __str__(self):
#         return '{0} - {1}'.format(self.sip_user_id, self.name)
# 
#     # FIXME: We need this to be backwards compatible for one release.
#     def save(self, *args, **kwargs):
#         """
#         Make sure the id fields stays the same as sip_user_id so we are
#         backwards compatible.
#         """
#         if self.sip_user_id:
#             self.id = self.sip_user_id
#             obj = super(Device, self).save(*args, **kwargs)
#             return obj
# 
# Path: api/tests/utils.py
# def mocked_send_apns_message(device, app, message_type, data=None):
#     cache.set('attempts', data.get('attempt', 1), 300)
#     print('WORKED APNS')
#     print(data.get('attempt', 1))
# 
# class ThreadWithReturn(Thread):
#     def __init__(self, *args, **kwargs):
#         super(ThreadWithReturn, self).__init__(*args, **kwargs)
# 
#         self._return = None
# 
#     def run(self):
#         if self._target:
#             self._return = self._target(*self._args, **self._kwargs)
# 
#     def join(self, *args, **kwargs):
#         super(ThreadWithReturn, self).join(*args, **kwargs)
# 
#         return self._return


from datetime import datetime, timedelta
from unittest import mock
from django.conf import settings
from django.test import TransactionTestCase
from rest_framework.test import APIClient
from app.models import App, Device
from .utils import mocked_send_apns_message, ThreadWithReturn
import time
            'caller_id': 'Test name',
            'phonenumber': '0123456789',
            'call_id': 'sduiqayduiryqwuioeryqwer76789',
        }

        # Now the device exists, call it again in seperate thread.
        thread = ThreadWithReturn(target=self.client.post, args=('/api/incoming-call/', call_data))
        thread.start()

        # Simulate some wait-time before device responds.
        time.sleep(1)

        app_data = {
            'unique_key': call_data['call_id'],
            'message_start_time': time.time(),
        }
        # Send the fake response from device.
        self.client.post('/api/call-response/', app_data)

        # Wait for the incoming-call to finish.
        response = thread.join()

        # Check if incoming-call got accepted.
        self.assertEqual(response.content, b'status=ACK')

    def test_performance(self):
        iterations = int(settings.PERFORMANCE_TEST_ITERATIONS)

        run_time = 0
        for i in range(iterations):

# Path: app/models.py
# class App(models.Model):
#     """
#     Model that contains information about the supported apps by the middleware.
#     """
#     platform = models.CharField(choices=PLATFORM_CHOICES, max_length=10)
#     app_id = models.CharField(max_length=255)
# 
#     push_key = models.CharField(max_length=255)
#     logentries_token = models.CharField(max_length=255, blank=False, null=False, default='')
#     partner_logentries_token = models.CharField(max_length=255, blank=True, null=True, default='')
# 
#     def __str__(self):
#         return '{0} for {1}'.format(self.app_id, self.platform)
# 
#     class Meta:
#         unique_together = ('app_id', 'platform')
# 
# class Device(models.Model):
#     """
#     Model for all device who register at the middleware.
#     """
#     # FIXME: We need this to be backwards compatible for one release.
#     id = models.CharField(max_length=255, unique=True)
# 
#     name = models.CharField(max_length=255, blank=True, null=True)
#     sip_user_id = models.CharField(max_length=255, unique=True, primary_key=True)
#     os_version = models.CharField(max_length=255, blank=True, null=True)
#     client_version = models.CharField(max_length=255, blank=True, null=True)
#     token = models.CharField(max_length=250)
#     sandbox = models.BooleanField(default=False)
#     last_seen = models.DateTimeField(blank=True, null=True)
#     app = models.ForeignKey(App)
#     remote_logging_id = models.CharField(max_length=255, blank=True, null=True)
# 
#     def __str__(self):
#         return '{0} - {1}'.format(self.sip_user_id, self.name)
# 
#     # FIXME: We need this to be backwards compatible for one release.
#     def save(self, *args, **kwargs):
#         """
#         Make sure the id fields stays the same as sip_user_id so we are
#         backwards compatible.
#         """
#         if self.sip_user_id:
#             self.id = self.sip_user_id
#             obj = super(Device, self).save(*args, **kwargs)
#             return obj
# 
# Path: api/tests/utils.py
# def mocked_send_apns_message(device, app, message_type, data=None):
#     cache.set('attempts', data.get('attempt', 1), 300)
#     print('WORKED APNS')
#     print(data.get('attempt', 1))
# 
# class ThreadWithReturn(Thread):
#     def __init__(self, *args, **kwargs):
#         super(ThreadWithReturn, self).__init__(*args, **kwargs)
# 
#         self._return = None
# 
#     def run(self):
#         if self._target:
#             self._return = self._target(*self._args, **self._kwargs)
# 
#     def join(self, *args, **kwargs):
#         super(ThreadWithReturn, self).join(*args, **kwargs)
# 
#         return self._return


# Path: api/tests/test_performance.py
from datetime import datetime, timedelta
from unittest import mock
from django.conf import settings
from django.test import TransactionTestCase
from rest_framework.test import APIClient
from app.models import App, Device
from .utils import mocked_send_apns_message, ThreadWithReturn
import time

            'caller_id': 'Test name',
            'phonenumber': '0123456789',
            'call_id': 'sduiqayduiryqwuioeryqwer76789',
        }

        # Now the device exists, call it again in seperate thread.
        thread = ThreadWithReturn(target=self.client.post, args=('/api/incoming-call/', call_data))
        thread.start()

        # Simulate some wait-time before device responds.
        time.sleep(1)

        app_data = {
            'unique_key': call_data['call_id'],
            'message_start_time': time.time(),
        }
        # Send the fake response from device.
        self.client.post('/api/call-response/', app_data)

        # Wait for the incoming-call to finish.
        response = thread.join()

        # Check if incoming-call got accepted.
        self.assertEqual(response.content, b'status=ACK')

    def test_performance(self):
        iterations = int(settings.PERFORMANCE_TEST_ITERATIONS)

        run_time = 0
        for i in range(iterations):

            start = time.time()