@Override public UuidGenerator.WithFixedBase createNew() { return new UuidGeneratorImpl().withFixedBase(); } }
@Test public void generate_from_FixedBase_concurrent_test() throws InterruptedException { UuidGenerator.WithFixedBase withFixedBase = underTest.withFixedBase(); int rounds = 500; List<byte[]> uuids1 = new ArrayList<>(rounds); List<byte[]> uuids2 = new ArrayList<>(rounds); AtomicInteger cnt = new AtomicInteger(); Thread t1 = new Thread(() -> { for (int i = 0; i < rounds; i++) { uuids1.add(withFixedBase.generate(cnt.getAndIncrement())); } }); Thread t2 = new Thread(() -> { for (int i = 0; i < rounds; i++) { uuids2.add(withFixedBase.generate(cnt.getAndIncrement())); } }); t1.start(); t2.start(); t1.join(); t2.join(); Base64.Encoder encoder = Base64.getEncoder(); Set<String> uuids = new HashSet<>(rounds * 2); uuids1.forEach(bytes -> uuids.add(encoder.encodeToString(bytes))); uuids2.forEach(bytes -> uuids.add(encoder.encodeToString(bytes))); assertThat(uuids).hasSize(rounds * 2); } }
@Test public void generate_concurrent_test() throws InterruptedException { int rounds = 500; List<byte[]> uuids1 = new ArrayList<>(rounds); List<byte[]> uuids2 = new ArrayList<>(rounds); Thread t1 = new Thread(() -> { for (int i = 0; i < rounds; i++) { uuids1.add(underTest.generate()); } }); Thread t2 = new Thread(() -> { for (int i = 0; i < rounds; i++) { uuids2.add(underTest.generate()); } }); t1.start(); t2.start(); t1.join(); t2.join(); Base64.Encoder encoder = Base64.getEncoder(); Set<String> uuids = new HashSet<>(rounds * 2); uuids1.forEach(bytes -> uuids.add(encoder.encodeToString(bytes))); uuids2.forEach(bytes -> uuids.add(encoder.encodeToString(bytes))); assertThat(uuids).hasSize(rounds * 2); }
@Test public void generate_returns_unique_values_without_common_initial_letter_given_more_than_one_millisecond_between_generate_calls() throws InterruptedException { Base64.Encoder encoder = Base64.getEncoder(); int count = 30; Set<String> uuids = new HashSet<>(count); for (int i = 0; i < count; i++) { Thread.sleep(5); uuids.add(encoder.encodeToString(underTest.generate())); } assertThat(uuids).hasSize(count); Iterator<String> iterator = uuids.iterator(); String firstUuid = iterator.next(); String base = firstUuid.substring(0, firstUuid.length() - 4); for (int i = 1; i < count; i++) { assertThat(iterator.next()).describedAs("i=" + i).doesNotStartWith(base); } }
@Override public UuidGenerator.WithFixedBase createNew() { return new UuidGeneratorImpl().withFixedBase(); } }