<commit_before>from __future__ import unicode_literals

import os
import unittest

from django import VERSION
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import ui
from selenium.webdriver.support.ui import Select

try:
    from taggit.models import Tag
except ImportError:
    Tag = None



if VERSION[0] == 1 and VERSION[1] < 7:
    # Patch for travis
    from django.test.testcases import StoppableWSGIServer

    def patient_shutdown(self):
        """
        Stops the serve_forever loop.

        Blocks until the loop has finished. This must be called while
        serve_forever() is running in another thread, or it will
        deadlock.
        """
        self._StoppableWSGIServer__serving = False
        if not self._StoppableWSGIServer__is_shut_down.wait(30):
            raise RuntimeError(
                "Failed to shutdown the live test server in 2 seconds. The "
                "server might be stuck or generating a slow response.")
    StoppableWSGIServer.shutdown = patient_shutdown

    from django.test import LiveServerTestCase
else:
    # LiveServerTestCase doesn't serve static files in 1.7 anymore
    from django.contrib.staticfiles.testing import StaticLiveServerTestCase as LiveServerTestCase


if os.environ.get('TRAVIS', False):
    WAIT_TIME = 30
elif os.environ.get('BUILD_ID', False):  #  Jenkins build server
    WAIT_TIME = 30
else:
    WAIT_TIME = 5


class WidgetTestCase(LiveServerTestCase):
    input_name_suffix = '-autocomplete'
    autocomplete_name = 'relation'
    fixtures = ['basic_fk_model_test_case.json', 'test_user.json']
    test_case_setup_done = False

    @classmethod
    def setUpClass(cls):
        if os.environ.get('TESTS_SKIP_LIVESERVER', False):
            raise unittest.SkipTest('TESTS_SKIP_LIVESERVER enabled')

        cls.selenium = getattr(webdriver, 
            os.environ.get('TESTS_WEBDRIVER', 'Firefox'))()

        cls.selenium.implicitly_wait(WAIT_TIME)
        super(WidgetTestCase, cls).setUpClass()

    @classmethod
    def tearDownClass(cls):
        super(WidgetTestCase, cls).tearDownClass()
        cls.selenium.quit()
        cls.test_case_setup_done = False

    def setUp(self):
        if self.__class__.test_case_setup_done is False:
            self.set_implicit_wait()
            self.setup_test_case()
        self.__class__.test_case_setup_done = True<commit_msg>tests: use global Selenium singleton<commit_after>from __future__ import unicode_literals

import atexit
import os
import unittest

from django import VERSION
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import ui
from selenium.webdriver.support.ui import Select

try:
    from taggit.models import Tag
except ImportError:
    Tag = None



if VERSION[0] == 1 and VERSION[1] < 7:
    # Patch for travis
    from django.test.testcases import StoppableWSGIServer

    def patient_shutdown(self):
        """
        Stops the serve_forever loop.

        Blocks until the loop has finished. This must be called while
        serve_forever() is running in another thread, or it will
        deadlock.
        """
        self._StoppableWSGIServer__serving = False
        if not self._StoppableWSGIServer__is_shut_down.wait(30):
            raise RuntimeError(
                "Failed to shutdown the live test server in 2 seconds. The "
                "server might be stuck or generating a slow response.")
    StoppableWSGIServer.shutdown = patient_shutdown

    from django.test import LiveServerTestCase
else:
    # LiveServerTestCase doesn't serve static files in 1.7 anymore
    from django.contrib.staticfiles.testing import StaticLiveServerTestCase as LiveServerTestCase


if os.environ.get('TRAVIS', False):
    WAIT_TIME = 30
elif os.environ.get('BUILD_ID', False):  #  Jenkins build server
    WAIT_TIME = 30
else:
    WAIT_TIME = 5


# Global Selenium instance.
class Selenium():
    selenium = None

    def __new__(cls):
        if not cls.selenium:
            selenium = getattr(webdriver, os.environ.get('TESTS_WEBDRIVER', 'Firefox'))()
            selenium.implicitly_wait(WAIT_TIME)
            atexit.register(selenium.quit)
            cls.selenium = selenium
        return cls.selenium


class WidgetTestCase(LiveServerTestCase):
    input_name_suffix = '-autocomplete'
    autocomplete_name = 'relation'
    fixtures = ['basic_fk_model_test_case.json', 'test_user.json']
    test_case_setup_done = False

    @classmethod
    def setUpClass(cls):
        if os.environ.get('TESTS_SKIP_LIVESERVER', False):
            raise unittest.SkipTest('TESTS_SKIP_LIVESERVER enabled')

        cls.selenium = Selenium()
        super(WidgetTestCase, cls).setUpClass()

    @classmethod
    def tearDownClass(cls):
        super(WidgetTestCase, cls).tearDownClass()
        cls.test_case_setup_done = False

    def setUp(self):
        if self.__class__.test_case_setup_done is False:
            self.set_implicit_wait()
            self.setup_test_case()
        self.__class__.test_case_setup_done = True
{'fraction_non_alphanumeric': 0.07447224394057858, 'fraction_numerical': 0.00547302580140735, 'mean_word_length': 3.0708035003977723, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6632084', 'n_tokens_mistral': 1601, 'n_tokens_neox': 1488, 'n_words': 395}