<commit_before>#!/usr/bin/env python

# Copyright 2014 Open Source Robotics Foundation

# 
#    Licensed under the Apache License, Version 2.0 (the "License");
#    you may not use this file except in compliance with the License.
#    You may obtain a copy of the License at
# 
#        http://www.apache.org/licenses/LICENSE-2.0
# 
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS,
#    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#    See the License for the specific language governing permissions and
#    limitations under the License.

import os
import subprocess
import shutil
import tempfile
import time
import unittest


class TempDirLockfile():
    def __enter__(self):
        self.dir = tempfile.mkdtemp(prefix=os.path.expanduser('~')+'/')
        return os.path.join(self.dir, 'lockfile')

    def __exit__(self, type, value, traceback):
        shutil.rmtree(self.dir)


class TestWithlock(unittest.TestCase):

    def test_basic(self):
        """ Check for basic functionality """
        with TempDirLockfile() as lf:
            cmd = "./withlock %s echo hello world" % lf
            self.assertEqual(subprocess.call(cmd.split()), 0)

    def test_version(self):
        """ Check for not crashing """
        cmd = "./withlock --version"
        self.assertEqual(subprocess.call(cmd.split()), 0)

    def test_help(self):
        """ Check for not crashing """
        cmd = "./withlock --help"
        self.assertEqual(subprocess.call(cmd.split()), 0)

    def test_group_writeable(self):
        """ Test a group writable location for protection """
        cmd = "./withlock /tmp/lockfile echo hello world"
        self.assertEqual(subprocess.call(cmd.split()), 3)

    def test_trylock(self):
        with TempDirLockfile() as lf:
            cmd = "./withlock %s sleep 5" % lf
            p = subprocess.Popen(cmd.split())
            cmd = "./withlock %s echo hello world" % lf
            retval = subprocess.call(cmd.split())
            p.kill()
            self.assertEqual(retval, 1)

    def test_trylock_quiet(self):
        with TempDirLockfile() as lf:
            cmd = "./withlock %s sleep 5" % lf
            p = subprocess.Popen(cmd.split())
            cmd = "./withlock -q %s echo hello world" % lf
            retval = subprocess.call(cmd.split())
            p.kill()
            self.assertEqual(retval, 0)


    def test_timeout(self):
        with TempDirLockfile() as lf:
            cmd = "./withlock %s sleep 5" % lf
            p = subprocess.Popen(cmd.split())
            cmd = "./withlock -w 2 %s echo hello world" % lf
            start_time = time.time()
            retval = subprocess.call(cmd.split())
            p.kill()
            self.assertEqual(retval, 1)
            delta = time.time() - start_time
            self.assertTrue(2.0 <= delta)
            self.assertTrue(delta < 3.0)
<commit_msg>wait to give blocking lock time to start before test lock<commit_after>#!/usr/bin/env python

# Copyright 2014 Open Source Robotics Foundation

# 
#    Licensed under the Apache License, Version 2.0 (the "License");
#    you may not use this file except in compliance with the License.
#    You may obtain a copy of the License at
# 
#        http://www.apache.org/licenses/LICENSE-2.0
# 
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS,
#    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#    See the License for the specific language governing permissions and
#    limitations under the License.

import os
import subprocess
import shutil
import tempfile
import time
import unittest


class TempDirLockfile():
    def __enter__(self):
        self.dir = tempfile.mkdtemp(prefix=os.path.expanduser('~')+'/')
        return os.path.join(self.dir, 'lockfile')

    def __exit__(self, type, value, traceback):
        shutil.rmtree(self.dir)


class TestWithlock(unittest.TestCase):

    def test_basic(self):
        """ Check for basic functionality """
        with TempDirLockfile() as lf:
            cmd = "./withlock %s echo hello world" % lf
            self.assertEqual(subprocess.call(cmd.split()), 0)

    def test_version(self):
        """ Check for not crashing """
        cmd = "./withlock --version"
        self.assertEqual(subprocess.call(cmd.split()), 0)

    def test_help(self):
        """ Check for not crashing """
        cmd = "./withlock --help"
        self.assertEqual(subprocess.call(cmd.split()), 0)

    def test_group_writeable(self):
        """ Test a group writable location for protection """
        cmd = "./withlock /tmp/lockfile echo hello world"
        self.assertEqual(subprocess.call(cmd.split()), 3)

    def test_trylock(self):
        with TempDirLockfile() as lf:
            cmd = "./withlock %s sleep 5" % lf
            p = subprocess.Popen(cmd.split())
            # make sure the above has started
            time.sleep(0.1)
            cmd = "./withlock %s echo hello world" % lf
            retval = subprocess.call(cmd.split())
            p.kill()
            self.assertEqual(retval, 1)

    def test_trylock_quiet(self):
        with TempDirLockfile() as lf:
            cmd = "./withlock %s sleep 5" % lf
            p = subprocess.Popen(cmd.split())
            # make sure the above has started
            time.sleep(0.1)
            cmd = "./withlock -q %s echo hello world" % lf
            retval = subprocess.call(cmd.split())
            p.kill()
            self.assertEqual(retval, 0)

    def test_timeout(self):
        with TempDirLockfile() as lf:
            cmd = "./withlock %s sleep 5" % lf
            p = subprocess.Popen(cmd.split())
            # make sure the above has started
            time.sleep(0.1)
            cmd = "./withlock -w 2 %s echo hello world" % lf
            start_time = time.time()
            retval = subprocess.call(cmd.split())
            p.kill()
            self.assertEqual(retval, 1)
            delta = time.time() - start_time
            self.assertTrue(2.0 <= delta)
            self.assertTrue(delta < 3.0)

{'fraction_non_alphanumeric': 0.1121450344938232, 'fraction_numerical': 0.0083426921225734, 'mean_word_length': 2.2134020618556702, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '6565229', 'n_tokens_mistral': 1821, 'n_tokens_neox': 1755, 'n_words': 557}