forked from inspirehep/hepcrawl
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
global: add testlib module for reusability
* Adds celery_monitor module to testlib. * Adds tasks module to testlib. * Adds custom scrapyd runner for coverage. Closes inspirehep#100 Signed-off-by: Spiros Delviniotis <[email protected]>
- Loading branch information
1 parent
a587174
commit e74f9bb
Showing
10 changed files
with
105 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,10 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# This file is part of hepcrawl. | ||
# Copyright (C) 2015, 2016, 2017 CERN. | ||
# Copyright (C) 2017 CERN. | ||
# | ||
# hepcrawl is a free software; you can redistribute it and/or modify it | ||
# under the terms of the Revised BSD License; see LICENSE file for | ||
# more details. | ||
|
||
from __future__ import absolute_import, print_function, unicode_literals |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# This file is part of hepcrawl. | ||
# Copyright (C) 2017 CERN. | ||
# | ||
# hepcrawl is a free software; you can redistribute it and/or modify it | ||
# under the terms of the Revised BSD License; see LICENSE file for | ||
# more details. | ||
|
||
"""Celery monitor dealing with celery tasks for functional tests.""" | ||
|
||
from __future__ import absolute_import, print_function, unicode_literals | ||
|
||
from itertools import islice | ||
|
||
|
||
class CeleryMonitor(object): | ||
def __init__(self, app, monitor_timeout=3, monitor_iter_limit=100): | ||
self.results = [] | ||
self.recv = None | ||
self.app = app | ||
self.connection = None | ||
self.monitor_timeout = monitor_timeout | ||
self.monitor_iter_limit = monitor_iter_limit | ||
|
||
def __enter__(self): | ||
state = self.app.events.State() | ||
|
||
def announce_succeeded_tasks(event): | ||
state.event(event) | ||
task = state.tasks.get(event['uuid']) | ||
print('TASK SUCCEEDED: %s[%s] %s' % (task.name, task.uuid, task.info(),)) | ||
tasks = self.app.AsyncResult(task.id) | ||
for task in tasks.result: | ||
self.results.append(task) | ||
self.recv.should_stop = True | ||
|
||
def announce_failed_tasks(event): | ||
state.event(event) | ||
task = state.tasks.get(event['uuid']) | ||
print('TASK FAILED: %s[%s] %s' % (task.name, task.uuid, task.info(),)) | ||
self.results.append(task.info()) | ||
self.recv.should_stop = True | ||
|
||
self.app.control.enable_events() | ||
self.connection = self.app.connection() | ||
self.recv = self.app.events.Receiver(self.connection, handlers={ | ||
'task-succeeded': announce_succeeded_tasks, | ||
'task-failed': announce_failed_tasks, | ||
}) | ||
|
||
return self | ||
|
||
def __exit__(self, exc_type, exc_val, exc_tb): | ||
events_iter = self.recv.itercapture(limit=None, timeout=self.monitor_timeout, wakeup=True) | ||
self._wait_for_results(events_iter) | ||
self.connection.__exit__() | ||
|
||
def _wait_for_results(self, events_iter): | ||
any(islice(events_iter, self.monitor_iter_limit)) | ||
|
||
@classmethod | ||
def do_crawl(cls, | ||
app, | ||
monitor_timeout, | ||
monitor_iter_limit, | ||
crawler_instance, | ||
project='hepcrawl', | ||
spider='WSP', | ||
settings=None, | ||
**crawler_arguments): | ||
|
||
if settings is None: | ||
settings = {} | ||
|
||
with cls(app, monitor_timeout=monitor_timeout, monitor_iter_limit=monitor_iter_limit) as my_monitor: | ||
crawler_instance.schedule( | ||
project=project, | ||
spider=spider, | ||
settings=settings or {}, | ||
**crawler_arguments | ||
) | ||
|
||
return my_monitor.results |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,4 +9,4 @@ | |
|
||
|
||
[scrapyd] | ||
runner = tests.functional.scrapyd_coverage_runner | ||
runner = hepcrawl.testlib.scrapyd_coverage_runner |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,10 @@ | ||
|
||
# -*- coding: utf-8 -*- | ||
# | ||
# This file is part of hepcrawl. | ||
# Copyright (C) 2017 CERN. | ||
# | ||
# hepcrawl is a free software; you can redistribute it and/or modify it | ||
# under the terms of the Revised BSD License; see LICENSE file for | ||
# more details. | ||
|
||
from __future__ import absolute_import, print_function, unicode_literals |