# Path: groundstation/peer_socket_pool.py
# class PeerSocketPool(list):
#     """
#     Encapsulates a pool of PeerSockets.
# 
#     Correctly replies to PeerSocket in Pool
#     """
#     def __init__(self, *args, **kwargs):
#         super(list, self).__init__(self, *args, **kwargs)
#         self.purged_connections = []
# 
#     def purge(self, peer):
#         self.purged_connections.remove(peer)
# 
#     def remove(self, other):
#         if other.peer in self.purged_connections:
#             return False
#         for idx, i in enumerate(self):
#             if i.peer == other.peer:
#                 conn = self.pop(idx)
#                 self.purged_connections.append(conn.peer)
#                 return conn
#         raise AttributeError("%s has no attribute %s" %
#                     (type(self), repr(other)))
# 
#     def __contains__(self, other):
#         for i in self:
#             if i.peer == other:
#                 return True
#         return False
# 
#     def __getitem__(self, key):
#         for i in self:
#             if i.peer == key:
#                 return i
#         raise KeyError


import unittest
from support.peer_socket_pool_fixture import MockPeer
from groundstation.peer_socket_pool import PeerSocketPool



class TestPeerSocketPool(unittest.TestCase):
    def test_add_remove(self):
        pool = PeerSocketPool()
        peer = MockPeer('127.0.0.1')

        pool.append(peer)
        pool.remove(peer)

        self.assertEqual(0, len(pool))

    def test_throws_error_on_remove_nonexistant_peer(self):
        pool = PeerSocketPool()
        peer = MockPeer('127.0.0.1')

        self.assertRaises(AttributeError, pool.remove, peer)

    def test_safe_multiple_removes(self):
        pool = PeerSocketPool()
        peer = MockPeer('127.0.0.1')

        pool.append(peer)
        pool.remove(peer)
        self.assertEqual(0, len(pool))
        pool.remove(peer)
        self.assertEqual(0, len(pool))

    def test_raises_after_purge(self):

# Path: groundstation/peer_socket_pool.py
# class PeerSocketPool(list):
#     """
#     Encapsulates a pool of PeerSockets.
# 
#     Correctly replies to PeerSocket in Pool
#     """
#     def __init__(self, *args, **kwargs):
#         super(list, self).__init__(self, *args, **kwargs)
#         self.purged_connections = []
# 
#     def purge(self, peer):
#         self.purged_connections.remove(peer)
# 
#     def remove(self, other):
#         if other.peer in self.purged_connections:
#             return False
#         for idx, i in enumerate(self):
#             if i.peer == other.peer:
#                 conn = self.pop(idx)
#                 self.purged_connections.append(conn.peer)
#                 return conn
#         raise AttributeError("%s has no attribute %s" %
#                     (type(self), repr(other)))
# 
#     def __contains__(self, other):
#         for i in self:
#             if i.peer == other:
#                 return True
#         return False
# 
#     def __getitem__(self, key):
#         for i in self:
#             if i.peer == key:
#                 return i
#         raise KeyError


# Path: test/test_peer_socket_pool.py
import unittest
from support.peer_socket_pool_fixture import MockPeer
from groundstation.peer_socket_pool import PeerSocketPool




class TestPeerSocketPool(unittest.TestCase):
    def test_add_remove(self):
        pool = PeerSocketPool()
        peer = MockPeer('127.0.0.1')

        pool.append(peer)
        pool.remove(peer)

        self.assertEqual(0, len(pool))

    def test_throws_error_on_remove_nonexistant_peer(self):
        pool = PeerSocketPool()
        peer = MockPeer('127.0.0.1')

        self.assertRaises(AttributeError, pool.remove, peer)

    def test_safe_multiple_removes(self):
        pool = PeerSocketPool()
        peer = MockPeer('127.0.0.1')

        pool.append(peer)
        pool.remove(peer)
        self.assertEqual(0, len(pool))
        pool.remove(peer)
        self.assertEqual(0, len(pool))

    def test_raises_after_purge(self):

        pool = PeerSocketPool()