python:samba:tests: Fix code spelling
[samba.git] / python / samba / tests / messaging.py
1 # -*- coding: utf-8 -*-
2 #
3 # Unix SMB/CIFS implementation.
4 # Copyright © Jelmer Vernooij <jelmer@samba.org> 2008
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 #
19
20 """Tests for samba.messaging."""
21 import samba
22 from samba.messaging import Messaging
23 from samba.tests import TestCase
24 import time
25 from samba.ndr import ndr_print
26 from samba.dcerpc import server_id
27 import random
28 import os
29
30
31 class MessagingTests(TestCase):
32
33     def get_context(self, *args, **kwargs):
34         kwargs['lp_ctx'] = samba.tests.env_loadparm()
35         return Messaging(*args, **kwargs)
36
37     def test_register(self):
38         x = self.get_context()
39
40         def callback():
41             pass
42         msg_type = x.register((callback, None))
43         self.assertTrue(isinstance(msg_type, int))
44         x.deregister(callback, msg_type)
45
46     def test_all_servers(self):
47         x = self.get_context()
48         self.assertTrue(isinstance(x.irpc_all_servers(), list))
49
50     def test_by_name(self):
51         x = self.get_context()
52         for name in x.irpc_all_servers():
53             self.assertTrue(isinstance(x.irpc_servers_byname(name.name), list))
54
55     def test_unknown_name(self):
56         x = self.get_context()
57         self.assertRaises(KeyError,
58                           x.irpc_servers_byname, "samba.messaging test NONEXISTING")
59
60     def test_assign_server_id(self):
61         x = self.get_context()
62         self.assertTrue(isinstance(x.server_id, server_id.server_id))
63
64     def test_add_remove_name(self):
65         x = self.get_context()
66         name = "samba.messaging test-%d" % random.randint(1, 1000000)
67         x.irpc_add_name(name)
68         name_list = x.irpc_servers_byname(name)
69         self.assertEqual(len(name_list), 1)
70         self.assertEqual(ndr_print(x.server_id),
71                          ndr_print(name_list[0]))
72         x.irpc_remove_name(name)
73         self.assertRaises(KeyError,
74                           x.irpc_servers_byname, name)
75
76     def test_ping_speed(self):
77         got_ping = {"count": 0}
78         got_pong = {"count": 0}
79         timeout = False
80
81         msg_pong = 0
82         msg_ping = 0
83
84         server_ctx = self.get_context((0, 1))
85
86         def ping_callback(got_ping, msg_type, src, data):
87             got_ping["count"] += 1
88             server_ctx.send(src, msg_pong, data)
89
90         msg_ping = server_ctx.register((ping_callback, got_ping))
91
92         def pong_callback(got_pong, msg_type, src, data):
93             got_pong["count"] += 1
94
95         client_ctx = self.get_context((0, 2))
96         msg_pong = client_ctx.register((pong_callback, got_pong))
97
98         # Try both server_id forms (structure and tuple)
99         client_ctx.send((0, 1), msg_ping, "testing")
100
101         client_ctx.send((0, 1), msg_ping, "testing2")
102
103         start_time = time.time()
104
105         # NOTE WELL: If debugging this with GDB, then the timeout will
106         # fire while you are trying to understand it.
107
108         while (got_ping["count"] < 2 or got_pong["count"] < 2) and not timeout:
109             client_ctx.loop_once(0.1)
110             server_ctx.loop_once(0.1)
111             if time.time() - start_time > 1:
112                 timeout = True
113
114         self.assertEqual(got_ping["count"], 2)
115         self.assertEqual(got_pong["count"], 2)
116
117     def test_pid_defaulting(self):
118         got_ping = {"count": 0}
119         got_pong = {"count": 0}
120         timeout = False
121
122         msg_pong = 0
123         msg_ping = 0
124
125         pid = os.getpid()
126         server_ctx = self.get_context((pid, 1))
127
128         def ping_callback(got_ping, msg_type, src, data):
129             got_ping["count"] += 1
130             server_ctx.send(src, msg_pong, data)
131
132         msg_ping = server_ctx.register((ping_callback, got_ping))
133
134         def pong_callback(got_pong, msg_type, src, data):
135             got_pong["count"] += 1
136
137         client_ctx = self.get_context((2,))
138         msg_pong = client_ctx.register((pong_callback, got_pong))
139
140         # Try one and two element tuple forms
141         client_ctx.send((pid, 1), msg_ping, "testing")
142
143         client_ctx.send((1,), msg_ping, "testing2")
144
145         start_time = time.time()
146
147         # NOTE WELL: If debugging this with GDB, then the timeout will
148         # fire while you are trying to understand it.
149
150         while (got_ping["count"] < 2 or got_pong["count"] < 2) and not timeout:
151             client_ctx.loop_once(0.1)
152             server_ctx.loop_once(0.1)
153             if time.time() - start_time > 1:
154                 timeout = True
155
156         self.assertEqual(got_ping["count"], 2)
157         self.assertEqual(got_pong["count"], 2)