Esempio n. 1
0
    def test_acquire_retry(self):
        pool = Pool(Mock(), max_connections=1)
        conn1 = pool.acquire()  # make the pool reach the max connection

        gevent.spawn_later(0, pool.release, conn1)
        conn = pool.acquire(retry=1)
        eq_(conn, conn1)
Esempio n. 2
0
    def test_connection_context_manager(self):
        mock_factory = Mock()
        mock_connection = Mock()
        mock_factory.return_value = mock_connection

        pool = Pool(mock_factory)

        with pool.connection() as conn:
            eq_(mock_connection, conn)
            ok_(mock_connection in pool._using)

        ok_(not mock_connection in pool._using)
        ok_(mock_connection in pool._pool)
Esempio n. 3
0
    def __init__(self, server_address: Tuple[str, int], *args, **kwargs):
        super().__init__(server_address, *args, **kwargs)
        self.methods = [
            self.get_prices_pfx,
            self.get_prices_nano,
            self.get_prices_try,
        ]
        self.pfx_handle = ''
        self.nano_handle = ''
        self.try_handle = ''
        self.pool = Pool(PoolClient,
                         dict(name=self.name, server_address=server_address))

        lock = RLock()

        def find_pfx():
            pool = gsocketpool.pool.Pool(
                PoolClient, dict(name=self.name,
                                 server_address=server_address))
            while not self.pfx_handle:
                with lock:
                    with pool.connection() as client:
                        self.pfx_handle = client.find_window('パートナーズFX$')
                gevent.sleep(10)

        def find_nano():
            pool = gsocketpool.pool.Pool(
                PoolClient, dict(name=self.name,
                                 server_address=server_address))
            while not self.nano_handle:
                with lock:
                    with pool.connection() as client:
                        self.nano_handle = client.find_window('パートナーズFX nano')
                gevent.sleep(10)

        def find_try():
            pool = gsocketpool.pool.Pool(
                PoolClient, dict(name=self.name,
                                 server_address=server_address))
            while not self.try_handle:
                with lock:
                    with pool.connection() as client:
                        self.try_handle = client.find_window('レート')
                gevent.sleep(10)

        gevent.spawn(find_pfx)
        gevent.spawn(find_nano)
        gevent.spawn(find_try)
Esempio n. 4
0
    def test_start_connection_reaper(self, mock_reaper):
        mock_reaper_ins = Mock()
        mock_reaper.return_value = mock_reaper_ins

        pool = Pool(Mock(), reap_expired_connections=True, reap_interval=3)

        mock_reaper.assert_called_once_with(pool, 3)
        mock_reaper_ins.start.assert_called_once_with()
Esempio n. 5
0
    def test_drop_expired_connections(self):
        mock_factory = Mock()

        mock_conn1 = Mock()
        mock_conn2 = Mock()
        mock_conn3 = Mock()

        mock_conn1.is_expired.return_value = True
        mock_conn2.is_expired.return_value = False
        mock_conn3.is_expired.return_value = True

        mock_factory.side_effect = [mock_conn1, mock_conn2, mock_conn3]
        pool = Pool(mock_factory, initial_connections=3)

        pool.drop_expired()

        ok_(mock_conn1 not in pool._pool)
        mock_conn1.close.assert_called_once_with()
        ok_(mock_conn2 in pool._pool)
        ok_(mock_conn3 not in pool._pool)
        mock_conn1.close.assert_called_once_with()
Esempio n. 6
0
    def test_drop(self):
        pool = Pool(Mock())
        conn1 = pool.acquire()
        pool.release(conn1)

        ok_(conn1 in pool._pool)

        pool.drop(conn1)

        ok_(conn1 not in pool._pool)
        conn1.close.assert_called_once_with()
Esempio n. 7
0
    def test_drop_closed_connection(self):
        pool = Pool(Mock())
        conn1 = pool.acquire()
        conn1.is_connected = Mock()
        conn1.is_connected.return_value = False
        pool.release(conn1)

        ok_(conn1 in pool._pool)

        pool.drop(conn1)

        ok_(conn1 not in pool._pool)
        ok_(not conn1.close.called)
Esempio n. 8
0
 def extend_accounts(_pool: Pool):
     with _pool.connection() as client:
         l = client.get_accounts() or []
     accounts.extend(l)
Esempio n. 9
0
    def test_acquire_and_release(self):
        mock_factory = Mock()
        mock_conn1 = Mock()
        mock_conn2 = Mock()
        mock_factory.side_effect = [mock_conn1, mock_conn2]
        pool = Pool(mock_factory, initial_connections=0)

        eq_(0, pool.size)

        # acquire conn1
        conn1 = pool.acquire()
        eq_(mock_conn1, conn1)
        eq_(1, pool.size)
        ok_(conn1 not in pool._pool)
        ok_(conn1 in pool._using)

        # acquire conn2
        conn2 = pool.acquire()
        eq_(mock_conn2, conn2)
        eq_(2, pool.size)
        ok_(conn2 in pool._using)

        # release conn1
        pool.release(conn1)
        ok_(conn1 in pool._pool)
        ok_(conn1 not in pool._using)

        # acquire conn1 again
        conn1_2 = pool.acquire()
        eq_(mock_conn1, conn1_2)
        eq_(2, pool.size)
        ok_(conn1_2 not in pool._pool)
        ok_(conn1_2 in pool._using)

        # release conn2
        pool.release(conn2)
        ok_(conn2 in pool._pool)
        ok_(conn2 not in pool._using)

        # release conn1
        pool.release(conn1_2)
        ok_(conn1_2 in pool._pool)
        ok_(conn1_2 not in pool._using)
Esempio n. 10
0
 def extend_accounts(_pool: Pool):
     with _pool.connection() as client:
         l = client.get_accounts() or []
         if do_refresh:
             client.refresh()
     accounts.extend(l)
Esempio n. 11
0
class BrowsePoolClient(PoolClient):
    def __init__(self, server_address: Tuple[str, int], *args, **kwargs):
        super().__init__(server_address, *args, **kwargs)
        self.methods = [
            self.get_prices_pfx,
            self.get_prices_nano,
            self.get_prices_try,
        ]
        self.pfx_handle = ''
        self.nano_handle = ''
        self.try_handle = ''
        self.pool = Pool(PoolClient,
                         dict(name=self.name, server_address=server_address))

        lock = RLock()

        def find_pfx():
            pool = gsocketpool.pool.Pool(
                PoolClient, dict(name=self.name,
                                 server_address=server_address))
            while not self.pfx_handle:
                with lock:
                    with pool.connection() as client:
                        self.pfx_handle = client.find_window('パートナーズFX$')
                gevent.sleep(10)

        def find_nano():
            pool = gsocketpool.pool.Pool(
                PoolClient, dict(name=self.name,
                                 server_address=server_address))
            while not self.nano_handle:
                with lock:
                    with pool.connection() as client:
                        self.nano_handle = client.find_window('パートナーズFX nano')
                gevent.sleep(10)

        def find_try():
            pool = gsocketpool.pool.Pool(
                PoolClient, dict(name=self.name,
                                 server_address=server_address))
            while not self.try_handle:
                with lock:
                    with pool.connection() as client:
                        self.try_handle = client.find_window('レート')
                gevent.sleep(10)

        gevent.spawn(find_pfx)
        gevent.spawn(find_nano)
        gevent.spawn(find_try)

    def get_prices(self, now: datetime) -> List[dict]:
        results = []

        def get(_method):
            nonlocal results
            with self.pool.connection() as c:
                results += _method(c, now)

        spawns = []
        for method in random.sample(self.methods, len(self.methods)):
            spawns.append(gevent.spawn(get, method))
        gevent.joinall(spawns, timeout=1)
        return results

    def get_prices_mnp(self, client, now: datetime, service: str,
                       instruments: List[str], handle: str,
                       frame: str) -> List[dict]:
        bid_str = '#bidCurrencyPrice{}'
        ask_str = '#askCurrencyPrice{}'
        results = []
        html = ''.join(client.get_elements_html('#PriceList', handle, frame))
        if not html:
            return []
        dom = lxml.html.fromstring(html)
        for i, instrument in enumerate(instruments):
            i += 1
            bid = float(dom.cssselect(bid_str.format(i))[0].text_content())
            ask = float(dom.cssselect(ask_str.format(i))[0].text_content())
            results.append(
                dict(service=service,
                     time=now,
                     instrument=instrument,
                     bid=bid,
                     ask=ask))
        return results

    def get_prices_pfx(self, client, now: datetime) -> List[dict]:
        if not self.pfx_handle:
            return []
        instruments = [
            'USD/JPY',
            'EUR/USD',
            'AUD/JPY',
            'NZD/JPY',
            'GBP/JPY',
            'EUR/JPY',
            'CHF/JPY',
            'CAD/JPY',
            'GBP/USD',
            'ZAR/JPY',
        ]
        return self.get_prices_mnp(client, now, 'pfx', instruments,
                                   self.pfx_handle, 'rate')

    def get_prices_nano(self, client, now: datetime) -> List[dict]:
        if not self.nano_handle:
            return []

        instruments = [
            'USD/JPY',
            'EUR/JPY',
            'AUD/JPY',
            'EUR/USD',
            'GBP/JPY',
            'NZD/JPY',
            'ZAR/JPY',
            'CHF/JPY',
        ]
        return self.get_prices_mnp(client, now, 'nano', instruments,
                                   self.nano_handle, 'rate')

    def get_prices_try(self, client, now: datetime) -> List[dict]:
        service = 'try'
        if not self.try_handle:
            return []
        results = []
        html = ''.join(client.get_elements_html('#rateList2', self.try_handle))
        if not html:
            return []
        dom = lxml.html.fromstring(html)
        replace_spaces = re.compile('[^A-Z/]')
        for e in dom.cssselect('.currencyPair'):
            instrument = e.cssselect('td')[0].text_content()
            instrument = replace_spaces.sub('', instrument)
            bid = e.cssselect('td.bid')[0].text_content()
            ask = e.cssselect('td.ask')[0].text_content()
            bid, ask = float(bid), float(ask)
            results.append(
                dict(service=service,
                     time=now,
                     instrument=instrument,
                     bid=bid,
                     ask=ask))
        return results
Esempio n. 12
0
    def test_create_initial_connections(self):
        mock_factory = Mock()
        pool = Pool(mock_factory, initial_connections=10)

        eq_(10, mock_factory.call_count)
        eq_(10, pool.size)
Esempio n. 13
0
 def extend_prices(_pool: Pool):
     with _pool.connection() as client:
         l = client.get_prices() or []
     for x in l:
         x['time'] = now
     prices.extend(l)
Esempio n. 14
0
    def test_drop_invalid_connection(self):
        pool = Pool(Mock())

        pool.drop(Mock())
Esempio n. 15
0
    def test_drop_using_connection(self):
        pool = Pool(Mock())

        conn1 = pool.acquire()
        pool.drop(conn1)
Esempio n. 16
0
 def test_max_connections(self):
     pool = Pool(Mock(), max_connections=1)
     for _ in range(2):
         pool.acquire()
Esempio n. 17
0
    def test_release_already_released_connection(self):
        pool = Pool(Mock())
        conn1 = pool.acquire()

        pool.release(conn1)
        pool.release(conn1)
Esempio n. 18
0
    def test_release_invalid_connection(self):
        pool = Pool(Mock())

        pool.release(Mock())