<commit_before>#!/usr/bin/env python2
# coding: utf-8

import os
import subprocess
import time
import unittest

import daemonize

this_base = os.path.dirname(__file__)


def subproc(script):

    subproc = subprocess.Popen(['sh'],
                               close_fds=True,
                               stdin=subprocess.PIPE,
                               stdout=subprocess.PIPE,
                               stderr=subprocess.PIPE)

    out, err = subproc.communicate(script)

    subproc.wait()
    return (subproc.returncode, out, err)


def read_file(fn):
    try:
        with open(fn, 'r') as f:
            cont = f.read()
            print cont
            return cont
    except EnvironmentError as e:
        return None


class TestDaemonize(unittest.TestCase):

    foo_fn = '/tmp/foo'
    bar_fn = '/tmp/bar'
    pidfn = '/tmp/test_daemonize.pid'

    def setUp(self):

        # kill foo.py and kill bar.py
        # bar.py might be waiting for foo.py to release lock-file.
        try:
            subproc('python2 {b}/foo.py stop'.format(b=this_base))
        except Exception as e:
            print repr(e)

        time.sleep(0.1)

        try:
            subproc('python2 {b}/foo.py stop'.format(b=this_base))
        except Exception as e:
            print repr(e)

        # remove written file

        try:
            os.unlink(self.foo_fn)
        except EnvironmentError as e:
            pass

        try:
            os.unlink(self.bar_fn)
        except EnvironmentError as e:
            pass

    def tearDown(self):
        self.setUp()

    def test_start(self):

        subproc('python2 {b}/foo.py start'.format(b=this_base))
        time.sleep(0.2)

        self.assertEqual('foo-before', read_file(self.foo_fn))
        time.sleep(1)
        self.assertEqual('foo-after', read_file(self.foo_fn))

    def test_stop(self):

        subproc('python2 {b}/foo.py start'.format(b=this_base))
        time.sleep(0.2)

        self.assertEqual('foo-before', read_file(self.foo_fn), 'foo started')

        subproc('python2 {b}/foo.py stop'.format(b=this_base))
        time.sleep(0.2)

        self.assertEqual('foo-before', read_file(self.foo_fn),
                         'process has been kill thus no content is updated')

    def test_restart(self):

        subproc('python2 {b}/foo.py start'.format(b=this_base))
        time.sleep(0.2)

        self.assertEqual('foo-before', read_file(self.foo_fn))
<commit_msg>refine test of daemonize<commit_after>#!/usr/bin/env python2
# coding: utf-8

import os
import subprocess
import time
import unittest

import daemonize

this_base = os.path.dirname(__file__)


def subproc(script):

    subproc = subprocess.Popen(['sh'],
                               close_fds=True,
                               stdin=subprocess.PIPE,
                               stdout=subprocess.PIPE,
                               stderr=subprocess.PIPE)

    out, err = subproc.communicate(script)

    subproc.wait()
    return (subproc.returncode, out, err)


def read_file(fn):
    try:
        with open(fn, 'r') as f:
            cont = f.read()
            print cont
            return cont
    except EnvironmentError as e:
        return None


class TestDaemonize(unittest.TestCase):

    foo_fn = '/tmp/foo'
    bar_fn = '/tmp/bar'
    pidfn = '/tmp/test_daemonize.pid'

    def _clean(self):

        # kill foo.py and kill bar.py
        # bar.py might be waiting for foo.py to release lock-file.
        try:
            subproc('python2 {b}/foo.py stop'.format(b=this_base))
        except Exception as e:
            print repr(e)

        time.sleep(0.1)

        try:
            subproc('python2 {b}/bar.py stop'.format(b=this_base))
        except Exception as e:
            print repr(e)

        # remove written file

        try:
            os.unlink(self.foo_fn)
        except EnvironmentError as e:
            pass

        try:
            os.unlink(self.bar_fn)
        except EnvironmentError as e:
            pass

    def setUp(self):
        self.clean()

    def tearDown(self):
        self.clean()

    def test_start(self):

        subproc('python2 {b}/foo.py start'.format(b=this_base))
        time.sleep(0.2)

        self.assertEqual('foo-before', read_file(self.foo_fn))
        time.sleep(1)
        self.assertEqual('foo-after', read_file(self.foo_fn))

    def test_stop(self):

        subproc('python2 {b}/foo.py start'.format(b=this_base))
        time.sleep(0.2)

        self.assertEqual('foo-before', read_file(self.foo_fn), 'foo started')

        subproc('python2 {b}/foo.py stop'.format(b=this_base))
        time.sleep(0.2)

        self.assertEqual('foo-before', read_file(self.foo_fn),
                         'process has been kill thus no content is updated')

    def test_restart(self):

        subproc('python2 {b}/foo.py start'.format(b=this_base))
        time.sleep(0.2)

        self.assertEqual('foo-before', read_file(self.foo_fn))

{'fraction_non_alphanumeric': 0.11523790403533427, 'fraction_numerical': 0.007628990162617948, 'mean_word_length': 1.894828588030215, 'pattern_counts': {'":': 0, '<': 3, '<?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': '4916355', 'n_tokens_mistral': 1601, 'n_tokens_neox': 1535, 'n_words': 328}