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