blob: a2f022da0b1562cb248c96ba21bb9d1eff019f86 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
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
33 @Test
34 public final void testBasic() throws InterruptedException {
35 WallClockTimestamp ts1 = new WallClockTimestamp();
36 Thread.sleep(50);
37 WallClockTimestamp ts2 = new WallClockTimestamp();
Ray Milkeyd083bc42015-07-22 16:34:22 -070038 long stamp = System.currentTimeMillis() + 10000;
39 WallClockTimestamp ts3 = new WallClockTimestamp(stamp);
40
Madan Jampanifd26ffb2014-10-13 14:08:55 -070041
42 assertTrue(ts1.compareTo(ts1) == 0);
43 assertTrue(ts2.compareTo(ts1) > 0);
44 assertTrue(ts1.compareTo(ts2) < 0);
Ray Milkeyd083bc42015-07-22 16:34:22 -070045 assertTrue(ts3.unixTimestamp() == stamp);
Madan Jampanifd26ffb2014-10-13 14:08:55 -070046 }
47
48 @Test
49 public final void testKryoSerializable() {
50 WallClockTimestamp ts1 = new WallClockTimestamp();
Ray Milkeyd083bc42015-07-22 16:34:22 -070051 WallClockTimestamp ts2 = new WallClockTimestamp(System.currentTimeMillis() + 10000);
Madan Jampanifd26ffb2014-10-13 14:08:55 -070052 final ByteBuffer buffer = ByteBuffer.allocate(1 * 1024 * 1024);
Yuta HIGUCHI8d143d22014-10-19 23:15:09 -070053 final KryoNamespace kryos = KryoNamespace.newBuilder()
Madan Jampanifd26ffb2014-10-13 14:08:55 -070054 .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 Milkeyd083bc42015-07-22 16:34:22 -070062 .addEqualityGroup(ts1, copy)
63 .addEqualityGroup(ts2)
64 .testEquals();
Madan Jampanifd26ffb2014-10-13 14:08:55 -070065 }
66}