<commit_before>import time
import unittest

import pants

class TestTimers(unittest.TestCase):
    def setUp(self):
        self.times_called = []

    def timer(self):
        self.times_called.append(time.time())

    def test_callback(self):
        pants.callback(self.timer)
        pants.engine.poll(0.01)
        pants.engine.poll(0.01)
        pants.engine.poll(0.01)
        self.assertEquals(len(self.times_called), 1)

    def test_loop(self):
        pants.loop(self.timer)
        pants.engine.poll(0.01)
        pants.engine.poll(0.01)
        pants.engine.poll(0.01)
        self.assertEquals(len(self.times_called), 3)

    def test_defer(self):
        expected_time = time.time() + 0.01
        pants.defer(0.01, self.timer)
        pants.engine.poll(0.2)
        pants.engine.poll(0.2)
        pants.engine.poll(0.2)
        self.assertEquals(len(self.times_called), 1)
        self.assertLess(abs(expected_time - self.times_called[0]), 0.01)

    def test_defer_with_zero_delay(self):
        self.assertRaises(ValueError, pants.defer, 0, self.timer)

    def test_defer_with_negative_delay(self):
        self.assertRaises(ValueError, pants.defer, -1.0, self.timer)

    def test_cycle(self):
        expected_times = [time.time() + 0.01, time.time() + 0.02, time.time() + 0.03]
        pants.cycle(0.01, self.timer)
        pants.engine.poll(0.2)
        pants.engine.poll(0.2)
        pants.engine.poll(0.2)
        self.assertEquals(len(self.times_called), 3)
        self.assertLess(abs(expected_times[0] - self.times_called[0]), 0.01)
        self.assertLess(abs(expected_times[1] - self.times_called[1]), 0.01)
        self.assertLess(abs(expected_times[2] - self.times_called[2]), 0.01)

    def test_cycle_with_zero_delay(self):
        self.assertRaises(ValueError, pants.cycle, 0, self.timer)

    def test_cycle_with_negative_delay(self):
        self.assertRaises(ValueError, pants.cycle, -1.0, self.timer)<commit_msg>Added unit tests for timer cancellation.<commit_after>import time
import unittest

import pants

class TestTimers(unittest.TestCase):
    def setUp(self):
        self.times_called = []

    def timer(self):
        self.times_called.append(time.time())

    def test_callback(self):
        pants.callback(self.timer)
        pants.engine.poll(0.01)
        pants.engine.poll(0.01)
        pants.engine.poll(0.01)
        self.assertEquals(len(self.times_called), 1)

    def test_callback_cancel(self):
        cancel_callback = pants.callback(self.timer)
        cancel_callback()
        pants.engine.poll(0.01)
        pants.engine.poll(0.01)
        pants.engine.poll(0.01)
        self.assertEquals(len(self.times_called), 0)

    def test_loop(self):
        pants.loop(self.timer)
        pants.engine.poll(0.01)
        pants.engine.poll(0.01)
        pants.engine.poll(0.01)
        self.assertEquals(len(self.times_called), 3)

    def test_loop_cancel(self):
        cancel_loop = pants.loop(self.timer)
        pants.engine.poll(0.01)
        pants.engine.poll(0.01)
        self.assertEquals(len(self.times_called), 2)
        cancel_loop()
        pants.engine.poll(0.01)
        self.assertEquals(len(self.times_called), 2)

    def test_defer(self):
        expected_time = time.time() + 0.01
        pants.defer(0.01, self.timer)
        pants.engine.poll(0.2)
        pants.engine.poll(0.2)
        pants.engine.poll(0.2)
        self.assertEquals(len(self.times_called), 1)
        self.assertLess(abs(expected_time - self.times_called[0]), 0.01)

    def test_defer_cancel(self):
        cancel_defer = pants.defer(0.01, self.timer)
        cancel_defer()
        pants.engine.poll(0.2)
        pants.engine.poll(0.2)
        pants.engine.poll(0.2)
        self.assertEquals(len(self.times_called), 0)

    def test_defer_with_zero_delay(self):
        self.assertRaises(ValueError, pants.defer, 0, self.timer)

    def test_defer_with_negative_delay(self):
        self.assertRaises(ValueError, pants.defer, -1.0, self.timer)

    def test_cycle(self):
        expected_times = [time.time() + 0.01, time.time() + 0.02, time.time() + 0.03]
        pants.cycle(0.01, self.timer)
        pants.engine.poll(0.2)
        pants.engine.poll(0.2)
        pants.engine.poll(0.2)
        self.assertEquals(len(self.times_called), 3)
        self.assertLess(abs(expected_times[0] - self.times_called[0]), 0.01)
        self.assertLess(abs(expected_times[1] - self.times_called[1]), 0.01)
        self.assertLess(abs(expected_times[2] - self.times_called[2]), 0.01)

    def test_cycle_cancel(self):
        expected_times = [time.time() + 0.01, time.time() + 0.02, time.time() + 0.03]
        cancel_cycle = pants.cycle(0.01, self.timer)
        pants.engine.poll(0.2)
        pants.engine.poll(0.2)
        self.assertEquals(len(self.times_called), 2)
        cancel_cycle()
        pants.engine.poll(0.2)
        self.assertEquals(len(self.times_called), 2)
        self.assertLess(abs(expected_times[0] - self.times_called[0]), 0.01)
        self.assertLess(abs(expected_times[1] - self.times_called[1]), 0.01)

    def test_cycle_with_zero_delay(self):
        self.assertRaises(ValueError, pants.cycle, 0, self.timer)

    def test_cycle_with_negative_delay(self):
        self.assertRaises(ValueError, pants.cycle, -1.0, self.timer)
{'fraction_non_alphanumeric': 0.13752122241086587, 'fraction_numerical': 0.04055838521033767, 'mean_word_length': 2.728551336146273, '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': '4479935', 'n_tokens_mistral': 1908, 'n_tokens_neox': 1844, 'n_words': 251}