blob: 8ea04fde0aa71738d76873bc3b5f9c2e2ed366e4 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07003 *
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 Milkeyd083bc42015-07-22 16:34:22 -070016package org.onosproject.store.service;
Madan Jampanifd26ffb2014-10-13 14:08:55 -070017
18import static org.junit.Assert.assertTrue;
19
20import java.nio.ByteBuffer;
21
22import org.junit.Test;
Brian O'Connorabafb502014-12-02 22:26:20 -080023import org.onosproject.store.Timestamp;
Yuta HIGUCHI8d143d22014-10-19 23:15:09 -070024import org.onlab.util.KryoNamespace;
Madan Jampanifd26ffb2014-10-13 14:08:55 -070025
26import com.google.common.testing.EqualsTester;
27
28/**
29 * Tests for {@link WallClockTimestamp}.
30 */
31public class WallClockTimestampTest {
32
Yuta HIGUCHIfbd9ae92018-01-24 23:39:06 -080033 @SuppressWarnings("SelfComparison")
Madan Jampanifd26ffb2014-10-13 14:08:55 -070034 @Test
35 public final void testBasic() throws InterruptedException {
36 WallClockTimestamp ts1 = new WallClockTimestamp();
37 Thread.sleep(50);
38 WallClockTimestamp ts2 = new WallClockTimestamp();
Ray Milkeyd083bc42015-07-22 16:34:22 -070039 long stamp = System.currentTimeMillis() + 10000;
40 WallClockTimestamp ts3 = new WallClockTimestamp(stamp);
41
Madan Jampanifd26ffb2014-10-13 14:08:55 -070042
43 assertTrue(ts1.compareTo(ts1) == 0);
44 assertTrue(ts2.compareTo(ts1) > 0);
45 assertTrue(ts1.compareTo(ts2) < 0);
Ray Milkeyd083bc42015-07-22 16:34:22 -070046 assertTrue(ts3.unixTimestamp() == stamp);
Madan Jampanifd26ffb2014-10-13 14:08:55 -070047 }
48
49 @Test
50 public final void testKryoSerializable() {
51 WallClockTimestamp ts1 = new WallClockTimestamp();
Ray Milkeyd083bc42015-07-22 16:34:22 -070052 WallClockTimestamp ts2 = new WallClockTimestamp(System.currentTimeMillis() + 10000);
Madan Jampanifd26ffb2014-10-13 14:08:55 -070053 final ByteBuffer buffer = ByteBuffer.allocate(1 * 1024 * 1024);
Yuta HIGUCHI8d143d22014-10-19 23:15:09 -070054 final KryoNamespace kryos = KryoNamespace.newBuilder()
Madan Jampanifd26ffb2014-10-13 14:08:55 -070055 .register(WallClockTimestamp.class)
56 .build();
57
58 kryos.serialize(ts1, buffer);
59 buffer.flip();
60 Timestamp copy = kryos.deserialize(buffer);
61
62 new EqualsTester()
Ray Milkeyd083bc42015-07-22 16:34:22 -070063 .addEqualityGroup(ts1, copy)
64 .addEqualityGroup(ts2)
65 .testEquals();
Madan Jampanifd26ffb2014-10-13 14:08:55 -070066 }
67}