test: handle clock drift #18

move factory tests to the root package.
This commit is contained in:
Fabio Lima 2022-04-21 14:08:02 -03:00
parent 236044af02
commit 5abd3f46ff
5 changed files with 41 additions and 71 deletions

View File

@ -3,13 +3,10 @@ package com.github.f4b6a3.ulid;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.junit.runners.Suite; import org.junit.runners.Suite;
import com.github.f4b6a3.ulid.factory.DefaultFactoryTest;
import com.github.f4b6a3.ulid.factory.MonotonicFactoryTest;
@RunWith(Suite.class) @RunWith(Suite.class)
@Suite.SuiteClasses({ @Suite.SuiteClasses({
MonotonicFactoryTest.class, UlidFactoryMonotonicTest.class,
DefaultFactoryTest.class, UlidFactoryDefaultfTest.class,
UlidTest.class, UlidTest.class,
}) })

View File

@ -1,4 +1,4 @@
package com.github.f4b6a3.ulid.factory; package com.github.f4b6a3.ulid;
import org.junit.Test; import org.junit.Test;
@ -8,10 +8,9 @@ import com.github.f4b6a3.ulid.UlidFactory;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import java.util.HashSet;
import java.util.Random; import java.util.Random;
public class DefaultFactoryTest extends UlidFactoryTest { public class UlidFactoryDefaultfTest extends UlidFactoryTest {
@Test @Test
public void testGetUlid() { public void testGetUlid() {
@ -30,35 +29,6 @@ public class DefaultFactoryTest extends UlidFactoryTest {
checkCreationTime(list, startTime, endTime); checkCreationTime(list, startTime, endTime);
} }
private void checkNullOrInvalid(Ulid[] list) {
for (Ulid ulid : list) {
assertNotNull("ULID is null", ulid);
}
}
private void checkUniqueness(Ulid[] list) {
HashSet<Ulid> set = new HashSet<>();
for (Ulid ulid : list) {
assertTrue(String.format("ULID is duplicated %s", ulid), set.add(ulid));
}
assertEquals("There are duplicated ULIDs", set.size(), list.length);
}
private void checkCreationTime(Ulid[] list, long startTime, long endTime) {
assertTrue("Start time was after end time", startTime <= endTime);
for (Ulid ulid : list) {
long creationTime = ulid.getTime();
assertTrue("Creation time was before start time " + creationTime + " " + startTime,
creationTime >= startTime);
assertTrue("Creation time was after end time", creationTime <= endTime);
}
}
@Test @Test
public void testGetUlidInParallel() throws InterruptedException { public void testGetUlidInParallel() throws InterruptedException {

View File

@ -1,4 +1,4 @@
package com.github.f4b6a3.ulid.factory; package com.github.f4b6a3.ulid;
import org.junit.Test; import org.junit.Test;
@ -9,10 +9,9 @@ import com.github.f4b6a3.ulid.UlidFactory;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashSet;
import java.util.Random; import java.util.Random;
public class MonotonicFactoryTest extends UlidFactoryTest { public class UlidFactoryMonotonicTest extends UlidFactoryTest {
@Test @Test
public void testGetUlid() { public void testGetUlid() {
@ -32,35 +31,6 @@ public class MonotonicFactoryTest extends UlidFactoryTest {
checkCreationTime(list, startTime, endTime); checkCreationTime(list, startTime, endTime);
} }
private void checkNullOrInvalid(Ulid[] list) {
for (Ulid ulid : list) {
assertNotNull("ULID is null", ulid);
}
}
private void checkUniqueness(Ulid[] list) {
HashSet<Ulid> set = new HashSet<>();
for (Ulid ulid : list) {
assertTrue(String.format("ULID is duplicated %s", ulid), set.add(ulid));
}
assertEquals("There are duplicated ULIDs", set.size(), list.length);
}
private void checkCreationTime(Ulid[] list, long startTime, long endTime) {
assertTrue("Start time was after end time", startTime <= endTime);
for (Ulid ulid : list) {
long creationTime = ulid.getTime();
assertTrue("Creation time was before start time " + creationTime + " " + startTime,
creationTime >= startTime);
assertTrue("Creation time was after end time", creationTime <= endTime);
}
}
private void checkOrdering(Ulid[] list) { private void checkOrdering(Ulid[] list) {
Ulid[] other = Arrays.copyOf(list, list.length); Ulid[] other = Arrays.copyOf(list, list.length);
Arrays.sort(other); Arrays.sort(other);

View File

@ -1,4 +1,8 @@
package com.github.f4b6a3.ulid.factory; package com.github.f4b6a3.ulid;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.util.HashSet; import java.util.HashSet;
import java.util.Random; import java.util.Random;
@ -27,6 +31,35 @@ public abstract class UlidFactoryTest {
return processors; return processors;
} }
protected void checkNullOrInvalid(Ulid[] list) {
for (Ulid ulid : list) {
assertNotNull("ULID is null", ulid);
}
}
protected void checkUniqueness(Ulid[] list) {
HashSet<Ulid> set = new HashSet<>();
for (Ulid ulid : list) {
assertTrue(String.format("ULID is duplicated %s", ulid), set.add(ulid));
}
assertEquals("There are duplicated ULIDs", set.size(), list.length);
}
protected void checkCreationTime(Ulid[] list, long startTime, long endTime) {
assertTrue("Start time was after end time", startTime <= endTime);
for (Ulid ulid : list) {
long creationTime = ulid.getTime();
assertTrue("Creation time was before start time " + creationTime + " " + startTime,
creationTime >= startTime);
assertTrue("Creation time was after end time", creationTime <= endTime);
}
}
protected static class TestThread extends Thread { protected static class TestThread extends Thread {
public static Set<UUID> hashSet = new HashSet<>(); public static Set<UUID> hashSet = new HashSet<>();

View File

@ -16,7 +16,7 @@ public class DemoTest {
for (int i = 0; i < max; i++) { for (int i = 0; i < max; i++) {
System.out.println(UlidCreator.getUlid()); System.out.println(UlidCreator.getUlid());
} }
System.out.println(HORIZONTAL_LINE); System.out.println(HORIZONTAL_LINE);
System.out.println("### Monotonic ULID"); System.out.println("### Monotonic ULID");
System.out.println(HORIZONTAL_LINE); System.out.println(HORIZONTAL_LINE);