Exemple #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);
        }
Exemple #2
0
        public void MachineInfoIsProcessedInBackground()
        {
            var provider = new ThreadRejectingMachineInfoProvider(Thread.CurrentThread.ManagedThreadId);

            HardwareUtils.Init(provider);

            // Should not throw
            HardwareUtils.GetMachineID(provider);
        }
Exemple #3
0
        public void ExceptionBubblesUp()
        {
            var provider = new ThrowingMachineInfoProvider();

            HardwareUtils.Init(provider);

            var exception = Assert.Throws <InvalidOperationException>(() => HardwareUtils.GetMachineID(provider));

            Assert.Equal("This provider only throws.", exception.Message);
        }
Exemple #4
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);
        }
Exemple #5
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);
        }
Exemple #6
0
        public void GeneratesMessageObject()
        {
            var provider = new StaticMachineInfoProvider();

            HardwareUtils.Init(provider);
            var messageObjectData = HardwareUtils.GetMachineID(provider);

            var kv = new KeyValue();

            using (var ms = new MemoryStream(messageObjectData))
            {
                Assert.True(kv.TryReadAsBinary(ms));
            }

            Assert.Equal("MessageObject", kv.Name);
            Assert.Equal("3018ba91fc5a72f8b3f74501af6dd6da331b6cbc", kv["BB3"].AsString());
            Assert.Equal("5d7e7734714b64bd6d88fef3ddbbf7ab4a749c5e", kv["FF2"].AsString());
            Assert.Equal("a1807176456746ba2a3f5574bf47677a919dab49", kv["3B3"].AsString());
        }