File: test_singleton.py

package info (click to toggle)
graphite-web 1.2.1~pre2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,976 kB
  • sloc: javascript: 86,824; python: 25,420; makefile: 124; sh: 91; ruby: 74; perl: 24
file content (36 lines) | stat: -rw-r--r-- 897 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# From https://github.com/reyoung/singleton


import unittest
from graphite.singleton import Singleton, ThreadSafeSingleton


class TestSingleton(unittest.TestCase):

    def _test_singleton(self, cls):
        @cls
        class IntSingleton(object):
            def __init__(self, default=0):
                self.i = default

        IntSingleton.initialize(10)
        a = IntSingleton.instance()
        b = IntSingleton.instance()

        self.assertEqual(a, b)
        self.assertEqual(id(a), id(b))
        self.assertTrue(IntSingleton.is_initialized())
        self.assertEqual(a.i, 10)
        self.assertEqual(b.i, 10)
        a.i = 100
        self.assertEqual(b.i, 100)

    def test_singleton(self):
        self._test_singleton(Singleton)

    def test_thread_safe_singleton(self):
        self._test_singleton(ThreadSafeSingleton)


if __name__ == '__main__':
    unittest.main()