Open Kilda Java Documentation
DiscoveryLinkTest.java
Go to the documentation of this file.
1 /* Copyright 2017 Telstra Open Source
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 package org.openkilda.wfm.isl;
17 
18 import static org.junit.Assert.assertEquals;
19 
23 
24 import org.junit.Before;
25 import org.junit.Test;
26 
27 public class DiscoveryLinkTest {
28 
29  private DiscoveryLink dn;
30  private static final SwitchId switchName = new SwitchId("ff:01");
31  private static final int portNumber = 1;
32 
33  @Before
34  public void setUp() throws Exception {
35  dn = new DiscoveryLink(switchName, portNumber, 1, 10);
36  }
37 
41  @Test
42  public void testDefaultLinkDiscovered() {
43  // initial state should be false
44  assertEquals(false, dn.isActive());
45  }
46 
47  @Test
49  dn.activate(new NetworkEndpoint(new SwitchId("ff:02"), 2));
50  assertEquals(true, dn.isActive());
51  }
52 
56  @Test
57  public void forlorn() {
58  int threshhold = 2;
59  dn = new DiscoveryLink(new SwitchId("ff:01"), 2, 0, threshhold);
60  assertEquals("A DN starts out as not excluded", true, dn.isNewAttemptAllowed());
61  dn.fail();
62  dn.fail();
63  dn.fail();
64  assertEquals("The DN should now be excluded", false, dn.isNewAttemptAllowed());
65  }
66 
70  @Test
71  public void failure() {
72  assertEquals(0, dn.getConsecutiveFailure());
73  dn.fail();
74  assertEquals(1, dn.getConsecutiveFailure());
76  assertEquals(0, dn.getConsecutiveFailure());
77  }
78 
82  @Test
83  public void getConsecutiveSuccess() {
84  assertEquals(0, dn.getConsecutiveSuccess());
85  dn.success();
86  assertEquals(1, dn.getConsecutiveSuccess());
88  assertEquals(0, dn.getConsecutiveSuccess());
89  }
90 
94  @Test
95  public void incAttempts() {
96  assertEquals(0, dn.getAttempts());
97  dn.incAttempts();
98  int attemptLimit = 2;
99  assertEquals(1, dn.getAttempts());
100  assertEquals(false, dn.isAttemptsLimitExceeded(attemptLimit));
101  dn.incAttempts();
102  assertEquals(2, dn.getAttempts());
103  assertEquals(false, dn.isAttemptsLimitExceeded(attemptLimit));
104  dn.incAttempts();
105  assertEquals(3, dn.getAttempts());
106  assertEquals(true, dn.isAttemptsLimitExceeded(attemptLimit));
107  }
108 
109  @Test
110  public void renew() {
111  dn.incAttempts();
112  dn.tick();
113  assertEquals(1, dn.getAttempts());
114  assertEquals(1, dn.getTimeCounter());
115  // renew clears both attempts and ticks
116  dn.renew();
117  assertEquals(0, dn.getAttempts());
118  assertEquals(0, dn.getTimeCounter());
119  }
120 
121  @Test
122  public void logTick() {
123  dn.tick();
124  dn.timeToCheck();
125  dn.resetTickCounter();
126  assertEquals(0, dn.getTimeCounter());
127 
128  }
129 }