mirror of
https://github.com/internetarchive/warcprox.git
synced 2025-01-18 13:22:09 +01:00
added tests for purging stale services and minimal tests for command line tool
This commit is contained in:
parent
dd5b2122cf
commit
df7c0b8e32
@ -54,4 +54,5 @@ def purge_stale_services(argv=None):
|
|||||||
|
|
||||||
rethinker = doublethink.Rethinker(servers=args.servers, db=args.database)
|
rethinker = doublethink.Rethinker(servers=args.servers, db=args.database)
|
||||||
registry = doublethink.services.ServiceRegistry(rethinker)
|
registry = doublethink.services.ServiceRegistry(rethinker)
|
||||||
registry.purge_stale_services()
|
registry.purge_stale_services()
|
||||||
|
sys.exit(0)
|
@ -17,14 +17,25 @@ limitations under the License.
|
|||||||
'''
|
'''
|
||||||
|
|
||||||
import doublethink
|
import doublethink
|
||||||
|
import doublethink.cli
|
||||||
import logging
|
import logging
|
||||||
import sys
|
import sys
|
||||||
import pytest
|
import pytest
|
||||||
import rethinkdb as r
|
import rethinkdb as r
|
||||||
|
import pkg_resources
|
||||||
|
|
||||||
logging.basicConfig(stream=sys.stderr, level=logging.INFO,
|
logging.basicConfig(stream=sys.stderr, level=logging.INFO,
|
||||||
format="%(asctime)s %(process)d %(levelname)s %(threadName)s %(name)s.%(funcName)s(%(filename)s:%(lineno)d) %(message)s")
|
format="%(asctime)s %(process)d %(levelname)s %(threadName)s %(name)s.%(funcName)s(%(filename)s:%(lineno)d) %(message)s")
|
||||||
|
|
||||||
|
class RethinkerForTesting(doublethink.Rethinker):
|
||||||
|
def __init__(self, *args, **kwargs):
|
||||||
|
super(RethinkerForTesting, self).__init__(*args, **kwargs)
|
||||||
|
|
||||||
|
def _random_server_connection(self):
|
||||||
|
self.last_conn = super(RethinkerForTesting, self)._random_server_connection()
|
||||||
|
# logging.info("self.last_conn=%s", self.last_conn)
|
||||||
|
return self.last_conn
|
||||||
|
|
||||||
@pytest.fixture(scope="module")
|
@pytest.fixture(scope="module")
|
||||||
def rr():
|
def rr():
|
||||||
rr = RethinkerForTesting()
|
rr = RethinkerForTesting()
|
||||||
@ -37,6 +48,16 @@ def rr():
|
|||||||
assert result["dbs_created"] == 1
|
assert result["dbs_created"] == 1
|
||||||
return RethinkerForTesting(db="doublethink_test_db")
|
return RethinkerForTesting(db="doublethink_test_db")
|
||||||
|
|
||||||
def test_cli(rr):
|
def test_cli(capsys, rr):
|
||||||
print(rr)
|
entrypoint = pkg_resources.get_entry_map(
|
||||||
doublethink.cli.purge_stale_services(['test'])
|
'doublethink')['console_scripts']['purge-stale-services']
|
||||||
|
callable = entrypoint.resolve()
|
||||||
|
with pytest.raises(SystemExit) as exit:
|
||||||
|
callable(['purge-stale-services'])
|
||||||
|
print(dir(exit))
|
||||||
|
assert exit.value.code != 0
|
||||||
|
out, err = capsys.readouterr()
|
||||||
|
with pytest.raises(SystemExit) as exit:
|
||||||
|
callable(['purge-stale-services', '-d', 'test'])
|
||||||
|
assert exit.value.code == 0
|
||||||
|
out, err = capsys.readouterr()
|
||||||
|
@ -258,3 +258,13 @@ def test_svcreg_heartbeat_server_down(rr):
|
|||||||
assert not 'host' in svc0
|
assert not 'host' in svc0
|
||||||
assert not 'pid' in svc0
|
assert not 'pid' in svc0
|
||||||
|
|
||||||
|
def test_purge_stale_services(rr):
|
||||||
|
rr.table('services').delete().run()
|
||||||
|
rr.table('services').insert({ 'id': 'old-service', "last_heartbeat": r.now(), 'ttl': 0.4 }).run()
|
||||||
|
time.sleep(1)
|
||||||
|
rr.table('services').insert({ 'id': 'new-service', "last_heartbeat": r.now(), 'ttl': 0.4 }).run()
|
||||||
|
svcreg = doublethink.ServiceRegistry(rr)
|
||||||
|
assert rr.table('services').count().run() == 2
|
||||||
|
svcreg.purge_stale_services()
|
||||||
|
assert rr.table('services').count().run() == 1
|
||||||
|
rr.table('services').delete().run()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user