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); }
public void MachineInfoIsProcessedInBackground() { var provider = new ThreadRejectingMachineInfoProvider(Thread.CurrentThread.ManagedThreadId); HardwareUtils.Init(provider); // Should not throw HardwareUtils.GetMachineID(provider); }
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); }
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); }
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); }
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()); }