# Path: weblablib/tasks.py
# class _TaskRunner(threading.Thread):
# 
#     _instances = []
#     _STEPS_WAITING = 20
# 
#     def __init__(self, number, weblab, app):
#         super(_TaskRunner, self).__init__()
#         self.name = 'weblab-task-runner-{}'.format(number)
#         self.daemon = True
#         self.app = app
#         self.weblab = weblab
#         self._stopping = False
# 
#     def stop(self):
#         self._stopping = True
# 
#     def run(self):
#         _TaskRunner._instances.append(self)
# 
#         while not self._stopping:
#             try:
#                 with self.app.app_context():
#                     self.weblab.run_tasks()
#             except redis.ConnectionError:
#                 # In the case of a redis ConnectionError, let's wait a bit more to see if
#                 # this happens again. It can be that we are just restarting the server
#                 # and Redis died before, or a Redis upgrade or so.
#                 traceback.print_exc()
#                 time.sleep(5)
#             except Exception:
#                 traceback.print_exc()
#                 continue
# 
#             for _ in six.moves.range(_TaskRunner._STEPS_WAITING):
#                 time.sleep(0.05)
#                 if self._stopping:
#                     break


import os
import sys
import json
import time
import base64
import datetime
import requests
import threading
import six
import flask.cli as flask_cli
import weblablib
import unittest
        import weblablib.views as weblablib_views
    from StringIO import StringIO
    from io import StringIO
from flask import Flask, url_for, render_template_string, g, session
from weblablib.tasks import _TaskRunner
from click.testing import CliRunner
    from flask_socketio import SocketIO, SocketIOTestClient
        return config

    def lab(self):
        # We can't know which one will be running first, so we force one to be running first
        task_object = self.unique_task.delay()
        while not self.task_is_running:
            time.sleep(0.05)
        
        # Now we now that the task is running. Let's run it again
        task2 = self.unique_task.delay()
        task2.join() # Should be fast
        if not task2.failed:
            raise Exception("unique=global should have avoided the second task")
        if task2.error['class'] != 'AlreadyRunningError':
            raise Exception("unique=global expected an AlreadyRunningError; got {}".format(task2.error))

        return str(task_object.task_id)

    def task(self):
        initial = time.time()
        self.task_is_running = True

        while not weblablib.current_task_stopping:
            time.sleep(0.05)

            elapsed = time.time() - initial
            if elapsed > 6:
                raise Exception("Error, nobody calling stop")

    def test_global_unique(self):

# Path: weblablib/tasks.py
# class _TaskRunner(threading.Thread):
# 
#     _instances = []
#     _STEPS_WAITING = 20
# 
#     def __init__(self, number, weblab, app):
#         super(_TaskRunner, self).__init__()
#         self.name = 'weblab-task-runner-{}'.format(number)
#         self.daemon = True
#         self.app = app
#         self.weblab = weblab
#         self._stopping = False
# 
#     def stop(self):
#         self._stopping = True
# 
#     def run(self):
#         _TaskRunner._instances.append(self)
# 
#         while not self._stopping:
#             try:
#                 with self.app.app_context():
#                     self.weblab.run_tasks()
#             except redis.ConnectionError:
#                 # In the case of a redis ConnectionError, let's wait a bit more to see if
#                 # this happens again. It can be that we are just restarting the server
#                 # and Redis died before, or a Redis upgrade or so.
#                 traceback.print_exc()
#                 time.sleep(5)
#             except Exception:
#                 traceback.print_exc()
#                 continue
# 
#             for _ in six.moves.range(_TaskRunner._STEPS_WAITING):
#                 time.sleep(0.05)
#                 if self._stopping:
#                     break


# Path: test_weblablib.py
import os
import sys
import json
import time
import base64
import datetime
import requests
import threading
import six
import flask.cli as flask_cli
import weblablib
import unittest
        import weblablib.views as weblablib_views
    from StringIO import StringIO
    from io import StringIO
from flask import Flask, url_for, render_template_string, g, session
from weblablib.tasks import _TaskRunner
from click.testing import CliRunner
    from flask_socketio import SocketIO, SocketIOTestClient

        return config

    def lab(self):
        # We can't know which one will be running first, so we force one to be running first
        task_object = self.unique_task.delay()
        while not self.task_is_running:
            time.sleep(0.05)
        
        # Now we now that the task is running. Let's run it again
        task2 = self.unique_task.delay()
        task2.join() # Should be fast
        if not task2.failed:
            raise Exception("unique=global should have avoided the second task")
        if task2.error['class'] != 'AlreadyRunningError':
            raise Exception("unique=global expected an AlreadyRunningError; got {}".format(task2.error))

        return str(task_object.task_id)

    def task(self):
        initial = time.time()
        self.task_is_running = True

        while not weblablib.current_task_stopping:
            time.sleep(0.05)

            elapsed = time.time() - initial
            if elapsed > 6:
                raise Exception("Error, nobody calling stop")

    def test_global_unique(self):

        original_steps_waiting = _TaskRunner._STEPS_WAITING