pymessaging: add single element tupple form of the server_id
authorGary Lockyer <gary@catalyst.net.nz>
Thu, 16 Mar 2017 03:26:01 +0000 (16:26 +1300)
committerAndrew Bartlett <abartlet@samba.org>
Tue, 28 Mar 2017 07:23:11 +0000 (09:23 +0200)
This avoids the python code needing to call getpid() internally,
while declaring a stable task_id.

Signed-off-by: Gary Lockyer <gary@catalyst.net.nz>
Reviewed-by: Andrew Bartlett <abartlet@samba.org>
Reviewed-by: Garming Sam <garming@catalyst.net.nz>
python/samba/tests/messaging.py
source4/lib/messaging/pymessaging.c

index 6ee18e791e361e021b93209d7cee27f14c00912d..41834c1a110cee26ec0bd02153cf17f5880b6316 100644 (file)
@@ -25,6 +25,7 @@ import time
 from samba.ndr import ndr_print
 from samba.dcerpc import server_id
 import random
+import os
 
 class MessagingTests(TestCase):
 
@@ -109,3 +110,44 @@ class MessagingTests(TestCase):
 
         self.assertEqual(got_ping["count"], 2)
         self.assertEqual(got_pong["count"], 2)
+
+    def test_pid_defaulting(self):
+        got_ping = {"count": 0}
+        got_pong = {"count": 0}
+        timeout = False
+
+        msg_pong = 0
+        msg_ping = 0
+
+        pid = os.getpid()
+        server_ctx = self.get_context((pid, 1))
+        def ping_callback(got_ping, msg_type, src, data):
+            got_ping["count"] += 1
+            server_ctx.send(src, msg_pong, data)
+
+        msg_ping = server_ctx.register((ping_callback, got_ping))
+
+        def pong_callback(got_pong, msg_type, src, data):
+            got_pong["count"] += 1
+
+        client_ctx = self.get_context((2,))
+        msg_pong = client_ctx.register((pong_callback, got_pong))
+
+        # Try one and two element tuple forms
+        client_ctx.send((pid, 1), msg_ping, "testing")
+
+        client_ctx.send((1,), msg_ping, "testing2")
+
+        start_time = time.time()
+
+        # NOTE WELL: If debugging this with GDB, then the timeout will
+        # fire while you are trying to understand it.
+
+        while (got_ping["count"] < 2 or got_pong["count"] < 2) and not timeout:
+            client_ctx.loop_once(0.1)
+            server_ctx.loop_once(0.1)
+            if time.time() - start_time > 1:
+                timeout = True
+
+        self.assertEqual(got_ping["count"], 2)
+        self.assertEqual(got_pong["count"], 2)
index 2d1601d5fd38051de11e2036043311b5ca9fdc87..d83f5e6a63790d99016833d77fc8c1d99adcea42 100644 (file)
@@ -62,13 +62,20 @@ static bool server_id_from_py(PyObject *object, struct server_id *server_id)
                server_id->task_id = task_id;
                server_id->vnn = vnn;
                return true;
-       } else {
+       } else if (PyTuple_Size(object) == 2) {
                unsigned long long pid;
                int task_id;
                if (!PyArg_ParseTuple(object, "KI", &pid, &task_id))
                        return false;
                *server_id = cluster_id(pid, task_id);
                return true;
+       } else {
+               unsigned long long pid = getpid();
+               int task_id;
+               if (!PyArg_ParseTuple(object, "I", &task_id))
+                       return false;
+               *server_id = cluster_id(pid, task_id);
+               return true;
        }
 }