# Path: amfast/remoting/connection_manager.py
# class NotConnectedError(ConnectionError):
#     pass
# 
# class SessionAttrError(ConnectionError):
#     pass
# 
# class MemoryConnectionManager(ConnectionManager):
#     """Manages connections in memory."""
# 
#     def __init__(self, connection_class=Connection, connection_params=None):
#         ConnectionManager.__init__(self, connection_class=connection_class,
#             connection_params=connection_params)
# 
#         self._lock = amfast.mutex_cls()
#         self.reset()
# 
#     def reset(self):
#         self._connections = {}
#         self._channels = {}
# 
#     def getConnectionCount(self, channel_name):
#         try:
#             return self._channels[channel_name]
#         except KeyError:
#             return 0
# 
#     def loadConnection(self, connection_id):
#         connection = self._connections.get(connection_id, None)
#         if connection is None:
#             raise NotConnectedError("Connection '%s' is not connected." % connection_id)
# 
#         return connection
# 
#     def initConnection(self, connection, channel):
#         connection._session = {}
#         connection._connected = True
#         connection._last_active = time.time() * 1000
#         connection._last_polled = 0.0
#         connection._authenticated = False
#         connection._flex_user = None
# 
#         self._connections[connection.id] = connection
# 
#         self._lock.acquire()
#         try:
#             try:
#                 self._channels[channel.name] += 1
#             except KeyError:
#                 self._channels[channel.name] = 1
#         finally:
#             self._lock.release()
# 
#     def iterConnectionIds(self):
#         return self._connections.keys().__iter__()
# 
#     # --- proxies for connection properties --- #
# 
#     def getConnected(self, connection):
#         return connection._connected
# 
#     def getLastActive(self, connection):
#         return connection._last_active
# 
#     def getLastPolled(self, connection):
#         return connection._last_polled
# 
#     def getAuthenticated(self, connection):
#         return connection._authenticated
# 
#     def getFlexUser(self, connection):
#         return connection._flex_user
# 
#     def getNotifyFunc(self, connection):
#         if not hasattr(connection, '_notify_func_id'):
#             return None
#         else:
#             return connection._getNotifyFuncById(connection._notify_func_id)
# 
#     # --- proxies for connection methods --- #
# 
#     def connectConnection(self, connection):
#         connection._connected = True
# 
#     def disconnectConnection(self, connection):
#         connection._connected = False
# 
#     def deleteConnection(self, connection):
#         self._lock.acquire()
#         try:
#             if connection.id in self._connections:
#                 del self._connections[connection.id]
# 
#             if connection.channel_name in self._channels:
#                 self._channels[connection.channel_name] -= 1
#         finally:
#             self._lock.release()
# 
#         ConnectionManager.deleteConnection(self, connection)
# 
#     def touchConnection(self, connection):
#         connection._last_active = time.time() * 1000
# 
#     def touchPolled(self, connection):
#         connection._last_polled = time.time() * 1000
# 
#     def authenticateConnection(self, connection, user):
#         connection._authenticated = True
#         connection._flex_user = user
# 
#     def unAuthenticateConnection(self, connection):
#         connection._authenticated = False
#         connection._flex_user = None
# 
#     def setNotifyFunc(self, connection, func):
#         connection._notify_func_id = connection._setNotifyFunc(func)
# 
#     def unSetNotifyFunc(self, connection):
#         if hasattr(connection, '_notify_func_id'):
#             connection._delNotifyFunc(connection._notify_func_id)
#             del connection._notify_func_id
# 
#     def getConnectionSessionAttr(self, connection, name):
#         try:
#             return connection._session[name]
#         except KeyError:
#             raise SessionAttrError("Attribute '%s' not found." % name)
# 
#     def setConnectionSessionAttr(self, connection, name, val):
#         connection._session[name] = val
# 
#     def delConnectionSessionAttr(self, connection, name):
#         try:
#             del connection._session[name]
#         except KeyError:
#             pass


import os
import sys
import time
import unittest
        import sqlalchemy as sa
        import sqlalchemy as sa
from amfast.remoting.connection_manager import NotConnectedError, \
    SessionAttrError, MemoryConnectionManager
        from amfast.remoting.gae_connection_manager import GaeConnectionManager
        from amfast.remoting.memcache_connection_manager import MemcacheConnectionManager
        from amfast.remoting.sa_connection_manager import SaConnectionManager
        from amfast.remoting.gae_connection_manager import GaeConnectionManager
        from amfast.remoting.sa_connection_manager import SaConnectionManager
        from amfast.remoting.memcache_connection_manager import MemcacheConnectionManager
        self.assertEquals(connection.authenticated, connection_2.authenticated)
        self.assertEquals(connection.flex_user, connection_2.flex_user)
        self.assertEquals(connection.last_active, connection_2.last_active)
        self.assertEquals(connection.last_polled, connection_2.last_polled)

    def testCreateConnection(self):
        connection = self.manager.createConnection(self.channel)
        self._testConnectionProps(connection)

    def testGetConnection(self):
        connection = self.manager.createConnection(self.channel)
        last_active = connection.last_active

        new_connection = self.manager.getConnection(connection.id, touch=True)
        self._testConnectionProps(new_connection)
        self.assertTrue(last_active < new_connection.last_active)
        last_active = new_connection.last_active

        new_connection = self.manager.getConnection(connection.id, touch=False)
        self.assertEquals(last_active, new_connection.last_active)

        self._testCompare(connection, new_connection)

    def testGetConnectionRaisesNotConnectedError(self):
        self.assertRaises(NotConnectedError, self.manager.getConnection, 'not_connected')

    def testDeleteConnection(self):
        connection = self.manager.createConnection(self.channel)
        self.manager.deleteConnection(connection)
        self.assertFalse(connection.connected)

# Path: amfast/remoting/connection_manager.py
# class NotConnectedError(ConnectionError):
#     pass
# 
# class SessionAttrError(ConnectionError):
#     pass
# 
# class MemoryConnectionManager(ConnectionManager):
#     """Manages connections in memory."""
# 
#     def __init__(self, connection_class=Connection, connection_params=None):
#         ConnectionManager.__init__(self, connection_class=connection_class,
#             connection_params=connection_params)
# 
#         self._lock = amfast.mutex_cls()
#         self.reset()
# 
#     def reset(self):
#         self._connections = {}
#         self._channels = {}
# 
#     def getConnectionCount(self, channel_name):
#         try:
#             return self._channels[channel_name]
#         except KeyError:
#             return 0
# 
#     def loadConnection(self, connection_id):
#         connection = self._connections.get(connection_id, None)
#         if connection is None:
#             raise NotConnectedError("Connection '%s' is not connected." % connection_id)
# 
#         return connection
# 
#     def initConnection(self, connection, channel):
#         connection._session = {}
#         connection._connected = True
#         connection._last_active = time.time() * 1000
#         connection._last_polled = 0.0
#         connection._authenticated = False
#         connection._flex_user = None
# 
#         self._connections[connection.id] = connection
# 
#         self._lock.acquire()
#         try:
#             try:
#                 self._channels[channel.name] += 1
#             except KeyError:
#                 self._channels[channel.name] = 1
#         finally:
#             self._lock.release()
# 
#     def iterConnectionIds(self):
#         return self._connections.keys().__iter__()
# 
#     # --- proxies for connection properties --- #
# 
#     def getConnected(self, connection):
#         return connection._connected
# 
#     def getLastActive(self, connection):
#         return connection._last_active
# 
#     def getLastPolled(self, connection):
#         return connection._last_polled
# 
#     def getAuthenticated(self, connection):
#         return connection._authenticated
# 
#     def getFlexUser(self, connection):
#         return connection._flex_user
# 
#     def getNotifyFunc(self, connection):
#         if not hasattr(connection, '_notify_func_id'):
#             return None
#         else:
#             return connection._getNotifyFuncById(connection._notify_func_id)
# 
#     # --- proxies for connection methods --- #
# 
#     def connectConnection(self, connection):
#         connection._connected = True
# 
#     def disconnectConnection(self, connection):
#         connection._connected = False
# 
#     def deleteConnection(self, connection):
#         self._lock.acquire()
#         try:
#             if connection.id in self._connections:
#                 del self._connections[connection.id]
# 
#             if connection.channel_name in self._channels:
#                 self._channels[connection.channel_name] -= 1
#         finally:
#             self._lock.release()
# 
#         ConnectionManager.deleteConnection(self, connection)
# 
#     def touchConnection(self, connection):
#         connection._last_active = time.time() * 1000
# 
#     def touchPolled(self, connection):
#         connection._last_polled = time.time() * 1000
# 
#     def authenticateConnection(self, connection, user):
#         connection._authenticated = True
#         connection._flex_user = user
# 
#     def unAuthenticateConnection(self, connection):
#         connection._authenticated = False
#         connection._flex_user = None
# 
#     def setNotifyFunc(self, connection, func):
#         connection._notify_func_id = connection._setNotifyFunc(func)
# 
#     def unSetNotifyFunc(self, connection):
#         if hasattr(connection, '_notify_func_id'):
#             connection._delNotifyFunc(connection._notify_func_id)
#             del connection._notify_func_id
# 
#     def getConnectionSessionAttr(self, connection, name):
#         try:
#             return connection._session[name]
#         except KeyError:
#             raise SessionAttrError("Attribute '%s' not found." % name)
# 
#     def setConnectionSessionAttr(self, connection, name, val):
#         connection._session[name] = val
# 
#     def delConnectionSessionAttr(self, connection, name):
#         try:
#             del connection._session[name]
#         except KeyError:
#             pass


# Path: tests/connection_test.py
import os
import sys
import time
import unittest
        import sqlalchemy as sa
        import sqlalchemy as sa
from amfast.remoting.connection_manager import NotConnectedError, \
    SessionAttrError, MemoryConnectionManager
        from amfast.remoting.gae_connection_manager import GaeConnectionManager
        from amfast.remoting.memcache_connection_manager import MemcacheConnectionManager
        from amfast.remoting.sa_connection_manager import SaConnectionManager
        from amfast.remoting.gae_connection_manager import GaeConnectionManager
        from amfast.remoting.sa_connection_manager import SaConnectionManager
        from amfast.remoting.memcache_connection_manager import MemcacheConnectionManager

        self.assertEquals(connection.authenticated, connection_2.authenticated)
        self.assertEquals(connection.flex_user, connection_2.flex_user)
        self.assertEquals(connection.last_active, connection_2.last_active)
        self.assertEquals(connection.last_polled, connection_2.last_polled)

    def testCreateConnection(self):
        connection = self.manager.createConnection(self.channel)
        self._testConnectionProps(connection)

    def testGetConnection(self):
        connection = self.manager.createConnection(self.channel)
        last_active = connection.last_active

        new_connection = self.manager.getConnection(connection.id, touch=True)
        self._testConnectionProps(new_connection)
        self.assertTrue(last_active < new_connection.last_active)
        last_active = new_connection.last_active

        new_connection = self.manager.getConnection(connection.id, touch=False)
        self.assertEquals(last_active, new_connection.last_active)

        self._testCompare(connection, new_connection)

    def testGetConnectionRaisesNotConnectedError(self):
        self.assertRaises(NotConnectedError, self.manager.getConnection, 'not_connected')

    def testDeleteConnection(self):
        connection = self.manager.createConnection(self.channel)
        self.manager.deleteConnection(connection)
        self.assertFalse(connection.connected)

        self.assertRaises(NotConnectedError, self.manager.getConnection, connection.id)