]>
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
8 from ..tools
import NotificationQueue
11 class Listener(object):
12 # pylint: disable=too-many-instance-attributes
24 NotificationQueue
.register(self
.log_type1
, 'type1', priority
=90)
25 NotificationQueue
.register(self
.log_type2
, 'type2')
26 NotificationQueue
.register(self
.log_type1_3
, ['type1', 'type3'])
27 NotificationQueue
.register(self
.log_all
, priority
=50)
29 # these should be ignored by the queue
30 NotificationQueue
.register(self
.log_type1
, 'type1')
31 NotificationQueue
.register(self
.log_type1_3
, ['type1', 'type3'])
32 NotificationQueue
.register(self
.log_all
)
34 def log_type1(self
, val
):
35 self
.type1_ts
.append(time
.time())
36 self
.type1
.append(val
)
38 def log_type2(self
, val
):
39 self
.type2_ts
.append(time
.time())
40 self
.type2
.append(val
)
42 def log_type1_3(self
, val
):
43 self
.type1_3_ts
.append(time
.time())
44 self
.type1_3
.append(val
)
46 def log_all(self
, val
):
47 self
.all_ts
.append(time
.time())
59 NotificationQueue
.deregister(self
.log_type1
, 'type1')
60 NotificationQueue
.deregister(self
.log_type2
, 'type2')
61 NotificationQueue
.deregister(self
.log_type1_3
, ['type1', 'type3'])
62 NotificationQueue
.deregister(self
.log_all
)
65 class NotificationQueueTest(unittest
.TestCase
):
68 cls
.listener
= Listener()
71 self
.listener
.register()
76 def test_invalid_register(self
):
77 with self
.assertRaises(Exception) as ctx
:
78 NotificationQueue
.register(None, 1)
79 self
.assertEqual(str(ctx
.exception
),
80 "n_types param is neither a string nor a list")
82 def test_notifications(self
):
83 NotificationQueue
.start_queue()
84 NotificationQueue
.new_notification('type1', 1)
85 NotificationQueue
.new_notification('type2', 2)
86 NotificationQueue
.new_notification('type3', 3)
87 NotificationQueue
.stop()
88 self
.assertEqual(self
.listener
.type1
, [1])
89 self
.assertEqual(self
.listener
.type2
, [2])
90 self
.assertEqual(self
.listener
.type1_3
, [1, 3])
91 self
.assertEqual(self
.listener
.all
, [1, 2, 3])
94 self
.assertLessEqual(self
.listener
.type1_3_ts
[0], self
.listener
.all_ts
[0])
95 self
.assertLessEqual(self
.listener
.all_ts
[0], self
.listener
.type1_ts
[0])
96 self
.assertLessEqual(self
.listener
.type2_ts
[0], self
.listener
.all_ts
[1])
97 self
.assertLessEqual(self
.listener
.type1_3_ts
[1], self
.listener
.all_ts
[2])
99 def test_notifications2(self
):
100 NotificationQueue
.start_queue()
101 for i
in range(0, 600):
102 typ
= "type{}".format(i
% 3 + 1)
103 if random
.random() < 0.5:
105 NotificationQueue
.new_notification(typ
, i
)
106 NotificationQueue
.stop()
107 for i
in range(0, 600):
110 self
.assertIn(i
, self
.listener
.type1
)
111 self
.assertIn(i
, self
.listener
.type1_3
)
113 self
.assertIn(i
, self
.listener
.type2
)
115 self
.assertIn(i
, self
.listener
.type1_3
)
116 self
.assertIn(i
, self
.listener
.all
)
118 self
.assertEqual(len(self
.listener
.type1
), 200)
119 self
.assertEqual(len(self
.listener
.type2
), 200)
120 self
.assertEqual(len(self
.listener
.type1_3
), 400)
121 self
.assertEqual(len(self
.listener
.all
), 600)
123 def test_deregister(self
):
124 NotificationQueue
.start_queue()
125 NotificationQueue
.new_notification('type1', 1)
126 NotificationQueue
.new_notification('type3', 3)
127 NotificationQueue
.stop()
128 self
.assertEqual(self
.listener
.type1
, [1])
129 self
.assertEqual(self
.listener
.type1_3
, [1, 3])
131 NotificationQueue
.start_queue()
132 NotificationQueue
.deregister(self
.listener
.log_type1_3
, ['type1'])
133 NotificationQueue
.new_notification('type1', 4)
134 NotificationQueue
.new_notification('type3', 5)
135 NotificationQueue
.stop()
136 self
.assertEqual(self
.listener
.type1
, [1, 4])
137 self
.assertEqual(self
.listener
.type1_3
, [1, 3, 5])