Exemplo n.º 1
0
        public void GenerationIsThreadSafe()
        {
            var provider = new CountingMachineInfoProvider();
            var trigger  = new ManualResetEventSlim();

            var threads = new Thread[100];

            for (var i = 0; i < threads.Length; i++)
            {
                threads[i] = new Thread(state =>
                {
                    var provider = (IMachineInfoProvider)state;
                    trigger.Wait();
                    HardwareUtils.Init(provider);
                    HardwareUtils.GetMachineID(provider);
                });
                threads[i].Start(provider);
            }

            trigger.Set();

            for (var i = 0; i < threads.Length; i++)
            {
                threads[i].Join();
            }

            Assert.Equal(3, provider.TotalInvocations);
        }
Exemplo n.º 2
0
        public void ProviderIsNotRetained()
        {
            WeakReference Setup()
            {
                var provider = new CountingMachineInfoProvider();

                HardwareUtils.Init(provider);
                HardwareUtils.GetMachineID(provider);
                return(new WeakReference(provider));
            }

            var provider = Setup();

            GC.Collect();
            Assert.False(provider.IsAlive);
        }
Exemplo n.º 3
0
        public void ResultIsCached()
        {
            var provider = new CountingMachineInfoProvider();

            HardwareUtils.Init(provider);
            HardwareUtils.GetMachineID(provider);

            var invocations = provider.TotalInvocations;

            for (var i = 0; i < 100; i++)
            {
                HardwareUtils.Init(provider);
                HardwareUtils.GetMachineID(provider);
            }

            Assert.Equal(invocations, provider.TotalInvocations);
        }