def test_should_return_10_seconds_as_uptime(self):
     """
     Should return the correct uptime
     """
     proxy = AsyncProxyFactory()
     time.sleep(10)
     self.assertEqual(proxy.get_uptime(), '0 days, 00:00:10')
 def test_bytes_transferred_update_returns_correct_megabytes_amount(self):
     """
     Should return the correct amount of bytes after information update.
     """
     proxy = AsyncProxyFactory()
     proxy.update_usage(2300000)
     self.assertEqual(proxy.get_bytes_transferred(BytesTransferedOutputFormat().MBYTES), 2.195404052734375)
 def test_bytes_transferred_update_returns_correct_kilobytes_amount(self):
     """
     Should return the correct amount of bytes after information update.
     """
     proxy = AsyncProxyFactory()
     proxy.update_usage(1024)
     self.assertEqual(proxy.get_bytes_transferred(BytesTransferedOutputFormat().KBYTES), 2)
class TestAsyncProxyFactory(TestCase):

    proxy = AsyncProxyFactory

    def setUp(self):
        self.proxy = AsyncProxyFactory()

    def test_bytes_transferred_update_returns_correct_bytes_amount(self):
        """
        Should return the correct amount of bytes after information update.
        """
        self.proxy.update_usage(1024)
        self.assertEqual(self.proxy.get_bytes_transferred(BytesTransferedOutputFormat().BYTES), 1024)


    def test_bytes_transferred_update_returns_correct_kilobytes_amount(self):
        """
        Should return the correct amount of bytes after information update.
        """
        proxy = AsyncProxyFactory()
        proxy.update_usage(1024)
        self.assertEqual(proxy.get_bytes_transferred(BytesTransferedOutputFormat().KBYTES), 2)

    def test_bytes_transferred_update_returns_correct_megabytes_amount(self):
        """
        Should return the correct amount of bytes after information update.
        """
        proxy = AsyncProxyFactory()
        proxy.update_usage(2300000)
        self.assertEqual(proxy.get_bytes_transferred(BytesTransferedOutputFormat().MBYTES), 2.195404052734375)

    def test_should_return_10_seconds_as_uptime(self):
        """
        Should return the correct uptime
        """
        proxy = AsyncProxyFactory()
        time.sleep(10)
        self.assertEqual(proxy.get_uptime(), '0 days, 00:00:10')
from twisted.application import internet, service
from server.async_proxy_factories import AsyncProxyFactory
from server.async_proxy_protocols import AsyncProxyChannel

"""
The basic steps to create a twisted application are:
  - Create the application that will contain the services.
  - Create the service instance.
  - Register the server into the application by setting the application
    as the service parent.

To run:
twistd --nodaemon --python=server/async_proxy_server.py

To change the port where the proxy will listen set up a env var
export ASYNC_PROXY_SERVER_PORT=5000

default docker address 192.168.99.100
"""

async_proxy_server_port = int(os.environ.get('ASYNC_PROXY_SERVER_PORT', 8000))

application = service.Application('AsyncProxy')
service_collection = service.IServiceCollection(application)

custom_factory = AsyncProxyFactory()
custom_factory.protocol = AsyncProxyChannel

async_proxy_service_tcp = internet.TCPServer(async_proxy_server_port, custom_factory)
async_proxy_service_tcp.setServiceParent(service_collection)
 def setUp(self):
     self.proxy = AsyncProxyFactory()