-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #104 from spirosdelviniotis/hepcrawl_refactor_tests
global: add testlib module for reusability
- Loading branch information
Showing
35 changed files
with
204 additions
and
153 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
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,91 @@ | ||
# -*- 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 | ||
|
||
import logging | ||
|
||
LOGGER = logging.getLogger(__name__) | ||
|
||
|
||
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']) | ||
LOGGER.info('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']) | ||
LOGGER.info('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, # iterable | ||
self.monitor_iter_limit # stop | ||
)) | ||
|
||
@classmethod | ||
def do_crawl( | ||
cls, | ||
app, | ||
monitor_timeout, | ||
monitor_iter_limit, | ||
crawler_instance, | ||
project='hepcrawl', | ||
spider='WSP', | ||
settings=None, | ||
**crawler_arguments | ||
): | ||
settings = settings or {} | ||
|
||
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, | ||
**crawler_arguments | ||
) | ||
|
||
return my_monitor.results |
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
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
File renamed without changes.
File renamed without changes.
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 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 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
Oops, something went wrong.