### Question:
KeyGenerator { public static String createKey() { final long now = clock.getAsLong(); final ByteBuffer buffer = ByteBuffer.wrap(new byte[8 + 1 + 8]); buffer.putLong(now); buffer.put(randomnessByte); buffer.putLong(getRandomPart(now)); return encodeKey(buffer.array()); } private  KeyGenerator(); static String createKey(); static long getKeyTimeMillis(String key); static String recreateKey(long timestamp, int random1, long random2);  }### Answer:
@Test public void testCreateKey() { String last = KeyGenerator.createKey(); for (int i = 0; i < 1000000; i++) { final String lastKey = last; final String key = KeyGenerator.createKey(); assertThat(key).is(new Condition<>((other) -> lastKey.compareTo(other) < 0, "greater than " + lastKey)); last = key; } }
@Test public void testCreateKeyMultithreaded() { final int count = 100000; final Collection<Callable<String>> tasks = IntStream.range(0, count).boxed() .map(i -> (Callable<String>) () -> KeyGenerator.createKey()).collect(Collectors.toList()); final ForkJoinPool pool = ForkJoinPool.commonPool(); final List<Future<String>> results = pool.invokeAll(tasks); final Set<String> keys = results.stream().map(t -> { try { return t.get(); } catch (InterruptedException | ExecutionException e) { throw new IllegalStateException(e); } }).collect(Collectors.toSet()); Assert.assertEquals("If " + count + " key generations are performed in parallel, it should yield " + count + " of distinct keys", count, keys.size()); }
@Test public void shouldGenerateKeysInChronologicalOrder() { for (int rnd = Byte.MIN_VALUE; rnd <= Byte.MAX_VALUE; rnd++) { KeyGenerator.randomnessByte = (byte) rnd; KeyGenerator.clock = () -> 1534509528000L; final String newer = KeyGenerator.createKey(); KeyGenerator.clock = () -> 1534245726000L; final String older = KeyGenerator.createKey(); assertThat(newer.compareTo(older)).isGreaterThan(0); } }