Ejemplo n.º 1
0
    def testAcquireAccount(self):
        account1 = Account('user1', 'test')
        self.assertRaises(ValueError, self.am.acquire_account)
        self.am.add_account(account1)
        self.assertEqual(self.am.acquire_account(), account1)
        account1.release()

        account2 = Account('user2', 'test')
        self.am.add_account(account2)
        self.assertEqual(self.am.acquire_account(account2), account2)
        account2.release()
        account = self.am.acquire_account()
        self.assertNotEqual(account, None)
        account.release()

        account3 = Account('user3', 'test')
        pool = AccountPool()
        pool.add_account(account3)
        self.am.add_pool(pool)
        self.assertEqual(self.am.acquire_account(account2), account2)
        account2.release()
        self.assertEqual(self.am.acquire_account(account3), account3)
        account3.release()
        account = self.am.acquire_account()
        self.assertNotEqual(account, None)
        account.release()
Ejemplo n.º 2
0
 def setUp(self):
     self.user1 = 'testuser1'
     self.password1 = 'test1'
     self.account1 = Account(self.user1, self.password1)
     self.user2 = 'testuser2'
     self.password2 = 'test2'
     self.account2 = Account(self.user2, self.password2)
     self.accm = AccountPool()
Ejemplo n.º 3
0
    def testGetAccountFromHash(self):
        pool1 = AccountPool()
        acc1 = Account('user1')
        pool1.add_account(acc1)
        self.am.add_pool(pool1)

        acc2 = Account('user2')
        self.am.add_account(acc2)
        self.assertEqual(self.am.get_account_from_hash(acc1.__hash__()), acc1)
        self.assertEqual(self.am.get_account_from_hash(acc2.__hash__()), acc2)
Ejemplo n.º 4
0
 def setUp(self):
     self.user1 = 'testuser1'
     self.password1 = 'test1'
     self.account1 = Account(self.user1, self.password1)
     self.user2 = 'testuser2'
     self.password2 = 'test2'
     self.account2 = Account(self.user2, self.password2)
     self.accm = AccountPool()
Ejemplo n.º 5
0
    def testAddPool(self):
        self.assertEqual(0, self.am.default_pool.n_accounts())
        account = Account('user', 'test')
        self.am.add_account(account)
        self.assertEqual(1, self.am.default_pool.n_accounts())

        def match_cb(host):
            self.data['match-called'] = True
            self.data['host'] = host
            return True

        # Replace the default pool.
        pool1 = AccountPool()
        self.am.add_pool(pool1)
        self.assertEqual(self.am.default_pool, pool1)

        # Add another pool, making sure that it does not replace
        # the default pool.
        pool2 = AccountPool()
        pool2.add_account(self.account)
        self.am.add_pool(pool2, match_cb)
        self.assertEqual(self.am.default_pool, pool1)
Ejemplo n.º 6
0
    def testReleaseAccounts(self):
        account1 = Account('foo')
        pool = AccountPool()
        pool.add_account(account1)
        pool.acquire_account(account1, 'one')
        self.am.add_pool(pool, lambda x: None)

        account2 = Account('bar')
        self.am.add_account(account2)
        self.am.acquire_account(account2, 'two')

        self.assertNotIn(account1, pool.unlocked_accounts)
        self.assertNotIn(account2, self.am.default_pool.unlocked_accounts)

        self.am.release_accounts('two')
        self.assertNotIn(account1, pool.unlocked_accounts)
        self.assertIn(account2, self.am.default_pool.unlocked_accounts)

        self.am.release_accounts('one')
        self.assertIn(account1, pool.unlocked_accounts)
        self.assertIn(account2, self.am.default_pool.unlocked_accounts)
Ejemplo n.º 7
0
    def testReleaseAccounts(self):
        account1 = Account('foo')
        account2 = Account('bar')
        pool = AccountPool()
        pool.add_account(account1)
        pool.add_account(account2)
        pool.acquire_account(account1, 'one')
        pool.acquire_account(account2, 'two')

        self.assertNotIn(account1, pool.unlocked_accounts)
        self.assertNotIn(account2, pool.unlocked_accounts)
        pool.release_accounts('one')
        self.assertIn(account1, pool.unlocked_accounts)
        self.assertNotIn(account2, pool.unlocked_accounts)
        pool.release_accounts('one')
        self.assertIn(account1, pool.unlocked_accounts)
        self.assertNotIn(account2, pool.unlocked_accounts)
        pool.release_accounts('two')
        self.assertIn(account1, pool.unlocked_accounts)
        self.assertIn(account2, pool.unlocked_accounts)
Ejemplo n.º 8
0
    def testConstructor(self):
        accm = AccountPool()
        self.assertEqual(accm.n_accounts(), 0)

        accm = AccountPool([self.account1, self.account2])
        self.assertEqual(accm.n_accounts(), 2)
Ejemplo n.º 9
0
class AccountPoolTest(unittest.TestCase):
    CORRELATE = AccountPool

    def setUp(self):
        self.user1 = 'testuser1'
        self.password1 = 'test1'
        self.account1 = Account(self.user1, self.password1)
        self.user2 = 'testuser2'
        self.password2 = 'test2'
        self.account2 = Account(self.user2, self.password2)
        self.accm = AccountPool()

    def testConstructor(self):
        accm = AccountPool()
        self.assertEqual(accm.n_accounts(), 0)

        accm = AccountPool([self.account1, self.account2])
        self.assertEqual(accm.n_accounts(), 2)

    def testAddAccount(self):
        self.assertEqual(self.accm.n_accounts(), 0)
        self.accm.add_account(self.account1)
        self.assertEqual(self.accm.n_accounts(), 1)

        self.accm.add_account(self.account2)
        self.assertEqual(self.accm.n_accounts(), 2)

    def testReset(self):
        self.testAddAccount()
        self.accm.reset()
        self.assertEqual(self.accm.n_accounts(), 0)

    def testHasAccount(self):
        self.assertEqual(self.accm.has_account(self.account1), False)
        self.accm.add_account(self.account1)
        self.assertEqual(self.accm.has_account(self.account1), True)

    def testGetAccountFromHash(self):
        account = Account('user', 'test')
        thehash = account.__hash__()
        self.accm.add_account(account)
        self.assertEqual(self.accm.get_account_from_hash(thehash), account)

    def testGetAccountFromName(self):
        self.testAddAccount()
        self.assertEqual(self.account2,
                         self.accm.get_account_from_name(self.user2))

    def testNAccounts(self):
        self.testAddAccount()

    def testAcquireAccount(self):
        self.testAddAccount()
        self.accm.acquire_account(self.account1)
        self.account1.release()
        self.accm.acquire_account(self.account1)
        self.account1.release()

        # Add three more accounts.
        filename = os.path.join(os.path.dirname(__file__), 'account_pool.cfg')
        self.accm.add_account(get_accounts_from_file(filename))
        self.assertEqual(self.accm.n_accounts(), 5)

        for i in range(0, 2000):
            # Each time an account is acquired a different one should be
            # returned.
            acquired = {}
            for n in range(0, 5):
                account = self.accm.acquire_account()
                self.assertTrue(account is not None)
                self.assertNotIn(account.get_name(), acquired)
                acquired[account.get_name()] = account

            # Release one account.
            acquired['abc'].release()

            # Acquire one account.
            account = self.accm.acquire_account()
            self.assertEqual(account.get_name(), 'abc')

            # Release all accounts.
            for account in list(acquired.values()):
                account.release()

    def testReleaseAccounts(self):
        account1 = Account('foo')
        account2 = Account('bar')
        pool = AccountPool()
        pool.add_account(account1)
        pool.add_account(account2)
        pool.acquire_account(account1, 'one')
        pool.acquire_account(account2, 'two')

        self.assertNotIn(account1, pool.unlocked_accounts)
        self.assertNotIn(account2, pool.unlocked_accounts)
        pool.release_accounts('one')
        self.assertIn(account1, pool.unlocked_accounts)
        self.assertNotIn(account2, pool.unlocked_accounts)
        pool.release_accounts('one')
        self.assertIn(account1, pool.unlocked_accounts)
        self.assertNotIn(account2, pool.unlocked_accounts)
        pool.release_accounts('two')
        self.assertIn(account1, pool.unlocked_accounts)
        self.assertIn(account2, pool.unlocked_accounts)
Ejemplo n.º 10
0
    def testReleaseAccounts(self):
        account1 = Account('foo')
        account2 = Account('bar')
        pool = AccountPool()
        pool.add_account(account1)
        pool.add_account(account2)
        pool.acquire_account(account1, 'one')
        pool.acquire_account(account2, 'two')

        self.assertNotIn(account1, pool.unlocked_accounts)
        self.assertNotIn(account2, pool.unlocked_accounts)
        pool.release_accounts('one')
        self.assertIn(account1, pool.unlocked_accounts)
        self.assertNotIn(account2, pool.unlocked_accounts)
        pool.release_accounts('one')
        self.assertIn(account1, pool.unlocked_accounts)
        self.assertNotIn(account2, pool.unlocked_accounts)
        pool.release_accounts('two')
        self.assertIn(account1, pool.unlocked_accounts)
        self.assertIn(account2, pool.unlocked_accounts)
Ejemplo n.º 11
0
    def testConstructor(self):
        accm = AccountPool()
        self.assertEqual(accm.n_accounts(), 0)

        accm = AccountPool([self.account1, self.account2])
        self.assertEqual(accm.n_accounts(), 2)
Ejemplo n.º 12
0
class AccountPoolTest(unittest.TestCase):
    CORRELATE = AccountPool

    def setUp(self):
        self.user1 = 'testuser1'
        self.password1 = 'test1'
        self.account1 = Account(self.user1, self.password1)
        self.user2 = 'testuser2'
        self.password2 = 'test2'
        self.account2 = Account(self.user2, self.password2)
        self.accm = AccountPool()

    def testConstructor(self):
        accm = AccountPool()
        self.assertEqual(accm.n_accounts(), 0)

        accm = AccountPool([self.account1, self.account2])
        self.assertEqual(accm.n_accounts(), 2)

    def testAddAccount(self):
        self.assertEqual(self.accm.n_accounts(), 0)
        self.accm.add_account(self.account1)
        self.assertEqual(self.accm.n_accounts(), 1)

        self.accm.add_account(self.account2)
        self.assertEqual(self.accm.n_accounts(), 2)

    def testReset(self):
        self.testAddAccount()
        self.accm.reset()
        self.assertEqual(self.accm.n_accounts(), 0)

    def testHasAccount(self):
        self.assertEqual(self.accm.has_account(self.account1), False)
        self.accm.add_account(self.account1)
        self.assertEqual(self.accm.has_account(self.account1), True)

    def testGetAccountFromHash(self):
        account = Account('user', 'test')
        thehash = account.__hash__()
        self.accm.add_account(account)
        self.assertEqual(self.accm.get_account_from_hash(thehash), account)

    def testGetAccountFromName(self):
        self.testAddAccount()
        self.assertEqual(self.account2,
                         self.accm.get_account_from_name(self.user2))

    def testNAccounts(self):
        self.testAddAccount()

    def testAcquireAccount(self):
        self.testAddAccount()
        self.accm.acquire_account(self.account1)
        self.account1.release()
        self.accm.acquire_account(self.account1)
        self.account1.release()

        # Add three more accounts.
        filename = os.path.join(os.path.dirname(__file__), 'account_pool.cfg')
        self.accm.add_account(get_accounts_from_file(filename))
        self.assertEqual(self.accm.n_accounts(), 5)

        for i in range(0, 2000):
            # Each time an account is acquired a different one should be
            # returned.
            acquired = {}
            for n in range(0, 5):
                account = self.accm.acquire_account()
                self.assertTrue(account is not None)
                self.assertNotIn(account.get_name(), acquired)
                acquired[account.get_name()] = account

            # Release one account.
            acquired['abc'].release()

            # Acquire one account.
            account = self.accm.acquire_account()
            self.assertEqual(account.get_name(), 'abc')

            # Release all accounts.
            for account in list(acquired.values()):
                account.release()

    def testReleaseAccounts(self):
        account1 = Account('foo')
        account2 = Account('bar')
        pool = AccountPool()
        pool.add_account(account1)
        pool.add_account(account2)
        pool.acquire_account(account1, 'one')
        pool.acquire_account(account2, 'two')

        self.assertNotIn(account1, pool.unlocked_accounts)
        self.assertNotIn(account2, pool.unlocked_accounts)
        pool.release_accounts('one')
        self.assertIn(account1, pool.unlocked_accounts)
        self.assertNotIn(account2, pool.unlocked_accounts)
        pool.release_accounts('one')
        self.assertIn(account1, pool.unlocked_accounts)
        self.assertNotIn(account2, pool.unlocked_accounts)
        pool.release_accounts('two')
        self.assertIn(account1, pool.unlocked_accounts)
        self.assertIn(account2, pool.unlocked_accounts)