]> git.basschouten.com Git - openhab-addons.git/blob
c2b6a1ad67c2af1d7f1c886099e0bae67b52605e
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
7  * This program and the accompanying materials are made available under the
8  * terms of the Eclipse Public License 2.0 which is available at
9  * http://www.eclipse.org/legal/epl-2.0
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.homematic.internal.handler;
14
15 import static org.hamcrest.CoreMatchers.is;
16 import static org.hamcrest.CoreMatchers.not;
17 import static org.hamcrest.MatcherAssert.assertThat;
18
19 import java.util.ArrayList;
20 import java.util.List;
21
22 import org.junit.jupiter.api.BeforeEach;
23 import org.junit.jupiter.api.Test;
24
25 /**
26  * Tests for {@link org.openhab.binding.homematic.internal.handler.SimplePortPool}.
27  *
28  * @author Florian Stolte - Initial Contribution
29  */
30 public class SimplePortPoolTest {
31
32     private SimplePortPool simplePortPool;
33
34     @BeforeEach
35     public void setup() {
36         this.simplePortPool = new SimplePortPool();
37     }
38
39     @Test
40     public void testPoolOnlyGivesOutPortsNotInUse() {
41         final List<Integer> ports = acquireSomePorts();
42
43         final int newPort = simplePortPool.getNextPort();
44
45         for (int port : ports) {
46             assertThat(port, not(is(newPort)));
47         }
48     }
49
50     @Test
51     public void testPoolReusesReleasedPort() {
52         final List<Integer> ports = acquireSomePorts();
53         final int firstPort = ports.get(0);
54
55         simplePortPool.release(firstPort);
56         int newPortAfterRelease = simplePortPool.getNextPort();
57
58         assertThat(newPortAfterRelease, is(firstPort));
59     }
60
61     @Test
62     public void testPoolSkipsPortsThatAreSetInUse() {
63         final int firstPort = simplePortPool.getNextPort();
64
65         simplePortPool.setInUse(firstPort + 1);
66         int newPortAfterSetInUse = simplePortPool.getNextPort();
67
68         assertThat(newPortAfterSetInUse, is(firstPort + 2));
69     }
70
71     private List<Integer> acquireSomePorts() {
72         final int numberOfPorts = 5;
73         final List<Integer> ports = new ArrayList<>(numberOfPorts);
74
75         for (int i = 0; i < numberOfPorts; i++) {
76             ports.add(simplePortPool.getNextPort());
77         }
78
79         return ports;
80     }
81 }