qpid-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ai...@apache.org
Subject svn commit: r886945 - in /qpid/trunk/qpid/dotnet/client-010: client/transport/util/UUID.cs test/transport/util/UUIDTest.cs
Date Thu, 03 Dec 2009 22:12:06 GMT
Author: aidan
Date: Thu Dec  3 22:12:06 2009
New Revision: 886945

URL: http://svn.apache.org/viewvc?rev=886945&view=rev
Log:
QPID-1972: fix UUID generation.

Patch from julien.lavigne

Modified:
    qpid/trunk/qpid/dotnet/client-010/client/transport/util/UUID.cs
    qpid/trunk/qpid/dotnet/client-010/test/transport/util/UUIDTest.cs

Modified: qpid/trunk/qpid/dotnet/client-010/client/transport/util/UUID.cs
URL: http://svn.apache.org/viewvc/qpid/trunk/qpid/dotnet/client-010/client/transport/util/UUID.cs?rev=886945&r1=886944&r2=886945&view=diff
==============================================================================
--- qpid/trunk/qpid/dotnet/client-010/client/transport/util/UUID.cs (original)
+++ qpid/trunk/qpid/dotnet/client-010/client/transport/util/UUID.cs Thu Dec  3 22:12:06 2009
@@ -26,9 +26,9 @@
     public class UUID
     {
         private long _mostSigBits;
-
         private long _leastSigBits;
         private static readonly Random _random = new Random();
+        private static readonly object _randomLock = new object();
 
 
         public UUID(long mostSigBits, long leastSigBits)
@@ -49,7 +49,7 @@
             set { _leastSigBits = value; }
         }
 
-        private UUID(byte[] r)
+        internal UUID(byte[] r)
         {
             MostSignificantBits = 0;
             LeastSignificantBits = 0;
@@ -59,27 +59,33 @@
                 LeastSignificantBits = (LeastSignificantBits << 8) | (r[i] & 0xff);

         }
 
-        public static UUID randomUUID()
+        public static UUID RandomUuid()
         {
             byte[] randomBytes = new byte[16];
-            _random.NextBytes(randomBytes);
-            randomBytes[6] &= 0x0f;  
-            randomBytes[6] |= 0x40; 
-            randomBytes[8] &= 0x3f; 
-            randomBytes[8] |= 0x80;           
+            lock (_randomLock)
+            {
+                _random.NextBytes(randomBytes);
+            }
+
+            randomBytes[6] &= 0x0f;
+            randomBytes[6] |= 0x40;
+            randomBytes[8] &= 0x3f;
+            randomBytes[8] |= 0x80;
+
             return new UUID(randomBytes);
         }
+       
 
         public override String ToString()
         {
-            return (digits(_mostSigBits >> 32, 8) + "-" +
-                    digits(_mostSigBits >> 16, 4) + "-" +
-                    digits(_mostSigBits, 4) + "-" +
-                    digits(_leastSigBits >> 48, 4) + "-" +
-                    digits(_leastSigBits, 12));
+            return (Digits(_mostSigBits >> 32, 8) + "-" +
+                    Digits(_mostSigBits >> 16, 4) + "-" +
+                    Digits(_mostSigBits, 4) + "-" +
+                    Digits(_leastSigBits >> 48, 4) + "-" +
+                    Digits(_leastSigBits, 12));
         }
 
-        private static String digits(long val, int digits)
+        private static String Digits(long val, int digits)
         {
             long hi = 1L << (digits * 4);
             return Convert.ToString((hi | (val & (hi - 1))), 16);

Modified: qpid/trunk/qpid/dotnet/client-010/test/transport/util/UUIDTest.cs
URL: http://svn.apache.org/viewvc/qpid/trunk/qpid/dotnet/client-010/test/transport/util/UUIDTest.cs?rev=886945&r1=886944&r2=886945&view=diff
==============================================================================
--- qpid/trunk/qpid/dotnet/client-010/test/transport/util/UUIDTest.cs (original)
+++ qpid/trunk/qpid/dotnet/client-010/test/transport/util/UUIDTest.cs Thu Dec  3 22:12:06
2009
@@ -28,20 +28,22 @@
 
     public class UUIDTest
     {
+
+
         [Test]
         public void createUUID()
         {
-            UUID uuid = UUID.randomUUID();
+            UUID uuid = UUID.RandomUuid();
             String uuidStr = uuid.ToString();
             Assert.IsNotNull(uuid);
-            UUID uuid2 = UUID.randomUUID();
+            UUID uuid2 = UUID.RandomUuid();
             Assert.AreNotSame(uuid, uuid2);
         }
 
         [Test]
         public void ToString_should_override_and_not_hide_base()
         {
-            UUID uuid = UUID.randomUUID();
+            UUID uuid = UUID.RandomUuid();
 
             string uuidStr = uuid.ToString();
             string uuidConcat = "Test." + uuid;
@@ -52,7 +54,7 @@
         [Test]
         public void two_uuid_with_same_value_should_have_same_hash_code()
         {
-            UUID uuid = UUID.randomUUID();
+            UUID uuid = UUID.RandomUuid();
             UUID uuid2 = new UUID(uuid.MostSignificantBits, uuid.LeastSignificantBits);
             
             Assert.AreEqual(uuid, uuid2);



---------------------------------------------------------------------
Apache Qpid - AMQP Messaging Implementation
Project:      http://qpid.apache.org
Use/Interact: mailto:commits-subscribe@qpid.apache.org


Mime
View raw message