Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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 | */ |
Ray Milkey | d083bc4 | 2015-07-22 16:34:22 -0700 | [diff] [blame] | 16 | package org.onosproject.store.service; |
Madan Jampani | fd26ffb | 2014-10-13 14:08:55 -0700 | [diff] [blame] | 17 | |
| 18 | import static org.junit.Assert.assertTrue; |
| 19 | |
| 20 | import java.nio.ByteBuffer; |
| 21 | |
| 22 | import org.junit.Test; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 23 | import org.onosproject.store.Timestamp; |
Yuta HIGUCHI | 8d143d2 | 2014-10-19 23:15:09 -0700 | [diff] [blame] | 24 | import org.onlab.util.KryoNamespace; |
Madan Jampani | fd26ffb | 2014-10-13 14:08:55 -0700 | [diff] [blame] | 25 | |
| 26 | import com.google.common.testing.EqualsTester; |
| 27 | |
| 28 | /** |
| 29 | * Tests for {@link WallClockTimestamp}. |
| 30 | */ |
| 31 | public class WallClockTimestampTest { |
| 32 | |
| 33 | @Test |
| 34 | public final void testBasic() throws InterruptedException { |
| 35 | WallClockTimestamp ts1 = new WallClockTimestamp(); |
| 36 | Thread.sleep(50); |
| 37 | WallClockTimestamp ts2 = new WallClockTimestamp(); |
Ray Milkey | d083bc4 | 2015-07-22 16:34:22 -0700 | [diff] [blame] | 38 | long stamp = System.currentTimeMillis() + 10000; |
| 39 | WallClockTimestamp ts3 = new WallClockTimestamp(stamp); |
| 40 | |
Madan Jampani | fd26ffb | 2014-10-13 14:08:55 -0700 | [diff] [blame] | 41 | |
| 42 | assertTrue(ts1.compareTo(ts1) == 0); |
| 43 | assertTrue(ts2.compareTo(ts1) > 0); |
| 44 | assertTrue(ts1.compareTo(ts2) < 0); |
Ray Milkey | d083bc4 | 2015-07-22 16:34:22 -0700 | [diff] [blame] | 45 | assertTrue(ts3.unixTimestamp() == stamp); |
Madan Jampani | fd26ffb | 2014-10-13 14:08:55 -0700 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | @Test |
| 49 | public final void testKryoSerializable() { |
| 50 | WallClockTimestamp ts1 = new WallClockTimestamp(); |
Ray Milkey | d083bc4 | 2015-07-22 16:34:22 -0700 | [diff] [blame] | 51 | WallClockTimestamp ts2 = new WallClockTimestamp(System.currentTimeMillis() + 10000); |
Madan Jampani | fd26ffb | 2014-10-13 14:08:55 -0700 | [diff] [blame] | 52 | final ByteBuffer buffer = ByteBuffer.allocate(1 * 1024 * 1024); |
Yuta HIGUCHI | 8d143d2 | 2014-10-19 23:15:09 -0700 | [diff] [blame] | 53 | final KryoNamespace kryos = KryoNamespace.newBuilder() |
Madan Jampani | fd26ffb | 2014-10-13 14:08:55 -0700 | [diff] [blame] | 54 | .register(WallClockTimestamp.class) |
| 55 | .build(); |
| 56 | |
| 57 | kryos.serialize(ts1, buffer); |
| 58 | buffer.flip(); |
| 59 | Timestamp copy = kryos.deserialize(buffer); |
| 60 | |
| 61 | new EqualsTester() |
Ray Milkey | d083bc4 | 2015-07-22 16:34:22 -0700 | [diff] [blame] | 62 | .addEqualityGroup(ts1, copy) |
| 63 | .addEqualityGroup(ts2) |
| 64 | .testEquals(); |
Madan Jampani | fd26ffb | 2014-10-13 14:08:55 -0700 | [diff] [blame] | 65 | } |
| 66 | } |