<commit_before>from django.test import TestCase
from rq import Queue
from redis import Redis
from subprocess import Popen

try:
    from subprocess import DEVNULL as devnull
except ImportError:
    from os import devnull  # fix for python < 3.3

def add(x, y):
    return x + y

class QueueTestCase(TestCase):
    def test_queued_code(self):
        # make sure an rqworker is running
        rqworker = Popen("rqworker", shell=True, stdout=devnull, stderr=devnull)

        q = Queue(connection=Redis())
        job = q.enqueue(add, 2, 2)

        while not job.result:
            pass

        try:
            self.assertEqual(add(2, 2), job.result)
        finally:
            # kill the rqworker, regardless of succcess
            rqworker.terminate()<commit_msg>Previous devnull strat didn't work on Python 2<commit_after>from django.test import TestCase
from rq import Queue
from redis import Redis
from subprocess import call

try:
    from subprocess import DEVNULL as devnull
except ImportError:
    import os
    devnull = open(os.devnull, 'w')

def add(x, y):
    return x + y

class QueueTestCase(TestCase):
    def test_queued_code(self):
        """Test a simple queuing task with rq"""

        # make sure an rqworker is running
        rqworker = call("rqworker", shell=True, stdout=devnull, stderr=devnull, close_fds=True)

        q = Queue(connection=Redis())
        job = q.enqueue(add, 2, 2)

        while not job.result:
            pass

        try:
            self.assertEqual(add(2, 2), job.result)
        finally:
            # kill the rqworker, regardless of succcess
            rqworker.terminate()

    def test_scheduled_code(self):
        """Test scheduling something with rq-scheduler"""
{'fraction_non_alphanumeric': 0.08371824480369515, 'fraction_numerical': 0.006351039260969977, 'mean_word_length': 2.617954070981211, 'pattern_counts': {'":': 0, '<': 4, '<?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': '5200344', 'n_tokens_mistral': 538, 'n_tokens_neox': 515, 'n_words': 170}