remove compatibility for old index file in environment.py (keep testing coverage though)

This commit is contained in:
Olivier Yiptong 2015-04-22 16:26:43 -04:00
Родитель 377bd03e53
Коммит f707c4cd38
2 изменённых файлов: 28 добавлений и 48 удалений

Просмотреть файл

@ -139,28 +139,13 @@ class Environment(object):
raise EnvironmentUninitializedError("Cannot obtain instance if uninitialized")
def _convert_legacy_index(legacy_index):
"""Given a tile index, convert to the current index format expected by API endpoints"""
index = {}
for k, v in legacy_index.iteritems():
index[k] = {'legacy': v}
return index
def _read_tile_index_loop(env, failure_sleep_duration=5, success_sleep_duration=15 * 60):
"""wait for 15 minutes (greenlet), then open tile index file and replace LINKS_LOCALIZATIONS"""
while True:
try:
with open(os.path.join(env.config.TILE_INDEX_DIR, env.config.TILE_INDEX_FILE), "r") as fp:
data = fp.read()
index = ujson.decode(data)
api_version = index.get('__ver__', 0)
if api_version == 3:
env.config.LINKS_LOCALIZATIONS = index
else:
env.config.LINKS_LOCALIZATIONS = _convert_legacy_index(index)
env.config.LINKS_LOCALIZATIONS = ujson.decode(data)
gevent.sleep(success_sleep_duration)
except Exception, e:
env.log_dict(name="application", action="gevent_tiles_update_error", message={

Просмотреть файл

@ -4,43 +4,12 @@ import gevent
from tempfile import NamedTemporaryFile
from nose.tools import assert_equals
from mock import Mock, PropertyMock
from onyx.environment import Environment, _convert_legacy_index, _read_tile_index_loop
from onyx.environment import Environment, _read_tile_index_loop
from tests.base import BaseTestCase
class TestTileIndexFormats(BaseTestCase):
def test_legacy_format(self):
"""
Tests legacy tile index format
"""
converted = _convert_legacy_index({'STAR/en-US': 'https://example.com/en-US-distro.json'})
assert_equals({'STAR/en-US': {'legacy': 'https://example.com/en-US-distro.json'}}, converted)
class TestReadLoop(BaseTestCase):
def test_read_legacy(self):
"""
Test reading legacy tile indexes
"""
env = Environment.instance()
test_file = NamedTemporaryFile()
env.config = Mock()
env.config.TILE_INDEX_DIR, env.config.TILE_INDEX_FILE = os.path.split(test_file.name)
json.dump({'STAR/en-US': 'https://example.com/en-US-distro.json'}, test_file)
test_file.flush()
index_mock = PropertyMock()
type(env.config).LINKS_LOCALIZATIONS = index_mock
gevent.spawn(_read_tile_index_loop, env)
gevent.sleep(0) # make the event loop tick
index_mock.assert_any_call({'STAR/en-US': {'legacy': 'https://example.com/en-US-distro.json'}})
def test_v3_index(self):
"""
Test reading v3 tile indexes
@ -62,3 +31,29 @@ class TestReadLoop(BaseTestCase):
gevent.sleep(0) # make the event loop tick
index_mock.assert_any_call(v3_data)
def test_index_failure(self):
"""
Test json file read failure
"""
env = Environment.instance()
test_file = NamedTemporaryFile()
env.config = Mock()
env.config.TILE_INDEX_DIR, env.config.TILE_INDEX_FILE = os.path.split(test_file.name)
env.log_dict = Mock()
v3_data = "{'STAR/en-US': {'legacy': 'data'}, '__ver__': 3"
test_file.write(v3_data)
test_file.flush()
index_mock = PropertyMock()
type(env.config).LINKS_LOCALIZATIONS = index_mock
gevent.spawn(_read_tile_index_loop, env)
gevent.sleep(0) # make the event loop tick
assert_equals(0, index_mock.call_count)
env.log_dict.assert_any_calls()
assert_equals('gevent_tiles_update_error', env.log_dict.call_args[1]['action'])