//[DataRow(true, true, MockUsbDevice.VendorId, MockUsbDevice.ProductId)] //[DataRow(false, true, MockUsbDevice.VendorId, MockUsbDevice.ProductId)] public async Task TestWriteAndReadThreadSafety(bool isHidConnected, bool isUsbConnected, uint vid, uint pid) { MockHidFactory.IsConnectedStatic = isHidConnected; MockUsbFactory.IsConnectedStatic = isUsbConnected; var connectedDeviceDefinition = (await DeviceManager.Current.GetConnectedDeviceDefinitionsAsync(new FilterDeviceDefinition { ProductId = pid, VendorId = vid })).ToList().First(); var mockHidDevice = new MockHidDevice() { DeviceId = connectedDeviceDefinition.DeviceId }; var writeAndReadTasks = new List <Task <byte[]> >(); //TODO: Does this properly test thread safety? for (byte i = 0; i < 10; i++) { writeAndReadTasks.Add(mockHidDevice.WriteAndReadAsync(new byte[64] { i, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 })); } var results = await Task.WhenAll(writeAndReadTasks); for (byte i = 0; i < results.Length; i++) { var result = results[i]; Assert.IsTrue(result[0] == i); } }
public async Task TestWriteAndReadThreadSafety(bool isHidConnected, bool isUsbConnected, uint vid, uint pid) { //TODO: Does this properly test thread safety? var actualCount = 0; #if !NET45 _ = _loggerMock.Setup(l => l.Log( LogLevel.Debug, It.IsAny <EventId>(), It.IsAny <It.IsAnyType>(), It.IsAny <Exception>(), (Func <It.IsAnyType, Exception, string>)It.IsAny <object>())).Callback(() => actualCount++); _ = _loggerMock.Setup(l => l.BeginScope(It.IsAny <It.IsAnyType>())).Returns(new Mock <IDisposable>().Object); #endif var(hid, usb) = GetMockedFactories(isHidConnected, isUsbConnected, vid, pid); var deviceManager = new List <IDeviceFactory> { hid.Object, usb.Object }.Aggregate(); var connectedDeviceDefinition = (await deviceManager.GetConnectedDeviceDefinitionsAsync()).ToList().First(); var mockHidDevice = new MockHidDevice(connectedDeviceDefinition.DeviceId, _loggerFactory, _loggerMock.Object); var writeAndReadTasks = new List <Task <TransferResult> >(); const int count = 10; for (byte i = 0; i < count; i++) { writeAndReadTasks.Add(mockHidDevice.WriteAndReadAsync(new byte[64] { i, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 })); } var results = await Task.WhenAll(writeAndReadTasks); for (byte i = 0; i < results.Length; i++) { var result = results[i]; Assert.IsTrue(result.Data[0] == i); } Assert.AreEqual(count * 2, actualCount); //TODO: this should get called 10 times and that seems to be what's happening, bu tif you specify 10 it says that it was called 20. //Bug in Moq? #if !NET45 CheckLogMessageText(_loggerMock, Messages.SuccessMessageWriteAndReadCalled, LogLevel.Information, Times.AtLeast(1)); #endif }
public async Task TestWriteAndReadThreadSafety(bool isHidConnected, bool isUsbConnected, uint vid, uint pid) { var readtraceCount = tracer.ReadCount; var writetraceCount = tracer.WriteCount; MockHidFactory.IsConnectedStatic = isHidConnected; MockUsbFactory.IsConnectedStatic = isUsbConnected; var connectedDeviceDefinition = (await DeviceManager.Current.GetConnectedDeviceDefinitionsAsync(new FilterDeviceDefinition { ProductId = pid, VendorId = vid })).ToList().First(); var mockHidDevice = new MockHidDevice(logger, tracer) { DeviceId = connectedDeviceDefinition.DeviceId }; var writeAndReadTasks = new List <Task <ReadResult> >(); //TODO: Does this properly test thread safety? const int count = 10; for (byte i = 0; i < count; i++) { writeAndReadTasks.Add(mockHidDevice.WriteAndReadAsync(new byte[64] { i, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 })); } var results = await Task.WhenAll(writeAndReadTasks); for (byte i = 0; i < results.Length; i++) { var result = results[i]; Assert.IsTrue(result.Data[0] == i); } Assert.AreEqual(readtraceCount + count, tracer.ReadCount); Assert.AreEqual(writetraceCount + count, tracer.WriteCount); Assert.IsTrue(logger.LogText.Contains(Messages.SuccessMessageWriteAndReadCalled)); }