< Summary

Class:Microsoft.Azure.ServiceBus.Primitives.ConcurrentRandom
Assembly:Microsoft.Azure.ServiceBus
File(s):C:\Git\azure-sdk-for-net\sdk\servicebus\Microsoft.Azure.ServiceBus\src\Primitives\ConcurrentRandom.cs
Covered lines:8
Uncovered lines:4
Coverable lines:12
Total lines:49
Line coverage:66.6% (8 of 12)
Covered branches:2
Total branches:2
Branch coverage:100% (2 of 2)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
.cctor()-100%100%
Next(...)-100%100%
NextPositiveLong()-0%100%
GetThreadLocalRandom()-100%100%

File(s)

C:\Git\azure-sdk-for-net\sdk\servicebus\Microsoft.Azure.ServiceBus\src\Primitives\ConcurrentRandom.cs

#LineLine coverage
 1// Copyright (c) Microsoft. All rights reserved.
 2// Licensed under the MIT license. See LICENSE file in the project root for full license information.
 3
 4namespace Microsoft.Azure.ServiceBus.Primitives
 5{
 6    using System;
 7
 8    internal static class ConcurrentRandom
 9    {
 10        // We lock on this when generating a seed for a threadLocalRandom
 11        [Fx.Tag.SynchronizationObject]
 212        static readonly Random SeedGenerator = new Random();
 13
 14        [ThreadStatic]
 15        static Random threadLocalRandom;
 16
 17        public static int Next(int minValue, int maxValue)
 18        {
 6819            return GetThreadLocalRandom().Next(minValue, maxValue);
 20        }
 21
 22        // A 64-bit signed integer, x, such that 0 ≤ x ≤Int64.MaxValue.
 23        // This is different from ulong because ulong is 64 bits.
 24        // This only makes use of 63 bits - because it always returns positives
 25        public static long NextPositiveLong()
 26        {
 027            var buffer = new byte[8];
 028            GetThreadLocalRandom().NextBytes(buffer);
 029            var ulongValue = (long)BitConverter.ToUInt64(buffer, 0);
 030            return Math.Abs(ulongValue);
 31        }
 32
 33        static Random GetThreadLocalRandom()
 34        {
 6835            if (threadLocalRandom == null)
 36            {
 37                int seed;
 1038                lock (SeedGenerator)
 39                {
 1040                    seed = SeedGenerator.Next();
 1041                }
 42
 1043                threadLocalRandom = new Random(seed);
 44            }
 45
 6846            return threadLocalRandom;
 47        }
 48    }
 49}