<commit_before>"""
Created on 2012-03-15

@author: <NAME>
"""

import unittest
from datetime import datetime
import time
from system import repeat_timer

class TestRepeatTimer(unittest.TestCase):
    INTERVAL = 3

    def method_yes(self, start_datetime, seconds):
        print 'YES executed with parameters %s %s' % (str(start_datetime), seconds)

    def method_no(self, start_datetime, seconds):
        raise AssertionError('Assertion failed as NO was executed with parameters %s %s' % (str(start_datetime), seconds))

    def method_checkpoint(self, start_datetime, seconds):
        print 'Entering method checkpoint with parameters %s %s' % (str(start_datetime), seconds)
        delta = datetime.utcnow() - start_datetime
        if delta.seconds != seconds:
            raise AssertionError('Assertion failed by %s % s' % (str(delta), seconds))

    def test_normal_workflow(self):
        self.obj = repeat_timer.RepeatTimer(TestRepeatTimer.INTERVAL,
                                            self.method_checkpoint,
                                            args=[datetime.utcnow(), TestRepeatTimer.INTERVAL])
        self.obj.start()
        time.sleep(TestRepeatTimer.INTERVAL)
        self.obj.cancel()
        assert True
        
    def test_cancellation(self):
        self.obj = repeat_timer.RepeatTimer(TestRepeatTimer.INTERVAL,
                                            self.method_no,
                                            args=[datetime.utcnow(), 0])
        self.obj.start()
        self.obj.cancel()
        time.sleep(TestRepeatTimer.INTERVAL)
        assert True

    def test_trigger(self):
        self.obj = repeat_timer.RepeatTimer(TestRepeatTimer.INTERVAL,
                                            self.method_yes,
                                            args=[datetime.utcnow(), 0])
        self.obj.start()
        self.obj.trigger()
        self.obj.cancel()
        time.sleep(TestRepeatTimer.INTERVAL)
        assert True

    def test_trigger_with_continuation(self):
        self.obj = repeat_timer.RepeatTimer(TestRepeatTimer.INTERVAL - 1,
            self.method_yes,
            args=[datetime.utcnow(), 0])
        self.obj.start()
        self.obj.trigger()
        time.sleep(TestRepeatTimer.INTERVAL)
        self.obj.cancel()
        assert True

if __name__ == '__main__':
    unittest.main()<commit_msg>[022] Rewrote timer test to use meaningful assertions<commit_after>"""
@author: <NAME>
"""

import unittest
from datetime import datetime
import time
from system import repeat_timer


class TestRepeatTimer(unittest.TestCase):
    INTERVAL = 3

    def make_method_yes(self, initial_multiplication=1):
        # the only way to implement nonlocal closure variables in Python 2.X
        cycle = {'index': initial_multiplication}

        def method_yes(start_datetime, seconds):
            delta = datetime.utcnow() - start_datetime
            assert delta.seconds == cycle['index'] * seconds
            cycle['index'] += 1
        return method_yes

    def method_no(self, start_datetime, seconds):
        raise AssertionError('Assertion failed as NO was executed with parameters %s %s' % (str(start_datetime), seconds))

    def method_checkpoint(self, start_datetime, seconds):
        print 'Entering method checkpoint with parameters %s %s' % (str(start_datetime), seconds)
        delta = datetime.utcnow() - start_datetime
        if delta.seconds != seconds:
            raise AssertionError('Assertion failed by %s % s' % (str(delta), seconds))

    def test_normal_workflow(self):
        self.obj = repeat_timer.RepeatTimer(TestRepeatTimer.INTERVAL,
                                            self.method_checkpoint,
                                            args=[datetime.utcnow(), TestRepeatTimer.INTERVAL])
        self.obj.start()
        time.sleep(TestRepeatTimer.INTERVAL)
        self.obj.cancel()

    def test_cancellation(self):
        self.obj = repeat_timer.RepeatTimer(TestRepeatTimer.INTERVAL,
                                            self.method_no,
                                            args=[datetime.utcnow(), 0])
        self.obj.start()
        self.obj.cancel()
        time.sleep(TestRepeatTimer.INTERVAL)
        assert True

    def test_trigger(self):
        self.obj = repeat_timer.RepeatTimer(TestRepeatTimer.INTERVAL,
                                            self.make_method_yes(),
                                            args=[datetime.utcnow(), 0])
        self.obj.start()
        self.obj.trigger()
        self.obj.cancel()
        time.sleep(TestRepeatTimer.INTERVAL)

    def test_trigger_with_continuation(self):
        self.obj = repeat_timer.RepeatTimer(TestRepeatTimer.INTERVAL - 1,
                                            self.make_method_yes(initial_multiplication=0),
                                            args=[datetime.utcnow(), TestRepeatTimer.INTERVAL - 1])
        self.obj.start()
        self.obj.trigger()
        time.sleep(TestRepeatTimer.INTERVAL)
        self.obj.cancel()

if __name__ == '__main__':
    unittest.main()
{'fraction_non_alphanumeric': 0.09576138147566719, 'fraction_numerical': 0.004905808477237049, 'mean_word_length': 2.037544696066746, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 28, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3190683', 'n_tokens_mistral': 1366, 'n_tokens_neox': 1322, 'n_words': 286}