Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem: HybridTimestamp Comparable Serializable isn't correct #197

Merged
merged 1 commit into from Jan 12, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions eventsourcing-hlc/build.gradle
Expand Up @@ -7,4 +7,6 @@ dependencies {

compile project(':eventsourcing-layout')

testCompile 'com.pholser:junit-quickcheck:0.7'
testCompile 'com.pholser:junit-quickcheck-generators:0.7'
}
Expand Up @@ -13,14 +13,15 @@
import lombok.Getter;
import org.apache.commons.net.ntp.TimeStamp;

import java.math.BigInteger;
import java.util.Date;

/**
* HybridTimestamp implements <a href="http://www.cse.buffalo.edu/tech-reports/2014-04.pdf">Hybrid Logical Clock</a>,
* currently heavily inspired by a corresponding <a href="https://github.com/tschottdorf/hlc-rs">Rust library</a>.
*/
@LayoutName("rfc.eventsourcing.com/spec:6/HLC/#Timestamp")
public class HybridTimestamp implements Comparable<HybridTimestamp>, SerializableComparable<Long> {
public class HybridTimestamp implements Comparable<HybridTimestamp>, SerializableComparable<BigInteger> {

private final PhysicalTimeProvider physicalTimeProvider;

Expand Down Expand Up @@ -165,7 +166,11 @@ public String toString() {
() + ">";
}

@Override public Long getSerializableComparable() {
return TimeStamp.getNtpTime(timestamp()).getDate().getTime();
@Override public BigInteger getSerializableComparable() {
TimeStamp t = new TimeStamp(logicalTime);
return BigInteger.valueOf(t.getSeconds())
.shiftLeft(64)
.add(BigInteger.valueOf(t.getFraction()).shiftLeft(32))
.add(BigInteger.valueOf(logicalCounter));
}
}
@@ -0,0 +1,41 @@
/**
* Copyright (c) 2016, All Contributors (see CONTRIBUTORS file)
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
package com.eventsourcing.hlc;

import com.google.common.base.Joiner;
import com.pholser.junit.quickcheck.Property;
import com.pholser.junit.quickcheck.runner.JUnitQuickcheck;
import lombok.SneakyThrows;
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.RunWith;
import org.testng.annotations.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

@RunWith(JUnitQuickcheck.class)
public class SerializableComparableTest {

@Property(trials = 1_000)
public void test(long logicalTime, long logicalCounter, long logicalTime1, long logicalCounter1) {
HybridTimestamp ts1 = new HybridTimestamp(logicalTime, logicalCounter);
HybridTimestamp ts2 = new HybridTimestamp(logicalTime1, logicalCounter1);
assertEquals(ts1.compareTo(ts2), ts1.getSerializableComparable().compareTo(ts2.getSerializableComparable()));
}

@Test
@SneakyThrows
public void run() {
JUnitCore junit = new JUnitCore();
Result result = junit.run(SerializableComparableTest.class);
if (!result.wasSuccessful()) {
fail(Joiner.on("\n").join(result.getFailures()));
}
}
}