test_stopwatch.py revision d3907f0d
1# -*- coding: utf8 -*-
2# Copyright (C) PyZMQ Developers
3# Distributed under the terms of the Modified BSD License.
4
5
6import sys
7import time
8
9from unittest import TestCase
10
11from zmq import Stopwatch, ZMQError
12
13if sys.version_info[0] >= 3:
14    long = int
15
16class TestStopWatch(TestCase):
17
18    def test_stop_long(self):
19        """Ensure stop returns a long int."""
20        watch = Stopwatch()
21        watch.start()
22        us = watch.stop()
23        self.assertTrue(isinstance(us, long))
24
25    def test_stop_microseconds(self):
26        """Test that stop/sleep have right units."""
27        watch = Stopwatch()
28        watch.start()
29        tic = time.time()
30        watch.sleep(1)
31        us = watch.stop()
32        toc = time.time()
33        self.assertAlmostEqual(us/1e6,(toc-tic),places=0)
34
35    def test_double_stop(self):
36        """Test error raised on multiple calls to stop."""
37        watch = Stopwatch()
38        watch.start()
39        watch.stop()
40        self.assertRaises(ZMQError, watch.stop)
41        self.assertRaises(ZMQError, watch.stop)
42
43