]>
git.proxmox.com Git - ceph.git/blob - ceph/src/pybind/mgr/dashboard/tests/test_notification.py
1 # -*- coding: utf-8 -*-
2 from __future__
import absolute_import
9 from ..tools
import NotificationQueue
12 class Listener(object):
13 # pylint: disable=too-many-instance-attributes
25 NotificationQueue
.register(self
.log_type1
, 'type1', priority
=90)
26 NotificationQueue
.register(self
.log_type2
, 'type2')
27 NotificationQueue
.register(self
.log_type1_3
, ['type1', 'type3'])
28 NotificationQueue
.register(self
.log_all
, priority
=50)
30 # these should be ignored by the queue
31 NotificationQueue
.register(self
.log_type1
, 'type1')
32 NotificationQueue
.register(self
.log_type1_3
, ['type1', 'type3'])
33 NotificationQueue
.register(self
.log_all
)
35 def log_type1(self
, val
):
36 self
.type1_ts
.append(time
.time())
37 self
.type1
.append(val
)
39 def log_type2(self
, val
):
40 self
.type2_ts
.append(time
.time())
41 self
.type2
.append(val
)
43 def log_type1_3(self
, val
):
44 self
.type1_3_ts
.append(time
.time())
45 self
.type1_3
.append(val
)
47 def log_all(self
, val
):
48 self
.all_ts
.append(time
.time())
60 NotificationQueue
.deregister(self
.log_type1
, 'type1')
61 NotificationQueue
.deregister(self
.log_type2
, 'type2')
62 NotificationQueue
.deregister(self
.log_type1_3
, ['type1', 'type3'])
63 NotificationQueue
.deregister(self
.log_all
)
66 class NotificationQueueTest(unittest
.TestCase
):
69 cls
.listener
= Listener()
72 self
.listener
.register()
77 def test_invalid_register(self
):
78 with self
.assertRaises(Exception) as ctx
:
79 NotificationQueue
.register(None, 1)
80 self
.assertEqual(str(ctx
.exception
),
81 "n_types param is neither a string nor a list")
83 def test_notifications(self
):
84 NotificationQueue
.start_queue()
85 NotificationQueue
.new_notification('type1', 1)
86 NotificationQueue
.new_notification('type2', 2)
87 NotificationQueue
.new_notification('type3', 3)
88 NotificationQueue
.stop()
89 self
.assertEqual(self
.listener
.type1
, [1])
90 self
.assertEqual(self
.listener
.type2
, [2])
91 self
.assertEqual(self
.listener
.type1_3
, [1, 3])
92 self
.assertEqual(self
.listener
.all
, [1, 2, 3])
95 self
.assertLessEqual(self
.listener
.type1_3_ts
[0], self
.listener
.all_ts
[0])
96 self
.assertLessEqual(self
.listener
.all_ts
[0], self
.listener
.type1_ts
[0])
97 self
.assertLessEqual(self
.listener
.type2_ts
[0], self
.listener
.all_ts
[1])
98 self
.assertLessEqual(self
.listener
.type1_3_ts
[1], self
.listener
.all_ts
[2])
100 def test_notifications2(self
):
101 NotificationQueue
.start_queue()
102 for i
in range(0, 600):
103 typ
= "type{}".format(i
% 3 + 1)
104 if random
.random() < 0.5:
106 NotificationQueue
.new_notification(typ
, i
)
107 NotificationQueue
.stop()
108 for i
in range(0, 600):
111 self
.assertIn(i
, self
.listener
.type1
)
112 self
.assertIn(i
, self
.listener
.type1_3
)
114 self
.assertIn(i
, self
.listener
.type2
)
116 self
.assertIn(i
, self
.listener
.type1_3
)
117 self
.assertIn(i
, self
.listener
.all
)
119 self
.assertEqual(len(self
.listener
.type1
), 200)
120 self
.assertEqual(len(self
.listener
.type2
), 200)
121 self
.assertEqual(len(self
.listener
.type1_3
), 400)
122 self
.assertEqual(len(self
.listener
.all
), 600)
124 def test_deregister(self
):
125 NotificationQueue
.start_queue()
126 NotificationQueue
.new_notification('type1', 1)
127 NotificationQueue
.new_notification('type3', 3)
128 NotificationQueue
.stop()
129 self
.assertEqual(self
.listener
.type1
, [1])
130 self
.assertEqual(self
.listener
.type1_3
, [1, 3])
132 NotificationQueue
.start_queue()
133 NotificationQueue
.deregister(self
.listener
.log_type1_3
, ['type1'])
134 NotificationQueue
.new_notification('type1', 4)
135 NotificationQueue
.new_notification('type3', 5)
136 NotificationQueue
.stop()
137 self
.assertEqual(self
.listener
.type1
, [1, 4])
138 self
.assertEqual(self
.listener
.type1_3
, [1, 3, 5])