pykolab/format

Christian Mollekopf mollekopf at kolabsys.com
Fri Feb 24 09:55:29 CET 2012


 pykolab/format/tests/test-thread_safety.py |   68 +++++++++--------------------
 1 file changed, 22 insertions(+), 46 deletions(-)

New commits:
commit cb63be089fce18dd9899526455022dad10f8e2ac
Author: Christian Mollekopf <mollekopf at kolabsys.com>
Date:   Fri Feb 24 09:54:15 2012 +0100

    Actually call the function from different threads instead of the main thread.

diff --git a/pykolab/format/tests/test-thread_safety.py b/pykolab/format/tests/test-thread_safety.py
index 7bcb210..8878356 100644
--- a/pykolab/format/tests/test-thread_safety.py
+++ b/pykolab/format/tests/test-thread_safety.py
@@ -9,10 +9,12 @@ from kolabformat import getSerializedUID
 from kolabformat import writeEvent
 
 import threading
+import time
 
 class ThreadEvent(threading.Thread):
     def __init__(self, *args, **kw):
         threading.Thread.__init__(self, target=self.create_event)
+        self.errorBit = False
 
     def create_event(self):
         self.myevent = Event()
@@ -27,12 +29,24 @@ class ThreadEvent(threading.Thread):
 
         self.myevent.setDescription("Celebrate new years eve with some champagne, family and friends")
 
+
     def write(self):
         self.create_event()
         writeEvent(self.myevent)
 
     def uid(self):
         return getSerializedUID()
+        
+    def run(self):
+        for i in range(10):
+            self.write()
+            self.uid1 = self.uid()
+            time.sleep(1)
+            #We expect the other thread to be called once while we're sleeping
+            #To be absolutely sure we'd have to use locking
+            self.uid2 = self.uid()
+            if self.uid1 != self.uid2:
+                self.errorBit = True
 
 class TestThreadSafety(unittest.TestCase):
     def create_threads(self):
@@ -41,51 +55,13 @@ class TestThreadSafety(unittest.TestCase):
 
         self.thread2 = ThreadEvent()
         self.thread2.start()
-
-        # Serialize the two events in order.
-        self.thread1.write()
-        self.uid1 = self.thread1.uid()
-
-        self.thread2.write()
-        self.uid2 = self.thread2.uid()
-
-        # Both threads should now have events with a different uid, contained
-        # within the (local) uid1 and uid2 variables.
-
-        # Now that we've serialized the event in thread #2, let's see if we can
-        # get back to the uid used for the event in thread #1.
-        self.uid3 = self.thread1.uid()
-
+       
+        time.sleep(10.0)
+        
+        self.thread2.join();
+        self.thread1.join();
+        
     def test_thread_safety_uid1_ne_uid2(self):
         self.create_threads()
-
-        # The uid for the event in thread #1 cannot be the same as the uid for
-        # the event in thread #2.
-        self.assertNotEqual(
-                self.uid1,
-                self.uid2,
-                "The <uid> for the event in thread #1 is the same as the " + \
-                    "<uid> for the event in thread #2."
-            )
-
-    def test_thread_safety_uid3_ne_uid2(self):
-        self.create_threads()
-
-        self.assertNotEqual(
-                self.uid3,
-                self.uid2,
-                "After serializing the event in thread #2, the uid method " + \
-                    "on thread #1 returns the same uid for the event in thread #1."
-            )
-
-    def test_thread_safety_uid1_eq_uid3(self):
-        self.create_threads()
-
-        self.assertEqual(
-                self.uid1,
-                self.uid3,
-                "After serializing the event in thread #2, the uid method " + \
-                    "on thread #1 returns a different uid for the event " + \
-                    "in thread #1, than was the original for the event in " + \
-                    "thread #1."
-            )
+        self.assertTrue(not self.thread1.errorBit)
+        self.assertTrue(not self.thread2.errorBit)





More information about the commits mailing list