View Javadoc
1   /*
2    * Copyright © 2016 Greg Chabala
3    *
4    * This file is part of brick-control-lab.
5    *
6    * brick-control-lab is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU Lesser General Public License as
8    * published by the Free Software Foundation, either version 3 of the
9    * License, or (at your option) any later version.
10   *
11   * brick-control-lab is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU Lesser General Public License for more details.
15   *
16   * You should have received a copy of the GNU Lesser General Public License
17   * along with brick-control-lab.  If not, see http://www.gnu.org/licenses/.
18   */
19  package org.chabala.brick.controllab;
20  
21  import org.junit.Rule;
22  import org.junit.Test;
23  import org.mockito.Mock;
24  import org.mockito.junit.MockitoJUnit;
25  import org.mockito.junit.MockitoRule;
26  
27  import java.time.Duration;
28  
29  import static org.mockito.Mockito.*;
30  
31  /**
32   * Testing the {@link KeepAliveMonitor}.
33   */
34  public class KeepAliveMonitorTest {
35  
36      private final Duration keepAliveDuration = Duration.ofMillis(500);
37      private final Duration nineTenthsDuration = keepAliveDuration.multipliedBy(9).dividedBy(10);
38      private final long keepAliveDurationMs = keepAliveDuration.toMillis();
39  
40      @Rule
41      public MockitoRule rule = MockitoJUnit.rule();
42  
43      @Mock
44      private SerialPortWriter serialPortWriter;
45  
46      @Test
47      public void testMonitorSendsKeepAlives() throws Exception {
48          when(serialPortWriter.getPortName()).thenReturn("COM#");
49          try (KeepAliveMonitor monitor = new KeepAliveMonitor(serialPortWriter, nineTenthsDuration)) {
50              verify(serialPortWriter, after(keepAliveDurationMs).times(1)).sendCommand(anyByte(), any());
51              verify(serialPortWriter, after(keepAliveDurationMs).times(2)).sendCommand(anyByte(), any());
52          }
53      }
54  
55      @Test
56      public void testResetPreventsKeepAlives() throws Exception {
57          long threeQuartersDurationMs = keepAliveDuration.multipliedBy(3).dividedBy(4).toMillis();
58          try (KeepAliveMonitor monitor = new KeepAliveMonitor(serialPortWriter, nineTenthsDuration)) {
59              verify(serialPortWriter, after(threeQuartersDurationMs).never()).sendCommand(anyByte(), any());
60              monitor.reset();
61              verify(serialPortWriter, after(threeQuartersDurationMs).never()).sendCommand(anyByte(), any());
62              verify(serialPortWriter, only()).getPortName();
63          }
64      }
65  
66      @Test
67      public void testClosePreventsKeepAlives() throws Exception {
68          try (KeepAliveMonitor monitor = new KeepAliveMonitor(serialPortWriter, nineTenthsDuration)) {
69              monitor.close();
70              verify(serialPortWriter, after(keepAliveDuration.multipliedBy(2).toMillis()).only()).getPortName();
71          }
72      }
73  }