Jonathan Hart | 4f178fa | 2020-02-03 10:46:01 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2020-present Open Networking Foundation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.opencord.olt.impl; |
| 17 | |
| 18 | import org.junit.Before; |
| 19 | import org.junit.Test; |
| 20 | import org.onosproject.cluster.NodeId; |
| 21 | import org.onosproject.net.DeviceId; |
| 22 | |
| 23 | import java.util.ArrayList; |
| 24 | import java.util.List; |
| 25 | |
| 26 | import static org.hamcrest.MatcherAssert.assertThat; |
| 27 | import static org.hamcrest.Matchers.equalTo; |
| 28 | |
| 29 | public class ConsistentHasherTest { |
| 30 | |
| 31 | private static final int WEIGHT = 10; |
| 32 | |
| 33 | private static final NodeId N1 = new NodeId("10.0.0.1"); |
| 34 | private static final NodeId N2 = new NodeId("10.0.0.2"); |
| 35 | private static final NodeId N3 = new NodeId("10.0.0.3"); |
| 36 | |
| 37 | private ConsistentHasher hasher; |
| 38 | |
| 39 | @Before |
| 40 | public void setUp() { |
| 41 | List<NodeId> servers = new ArrayList<>(); |
| 42 | servers.add(N1); |
| 43 | servers.add(N2); |
| 44 | |
| 45 | hasher = new ConsistentHasher(servers, WEIGHT); |
| 46 | } |
| 47 | |
| 48 | @Test |
| 49 | public void testHasher() { |
| 50 | DeviceId deviceId = DeviceId.deviceId("foo"); |
| 51 | NodeId server = hasher.hash(deviceId.toString()); |
| 52 | |
| 53 | assertThat(server, equalTo(N1)); |
| 54 | |
| 55 | deviceId = DeviceId.deviceId("bsaf"); |
| 56 | server = hasher.hash(deviceId.toString()); |
| 57 | |
| 58 | assertThat(server, equalTo(N2)); |
| 59 | } |
| 60 | |
| 61 | @Test |
| 62 | public void testAddServer() { |
| 63 | DeviceId deviceId = DeviceId.deviceId("foo"); |
| 64 | NodeId server = hasher.hash(deviceId.toString()); |
| 65 | |
| 66 | assertThat(server, equalTo(N1)); |
| 67 | |
| 68 | hasher.addServer(N3); |
| 69 | |
| 70 | server = hasher.hash(deviceId.toString()); |
| 71 | |
| 72 | assertThat(server, equalTo(N3)); |
| 73 | } |
| 74 | } |