Example #1
0
        public async Task GivenRunningService_WhenStop_ThenServiceStopped()
        {
            // Arrange
            var serialPortMonitorServiceConfiguration = new SerialPortMonitorServiceConfiguration
            {
                PollPauseMilliseconds = 100
            };
            var mockSerialPortDiscoveryService = new Mock <ISerialPortDiscoveryService>();
            var mockDateTimeService            = new Mock <IDateTimeService>();
            var sut = new SerialPortMonitorService(
                serialPortMonitorServiceConfiguration,
                mockSerialPortDiscoveryService.Object,
                mockDateTimeService.Object);

            mockSerialPortDiscoveryService.Setup(x => x.GetPorts())
            .Returns(Array.Empty <string>());

            mockDateTimeService.SetupGet(x => x.UtcNow).Returns(DateTime.UtcNow);
            sut.Start();

            // Act
            await sut.Stop();

            // Assert
            Assert.False(sut.IsRunning);
        }
Example #2
0
        public async Task Given2PortsConnected_WhenStart_ThenPortConnectedEventsRaised()
        {
            // Arrange
            var serialPortMonitorServiceConfiguration = new SerialPortMonitorServiceConfiguration
            {
                PollPauseMilliseconds = 100
            };
            var mockSerialPortDiscoveryService = new Mock <ISerialPortDiscoveryService>();
            var mockDateTimeService            = new Mock <IDateTimeService>();
            var sut = new SerialPortMonitorService(
                serialPortMonitorServiceConfiguration,
                mockSerialPortDiscoveryService.Object,
                mockDateTimeService.Object);

            mockSerialPortDiscoveryService.Setup(x => x.GetPorts())
            .Returns(new [] { "COM1", "COM2" });

            mockDateTimeService.SetupGet(x => x.UtcNow).Returns(DateTime.UtcNow);

            var currentlyConnectedPorts = new List <string>();

            sut.PortsConnected += (sender, e) =>
            {
                currentlyConnectedPorts.AddRange(e.SerialPorts);
            };

            // Act
            sut.Start();
            await Task.Delay(1000).ConfigureAwait(false);

            // Assert
            Assert.Equal(2, currentlyConnectedPorts.Count);
            Assert.Contains(currentlyConnectedPorts, x => x == "COM1");
            Assert.Contains(currentlyConnectedPorts, x => x == "COM2");
        }
Example #3
0
        public async Task GivenPortConnectionActivity_WhenRunning_ThenCorrectListOfConnectedPortsMaintained()
        {
            // Arrange
            var serialPortMonitorServiceConfiguration = new SerialPortMonitorServiceConfiguration
            {
                PollPauseMilliseconds = 100
            };
            var mockSerialPortDiscoveryService = new Mock <ISerialPortDiscoveryService>();
            var mockDateTimeService            = new Mock <IDateTimeService>();
            var sut = new SerialPortMonitorService(
                serialPortMonitorServiceConfiguration,
                mockSerialPortDiscoveryService.Object,
                mockDateTimeService.Object);

            mockDateTimeService.SetupGet(x => x.UtcNow).Returns(DateTime.UtcNow);

            var currentlyConnectedPorts = new List <string>();

            sut.PortsConnected += (sender, e) =>
            {
                foreach (var curPort in e.SerialPorts)
                {
                    lock (currentlyConnectedPorts)
                    {
                        currentlyConnectedPorts.Add(curPort);
                    }
                }
            };

            sut.PortsDisconnected += (sender, e) =>
            {
                foreach (var curPort in e.SerialPorts)
                {
                    lock (currentlyConnectedPorts)
                    {
                        currentlyConnectedPorts.Remove(curPort);
                    }
                }
            };

            // Act
            sut.Start();
            await PortConnectionActivity(mockSerialPortDiscoveryService).ConfigureAwait(false);

            // Assert
            Assert.Equal(2, currentlyConnectedPorts.Count);
            Assert.Contains(currentlyConnectedPorts, x => x == "COM3");
            Assert.Contains(currentlyConnectedPorts, x => x == "COM4");
        }