public void testMultiThreadedUniqueness() throws Exception { final int concurrency = 128, callsPerThread = 1000; List<Future<Set<Uid>>> handles = new ArrayList<Future<Set<Uid>>>(concurrency); ExecutorService executorService = Executors.newFixedThreadPool(concurrency); try { for (int i = 0; i < concurrency; i++) { handles.add(executorService.submit(new Callable<Set<Uid>>() { @Override public Set<Uid> call() throws Exception { Set<Uid> ids = new HashSet<Uid>(callsPerThread); for (int i = 0; i < callsPerThread; i++) ids.add(UidGenerator.generateUid()); return ids; } })); } } finally { executorService.shutdown(); } Set<Uid> allIds = new HashSet<Uid>(concurrency * callsPerThread); for (Future<Set<Uid>> handle : handles) allIds.addAll(handle.get()); assertEquals(concurrency * callsPerThread, allIds.size()); }
public void testMultiThreadedUniqueness() throws Exception { final int concurrency = 128, callsPerThread = 1000; List<Future<Set<Uid>>> handles = new ArrayList<Future<Set<Uid>>>(concurrency); ExecutorService executorService = Executors.newFixedThreadPool(concurrency); try { for (int i = 0; i < concurrency; i++) { handles.add(executorService.submit(new Callable<Set<Uid>>() { @Override public Set<Uid> call() throws Exception { Set<Uid> ids = new HashSet<Uid>(callsPerThread); for (int i = 0; i < callsPerThread; i++) ids.add(UidGenerator.generateUid()); return ids; } })); } } finally { executorService.shutdown(); } Set<Uid> allIds = new HashSet<Uid>(concurrency * callsPerThread); for (Future<Set<Uid>> handle : handles) allIds.addAll(handle.get()); assertEquals(concurrency * callsPerThread, allIds.size()); }
public void testExtracts() throws Exception { byte[] timestamp = Encoder.longToBytes(System.currentTimeMillis()); byte[] sequence = Encoder.intToBytes(1); byte[] serverId = "my-server-id".getBytes(); int uidLength = serverId.length + timestamp.length + sequence.length; byte[] uidArray = new byte[uidLength]; System.arraycopy(serverId, 0, uidArray, 0, serverId.length); System.arraycopy(timestamp, 0, uidArray, serverId.length, timestamp.length); System.arraycopy(sequence, 0, uidArray, serverId.length + timestamp.length, sequence.length); Uid uid = new Uid(uidArray); assertTrue(Arrays.equals(serverId, uid.extractServerId())); assertEquals(Encoder.bytesToLong(timestamp, 0), uid.extractTimestamp()); assertEquals(Encoder.bytesToInt(sequence, 0), uid.extractSequence()); }
public void testExtracts() throws Exception { byte[] timestamp = Encoder.longToBytes(System.currentTimeMillis()); byte[] sequence = Encoder.intToBytes(1); byte[] serverId = "my-server-id".getBytes(); int uidLength = serverId.length + timestamp.length + sequence.length; byte[] uidArray = new byte[uidLength]; System.arraycopy(serverId, 0, uidArray, 0, serverId.length); System.arraycopy(timestamp, 0, uidArray, serverId.length, timestamp.length); System.arraycopy(sequence, 0, uidArray, serverId.length + timestamp.length, sequence.length); Uid uid = new Uid(uidArray); assertTrue(Arrays.equals(serverId, uid.extractServerId())); assertEquals(Encoder.bytesToLong(timestamp, 0), uid.extractTimestamp()); assertEquals(Encoder.bytesToInt(sequence, 0), uid.extractSequence()); }
public void testHexaStringEncoder() throws Exception { byte[] result = Encoder.intToBytes(0x80); String hexString = new Uid(result).toString(); assertEquals("00000080", hexString); result = Encoder.longToBytes(0x81); hexString = new Uid(result).toString(); assertEquals("0000000000000081", hexString); result = Encoder.shortToBytes((short) 0xff); hexString = new Uid(result).toString(); assertEquals("00FF", hexString); }
public void testHexaStringEncoder() throws Exception { byte[] result = Encoder.intToBytes(0x80); String hexString = new Uid(result).toString(); assertEquals("00000080", hexString); result = Encoder.longToBytes(0x81); hexString = new Uid(result).toString(); assertEquals("0000000000000081", hexString); result = Encoder.shortToBytes((short) 0xff); hexString = new Uid(result).toString(); assertEquals("00FF", hexString); }