Madan Jampani | 0583387 | 2016-07-12 23:01:39 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-present Open Networking Laboratory |
| 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.onosproject.core.impl; |
| 17 | |
| 18 | import java.util.concurrent.atomic.AtomicLong; |
| 19 | import java.util.function.Supplier; |
| 20 | |
| 21 | import org.junit.Assert; |
| 22 | import org.junit.Test; |
| 23 | import org.onosproject.core.HybridLogicalTime; |
| 24 | |
| 25 | /** |
| 26 | * Unit tests for {@link HybridLogicalClockManager}. |
| 27 | */ |
| 28 | public class HybridLogicalClockManagerTest { |
| 29 | |
| 30 | @Test |
| 31 | public void testLocalEvents() { |
| 32 | AtomicLong ticker = new AtomicLong(); |
| 33 | Supplier<Long> ptSource = ticker::get; |
| 34 | HybridLogicalClockManager clockManager = new HybridLogicalClockManager(); |
| 35 | clockManager.physicalTimeSource = ptSource; |
| 36 | |
| 37 | HybridLogicalTime time1 = clockManager.timeNow(); |
| 38 | Assert.assertEquals(0, time1.logicalTime()); |
| 39 | Assert.assertEquals(1, time1.logicalCounter()); |
| 40 | |
| 41 | HybridLogicalTime time2 = clockManager.timeNow(); |
| 42 | Assert.assertEquals(0, time2.logicalTime()); |
| 43 | Assert.assertEquals(2, time2.logicalCounter()); |
| 44 | |
| 45 | ticker.incrementAndGet(); |
| 46 | |
| 47 | HybridLogicalTime time3 = clockManager.timeNow(); |
| 48 | Assert.assertEquals(1, time3.logicalTime()); |
| 49 | Assert.assertEquals(0, time3.logicalCounter()); |
| 50 | |
| 51 | HybridLogicalTime time4 = clockManager.timeNow(); |
| 52 | Assert.assertEquals(1, time4.logicalTime()); |
| 53 | Assert.assertEquals(1, time4.logicalCounter()); |
| 54 | } |
| 55 | |
| 56 | @Test |
| 57 | public void testReceiveEvents() { |
| 58 | AtomicLong ticker = new AtomicLong(1); |
| 59 | Supplier<Long> ptSource = ticker::get; |
| 60 | HybridLogicalClockManager clockManager = new HybridLogicalClockManager(); |
| 61 | clockManager.physicalTimeSource = ptSource; |
| 62 | |
| 63 | HybridLogicalTime time1 = clockManager.timeNow(); |
| 64 | Assert.assertEquals(1, time1.logicalTime()); |
| 65 | Assert.assertEquals(0, time1.logicalCounter()); |
| 66 | |
| 67 | HybridLogicalTime eventTime1 = new HybridLogicalTime(1, 0); |
| 68 | clockManager.recordEventTime(eventTime1); |
| 69 | |
| 70 | Assert.assertEquals(1, clockManager.logicalTime()); |
| 71 | Assert.assertEquals(1, clockManager.logicalCounter()); |
| 72 | |
| 73 | HybridLogicalTime eventTime2 = new HybridLogicalTime(2, 0); |
| 74 | clockManager.recordEventTime(eventTime2); |
| 75 | |
| 76 | Assert.assertEquals(2, clockManager.logicalTime()); |
| 77 | Assert.assertEquals(1, clockManager.logicalCounter()); |
| 78 | |
| 79 | HybridLogicalTime eventTime3 = new HybridLogicalTime(2, 2); |
| 80 | clockManager.recordEventTime(eventTime3); |
| 81 | |
| 82 | Assert.assertEquals(2, clockManager.logicalTime()); |
| 83 | Assert.assertEquals(3, clockManager.logicalCounter()); |
| 84 | |
| 85 | HybridLogicalTime eventTime4 = new HybridLogicalTime(2, 1); |
| 86 | clockManager.recordEventTime(eventTime4); |
| 87 | |
| 88 | Assert.assertEquals(2, clockManager.logicalTime()); |
| 89 | Assert.assertEquals(4, clockManager.logicalCounter()); |
| 90 | |
| 91 | ticker.set(4); |
| 92 | |
| 93 | HybridLogicalTime eventTime5 = new HybridLogicalTime(3, 0); |
| 94 | clockManager.recordEventTime(eventTime5); |
| 95 | |
| 96 | Assert.assertEquals(4, clockManager.logicalTime()); |
| 97 | Assert.assertEquals(0, clockManager.logicalCounter()); |
| 98 | } |
| 99 | } |