You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@usergrid.apache.org by to...@apache.org on 2013/11/28 01:16:02 UTC
[2/2] git commit: Added tests for the UUID Generator
Added tests for the UUID Generator
Project: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/commit/a022fb17
Tree: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/tree/a022fb17
Diff: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/diff/a022fb17
Branch: refs/heads/two-dot-o
Commit: a022fb1744f439a661affb1ad6729cb0b4c1c5ce
Parents: c904e15
Author: Todd Nine <to...@apache.org>
Authored: Wed Nov 27 17:15:55 2013 -0700
Committer: Todd Nine <to...@apache.org>
Committed: Wed Nov 27 17:15:55 2013 -0700
----------------------------------------------------------------------
.../persistence/model/util/UUIDGenerator.java | 7 +-
.../model/util/UUIDGeneratorTest.java | 119 +++++++++++++++++++
2 files changed, 122 insertions(+), 4 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/a022fb17/stack/corepersistence/model/src/main/java/org/apache/usergrid/persistence/model/util/UUIDGenerator.java
----------------------------------------------------------------------
diff --git a/stack/corepersistence/model/src/main/java/org/apache/usergrid/persistence/model/util/UUIDGenerator.java b/stack/corepersistence/model/src/main/java/org/apache/usergrid/persistence/model/util/UUIDGenerator.java
index d8f22af..ed03660 100644
--- a/stack/corepersistence/model/src/main/java/org/apache/usergrid/persistence/model/util/UUIDGenerator.java
+++ b/stack/corepersistence/model/src/main/java/org/apache/usergrid/persistence/model/util/UUIDGenerator.java
@@ -20,7 +20,7 @@ import com.fasterxml.uuid.impl.TimeBasedGenerator;
public class UUIDGenerator {
- private static final TimestampSynchronizer synchronizer = new TimestampSynchronizer() {
+ private static final TimestampSynchronizer synchronize = new TimestampSynchronizer() {
/**
* Pointer to the last value we returned
@@ -75,7 +75,7 @@ public class UUIDGenerator {
*/
static {
try {
- timer = new UUIDTimer( random, synchronizer );
+ timer = new UUIDTimer( random, synchronize );
}
catch ( IOException e ) {
throw new RuntimeException( "Couldn't intialize timer", e );
@@ -83,8 +83,7 @@ public class UUIDGenerator {
}
- private static final TimeBasedGenerator generator =
- new TimeBasedGenerator( EthernetAddress.fromInterface(), timer );
+ private static final TimeBasedGenerator generator = new TimeBasedGenerator( EthernetAddress.fromInterface(), timer );
/** Create a new time uuid */
http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/a022fb17/stack/corepersistence/model/src/test/java/org/apache/usergrid/persistence/model/util/UUIDGeneratorTest.java
----------------------------------------------------------------------
diff --git a/stack/corepersistence/model/src/test/java/org/apache/usergrid/persistence/model/util/UUIDGeneratorTest.java b/stack/corepersistence/model/src/test/java/org/apache/usergrid/persistence/model/util/UUIDGeneratorTest.java
new file mode 100644
index 0000000..ef1a9e9
--- /dev/null
+++ b/stack/corepersistence/model/src/test/java/org/apache/usergrid/persistence/model/util/UUIDGeneratorTest.java
@@ -0,0 +1,119 @@
+package org.apache.usergrid.persistence.model.util;
+
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+import java.util.UUID;
+import java.util.concurrent.Callable;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.Future;
+
+import org.junit.Test;
+
+import com.fasterxml.uuid.UUIDComparator;
+import com.google.common.collect.Sets;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+
+/** @author tnine */
+public class UUIDGeneratorTest {
+
+
+ @Test
+ public void testOrderingConcurrency() throws InterruptedException, ExecutionException {
+
+
+ //either all processor count, or 2 threads
+ final int numberThreads = Math.max( Runtime.getRuntime().availableProcessors(), 2 );
+
+ /**
+ * 10k uuids per thread
+ */
+ final int count = 10000;
+
+ ExecutorService executor = Executors.newFixedThreadPool( numberThreads );
+
+ List<UUIDConsumer> consumers = new ArrayList<UUIDConsumer>( numberThreads );
+
+ for ( int i = 0; i < numberThreads; i++ ) {
+ consumers.add( new UUIDConsumer( count ) );
+ }
+
+ List<Future<Void>> futures = executor.invokeAll( consumers );
+
+ //wait for them all to finish
+ for(Future<Void> future: futures){
+ future.get();
+ }
+
+ //now validate each one is in order and does not intersect with any other
+ for(int i = 0; i < numberThreads; i ++){
+
+ UUIDConsumer current = consumers.get( i );
+
+ current.validateOrder();
+
+ for(int j = i+1; j < numberThreads; j++){
+ current.noIntersect( consumers.get( j ) );
+ }
+ }
+
+ }
+
+
+ private static class UUIDConsumer implements Callable<Void> {
+
+ private final int toGenerate;
+ private final List<UUID> results;
+
+
+ private UUIDConsumer( final int toGenerate ) {
+ this.toGenerate = toGenerate;
+ this.results = new ArrayList<UUID>( toGenerate );
+ }
+
+
+ /**
+ * Validate that each UUID is greater than than it's previous entry when comparing them
+ */
+ public void validateOrder() {
+
+ for(int i = 0; i < toGenerate -1; i ++){
+ int comparison = UUIDComparator.staticCompare( results.get( i ), results.get( i+1 ) );
+ assertTrue(comparison < 0);
+ }
+ }
+
+
+ /**
+ * Validate the other UUID consumer does not have any intersection with this consumer
+ * @param other
+ */
+ public void noIntersect(UUIDConsumer other){
+
+ Set<UUID> firstSet = new HashSet<UUID>(results);
+ Set<UUID> otherSet = new HashSet<UUID>(other.results);
+
+ Set<UUID> intersection = Sets.intersection(firstSet, otherSet);
+
+ assertEquals("No UUID Generator should have a UUID that intersects with another UUID", 0, intersection.size());
+ }
+
+
+ @Override
+ public Void call() throws Exception {
+ for ( int i = 0; i < toGenerate; i++ ) {
+ this.results.add( UUIDGenerator.newTimeUUID() );
+ }
+
+ return null;
+ }
+ }
+}