Browse Source

Add factory helper function which returns a singleton

Not that ServiceTypes is super expensive or anything, but it's also
an encapsultation of static data, so there's really no need to construct
more than one of them in a program.

Change-Id: I2515fd9be27421006ed22a3ab01bef8cb48196f8
Monty Taylor 1 year ago
parent
commit
f9dca6f788
No account linked to committer's email address

+ 21
- 0
os_service_types/__init__.py View File

@@ -18,3 +18,24 @@ import pbr.version
18 18
 from os_service_types.service_types import ServiceTypes  # flake8: noqa
19 19
 
20 20
 __version__ = pbr.version.VersionInfo('os-service-types').version_string()
21
+_service_type_manager = None
22
+
23
+
24
+def get_service_types(*args, **kwargs):
25
+    """Return singleton instance of the ServiceTypes object.
26
+
27
+    Parameters are all passed through to the
28
+    :class:`~os_service_types.service_types.ServiceTypes` constructor.
29
+
30
+    .. note::
31
+
32
+      Only one singleton is kept, so if instances with different parameter
33
+      values are desired, directly calling the constructor is necessary.
34
+
35
+    :returns: Singleton instance of
36
+        :class:`~os_service_types.service_types.ServiceTypes`
37
+    """
38
+    global _service_type_manager
39
+    if not _service_type_manager:
40
+        _service_type_manager = ServiceTypes(*args, **kwargs)
41
+    return _service_type_manager

+ 38
- 0
os_service_types/tests/test_singleton.py View File

@@ -0,0 +1,38 @@
1
+# -*- coding: utf-8 -*-
2
+
3
+# Licensed under the Apache License, Version 2.0 (the "License"); you may
4
+# not use this file except in compliance with the License. You may obtain
5
+# a copy of the License at
6
+#
7
+#      http://www.apache.org/licenses/LICENSE-2.0
8
+#
9
+# Unless required by applicable law or agreed to in writing, software
10
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12
+# License for the specific language governing permissions and limitations
13
+# under the License.
14
+
15
+"""
16
+test_singleton
17
+------------
18
+
19
+Tests for `get_service_types` singleton factory function.
20
+"""
21
+import os_service_types
22
+from os_service_types.tests import base
23
+
24
+
25
+class TestSingleton(base.TestCase):
26
+
27
+    def setUp(self):
28
+        super(TestSingleton, self).setUp()
29
+        # Make an object with no network access
30
+        self.service_types = os_service_types.get_service_types()
31
+
32
+    def test_singleton_same(self):
33
+        service_types = os_service_types.get_service_types()
34
+        self.assertTrue(service_types is self.service_types)
35
+
36
+    def test_singleton_different(self):
37
+        service_types = os_service_types.ServiceTypes()
38
+        self.assertFalse(service_types is self.service_types)

+ 7
- 0
releasenotes/notes/get-service-types-f5e56799a55a6df4.yaml View File

@@ -0,0 +1,7 @@
1
+---
2
+features:
3
+  - |
4
+    Factory function ``os_service_types.get_service_types`` added. Returns
5
+    a singleton instance of ServiceTypes. ServiceTypes is a very low cost
6
+    object in the first place, but it does read a data file from disk and
7
+    then the data is pretty static.

Loading…
Cancel
Save