コード例 #1
0
 def setUp(self):
     super(TestCache, self).setUp()
     self.api_key = ApiKeyFactory(fallback_cache_expire=60)
     self.cache = FallbackCache(self.raven_client, self.redis_client,
                                self.stats_client)
コード例 #2
0
def cache(raven, redis, session, stats):
    yield FallbackCache(raven, redis, stats)
コード例 #3
0
class TestCache(QueryTest):
    def setUp(self):
        super(TestCache, self).setUp()
        self.api_key = ApiKeyFactory(fallback_cache_expire=60)
        self.cache = FallbackCache(self.raven_client, self.redis_client,
                                   self.stats_client)

    def _query(self, **kwargs):
        return Query(api_key=self.api_key, **kwargs)

    def test_get_blue(self):
        blues = BlueShardFactory.build_batch(2)
        query = self._query(blue=self.blue_model_query(blues))
        self.assertEqual(self.cache.get(query), None)
        self.check_stats(counter=[
            ('locate.fallback.cache', 1, 1, ['status:miss']),
        ])

    def test_set_blue(self):
        blues = BlueShardFactory.build_batch(2)
        blue = blues[0]
        query = self._query(blue=self.blue_model_query(blues))
        result = ExternalResult(blue.lat, blue.lon, blue.radius, None)
        self.cache.set(query, result)
        self.assertEqual(self.cache.get(query), result)
        self.check_stats(counter=[
            ('locate.fallback.cache', 1, 1, ['status:hit']),
        ])

    def test_get_cell(self):
        cells = CellShardFactory.build_batch(1)
        query = self._query(cell=self.cell_model_query(cells))
        self.assertEqual(self.cache.get(query), None)
        self.check_stats(counter=[
            ('locate.fallback.cache', 1, 1, ['status:miss']),
        ])

    def test_set_cell(self):
        cell = CellShardFactory.build()
        query = self._query(cell=self.cell_model_query([cell]))
        result = ExternalResult(cell.lat, cell.lon, cell.radius, None)
        self.cache.set(query, result, expire=60)
        keys = self.redis_client.keys('cache:fallback:cell:*')
        self.assertEqual(len(keys), 1)
        self.assertTrue(50 < self.redis_client.ttl(keys[0]) <= 60)
        self.assertEqual(self.cache.get(query), result)
        self.check_stats(counter=[
            ('locate.fallback.cache', 1, 1, ['status:hit']),
        ])

    def test_set_cell_not_found(self):
        cell = CellShardFactory.build()
        query = self._query(cell=self.cell_model_query([cell]))
        result = ExternalResult(None, None, None, None)
        self.cache.set(query, result)
        keys = self.redis_client.keys('cache:fallback:cell:*')
        self.assertEqual(len(keys), 1)
        self.assertEqual(self.redis_client.get(keys[0]), b'"404"')
        self.assertEqual(self.cache.get(query), result)
        self.check_stats(counter=[
            ('locate.fallback.cache', 1, 1, ['status:hit']),
        ])

    def test_get_cell_multi(self):
        cells = CellShardFactory.build_batch(2)
        query = self._query(cell=self.cell_model_query(cells))
        self.assertEqual(self.cache.get(query), None)
        self.check_stats(counter=[
            ('locate.fallback.cache', 1, 1, ['status:bypassed']),
        ])

    def test_get_wifi(self):
        wifis = WifiShardFactory.build_batch(2)
        query = self._query(wifi=self.wifi_model_query(wifis))
        self.assertEqual(self.cache.get(query), None)
        self.check_stats(counter=[
            ('locate.fallback.cache', 1, 1, ['status:miss']),
        ])

    def test_set_wifi(self):
        wifis = WifiShardFactory.build_batch(2)
        wifi = wifis[0]
        query = self._query(wifi=self.wifi_model_query(wifis))
        result = ExternalResult(wifi.lat, wifi.lon, wifi.radius, None)
        self.cache.set(query, result)
        self.assertEqual(self.cache.get(query), result)
        self.check_stats(counter=[
            ('locate.fallback.cache', 1, 1, ['status:hit']),
        ])

    def test_set_wifi_inconsistent(self):
        wifis1 = WifiShardFactory.build_batch(2)
        self.cache.set(self._query(wifi=self.wifi_model_query(wifis1)),
                       ExternalResult(wifis1[0].lat, wifis1[0].lon, 100, None))

        # similar lat/lon, worse accuracy
        wifis2 = WifiShardFactory.build_batch(2,
                                              lat=wifis1[0].lat + 0.0001,
                                              lon=wifis1[0].lon)
        self.cache.set(self._query(wifi=self.wifi_model_query(wifis2)),
                       ExternalResult(wifis2[0].lat, wifis2[0].lon, 200, None))

        # check combined query, avg lat/lon, max accuracy
        query = self._query(wifi=self.wifi_model_query(wifis1 + wifis2))
        cached = self.cache.get(query)
        self.assertAlmostEqual(cached[0], (wifis1[0].lat + wifis2[0].lat) / 2)
        self.assertAlmostEqual(cached[1], wifis1[0].lon)
        self.assertAlmostEqual(cached[2], 205.56, 2)
        self.assertTrue(cached[3] is None)

        # different lat/lon
        wifis3 = WifiShardFactory.build_batch(2, lat=wifis1[0].lat + 10.0)
        self.cache.set(self._query(wifi=self.wifi_model_query(wifis3)),
                       ExternalResult(wifis3[0].lat, wifis3[0].lon, 300, None))

        # check combined query, inconsistent result
        query = self._query(wifi=self.wifi_model_query(wifis1 + wifis2 +
                                                       wifis3))
        self.assertEqual(self.cache.get(query), None)

        self.check_stats(counter=[
            ('locate.fallback.cache', 1, 1, ['status:hit']),
            ('locate.fallback.cache', 1, 1, ['status:inconsistent']),
        ])

    def test_get_mixed(self):
        blues = BlueShardFactory.build_batch(2)
        cells = CellShardFactory.build_batch(1)
        wifis = WifiShardFactory.build_batch(2)

        query = self._query(cell=self.cell_model_query(cells),
                            wifi=self.wifi_model_query(wifis))
        self.assertEqual(self.cache.get(query), None)

        query = self._query(blue=self.blue_model_query(blues),
                            cell=self.cell_model_query(cells))
        self.assertEqual(self.cache.get(query), None)

        query = self._query(blue=self.blue_model_query(blues),
                            wifi=self.wifi_model_query(wifis))
        self.assertEqual(self.cache.get(query), None)

        self.check_stats(counter=[
            ('locate.fallback.cache', 3, 1, ['status:bypassed']),
        ])
コード例 #4
0
ファイル: test_fallback.py プロジェクト: voolitels/ichnaea
 def setUp(self):
     super(TestCache, self).setUp()
     self.cache = FallbackCache(
         self.raven_client, self.redis_client, self.stats_client,
         cache_expire=600)
コード例 #5
0
ファイル: test_fallback.py プロジェクト: voolitels/ichnaea
class TestCache(QueryTest):

    def setUp(self):
        super(TestCache, self).setUp()
        self.cache = FallbackCache(
            self.raven_client, self.redis_client, self.stats_client,
            cache_expire=600)

    def test_disabled(self):
        cache = DisabledCache()
        query = Query()
        self.assertEqual(
            cache.set(query, ExternalResult(None, None, None, None)), None)
        self.assertEqual(cache.get(query), None)

    def test_get_cell(self):
        cells = CellFactory.build_batch(1)
        query = Query(cell=self.cell_model_query(cells))
        self.assertEqual(self.cache.get(query), None)
        self.check_stats(counter=[
            ('locate.fallback.cache', 1, 1, ['status:miss']),
        ])

    def test_set_cell(self):
        cell = CellFactory.build()
        query = Query(cell=self.cell_model_query([cell]))
        result = ExternalResult(cell.lat, cell.lon, cell.radius, None)
        self.cache.set(query, result)
        keys = self.redis_client.keys('cache:fallback:cell:*')
        self.assertEqual(len(keys), 1)
        self.assertTrue(500 < self.redis_client.ttl(keys[0]) <= 600)
        self.assertEqual(self.cache.get(query), result)
        self.check_stats(counter=[
            ('locate.fallback.cache', 1, 1, ['status:hit']),
        ])

    def test_set_cell_not_found(self):
        cell = CellFactory.build()
        query = Query(cell=self.cell_model_query([cell]))
        result = ExternalResult(None, None, None, None)
        self.cache.set(query, result)
        keys = self.redis_client.keys('cache:fallback:cell:*')
        self.assertEqual(len(keys), 1)
        self.assertEqual(self.redis_client.get(keys[0]), b'"404"')
        self.assertEqual(self.cache.get(query), result)
        self.check_stats(counter=[
            ('locate.fallback.cache', 1, 1, ['status:hit']),
        ])

    def test_get_cell_multi(self):
        cells = CellFactory.build_batch(2)
        query = Query(cell=self.cell_model_query(cells))
        self.assertEqual(self.cache.get(query), None)
        self.check_stats(counter=[
            ('locate.fallback.cache', 1, 1, ['status:bypassed']),
        ])

    def test_get_wifi(self):
        wifis = WifiShardFactory.build_batch(2)
        query = Query(wifi=self.wifi_model_query(wifis))
        self.assertEqual(self.cache.get(query), None)
        self.check_stats(counter=[
            ('locate.fallback.cache', 1, 1, ['status:miss']),
        ])

    def test_set_wifi(self):
        wifis = WifiShardFactory.build_batch(2)
        wifi = wifis[0]
        query = Query(wifi=self.wifi_model_query(wifis))
        result = ExternalResult(wifi.lat, wifi.lon, wifi.radius, None)
        self.cache.set(query, result)
        self.assertEqual(self.cache.get(query), result)
        self.check_stats(counter=[
            ('locate.fallback.cache', 1, 1, ['status:hit']),
        ])

    def test_set_wifi_inconsistent(self):
        wifis1 = WifiShardFactory.build_batch(2)
        self.cache.set(
            Query(wifi=self.wifi_model_query(wifis1)),
            ExternalResult(wifis1[0].lat, wifis1[0].lon, 100, None))

        # similar lat/lon, worse accuracy
        wifis2 = WifiShardFactory.build_batch(
            2, lat=wifis1[0].lat + 0.0001, lon=wifis1[0].lon)
        self.cache.set(
            Query(wifi=self.wifi_model_query(wifis2)),
            ExternalResult(wifis2[0].lat, wifis2[0].lon, 200, None))

        # check combined query, avg lat/lon, max accuracy
        query = Query(wifi=self.wifi_model_query(wifis1 + wifis2))
        self.assertEqual(
            self.cache.get(query),
            ((wifis1[0].lat + wifis2[0].lat) / 2, wifis1[0].lon, 200, None))

        # different lat/lon
        wifis3 = WifiShardFactory.build_batch(2, lat=wifis1[0].lat + 10.0)
        self.cache.set(
            Query(wifi=self.wifi_model_query(wifis3)),
            ExternalResult(wifis3[0].lat, wifis3[0].lon, 300, None))

        # check combined query, inconsistent result
        query = Query(wifi=self.wifi_model_query(wifis1 + wifis2 + wifis3))
        self.assertEqual(self.cache.get(query), None)

        self.check_stats(counter=[
            ('locate.fallback.cache', 1, 1, ['status:hit']),
            ('locate.fallback.cache', 1, 1, ['status:inconsistent']),
        ])

    def test_get_mixed(self):
        cells = CellFactory.build_batch(1)
        wifis = WifiShardFactory.build_batch(2)
        query = Query(
            cell=self.cell_model_query(cells),
            wifi=self.wifi_model_query(wifis))
        self.assertEqual(self.cache.get(query), None)
        self.check_stats(counter=[
            ('locate.fallback.cache', 1, 1, ['status:bypassed']),
        ])
コード例 #6
0
ファイル: test_fallback.py プロジェクト: gasanq/ichnaea
def unwiredlabs_cache(raven, redis, session):
    yield FallbackCache(raven, redis, schema=UNWIREDLABS_V1_SCHEMA)
コード例 #7
0
ファイル: test_fallback.py プロジェクト: gasanq/ichnaea
def cache(raven, redis, session):
    yield FallbackCache(raven, redis)