diff options
author | Sébastien Lesaint <sebastien.lesaint@sonarsource.com> | 2016-12-06 12:19:29 +0100 |
---|---|---|
committer | Sébastien Lesaint <sebastien.lesaint@sonarsource.com> | 2016-12-06 12:19:29 +0100 |
commit | f37be4d76f5615f249a11d8c88aa0482bb84f21a (patch) | |
tree | 7aa220e885a72b34f5feb58b7cea72bf03e2ac88 /sonar-core/src/test | |
parent | 79a8e50ccd27c442e35a757bca181d783e99b1ba (diff) | |
parent | aea44b9d8f223e201b5a4a2f0d85206880e76de5 (diff) | |
download | sonarqube-f37be4d76f5615f249a11d8c88aa0482bb84f21a.tar.gz sonarqube-f37be4d76f5615f249a11d8c88aa0482bb84f21a.zip |
Merge branch 'branch-6.2'
Diffstat (limited to 'sonar-core/src/test')
-rw-r--r-- | sonar-core/src/test/java/org/sonar/core/util/UuidGeneratorImplTest.java | 61 |
1 files changed, 60 insertions, 1 deletions
diff --git a/sonar-core/src/test/java/org/sonar/core/util/UuidGeneratorImplTest.java b/sonar-core/src/test/java/org/sonar/core/util/UuidGeneratorImplTest.java index 21200b0aa40..175aec720c6 100644 --- a/sonar-core/src/test/java/org/sonar/core/util/UuidGeneratorImplTest.java +++ b/sonar-core/src/test/java/org/sonar/core/util/UuidGeneratorImplTest.java @@ -19,10 +19,13 @@ */ package org.sonar.core.util; +import java.util.ArrayList; import java.util.Base64; import java.util.HashSet; import java.util.Iterator; +import java.util.List; import java.util.Set; +import java.util.concurrent.atomic.AtomicInteger; import org.junit.Test; import static org.assertj.core.api.Assertions.assertThat; @@ -31,7 +34,7 @@ public class UuidGeneratorImplTest { private UuidGeneratorImpl underTest = new UuidGeneratorImpl(); @Test - public void generate_returns_unique_values_without_common_initial_letter_given_more_than_one_milisecond_between_generate_calls() throws InterruptedException { + 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); @@ -50,6 +53,33 @@ public class UuidGeneratorImplTest { } @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_from_FixedBase_returns_unique_values_where_only_last_4_later_letter_change() { Base64.Encoder encoder = Base64.getEncoder(); int count = 100_000; @@ -68,4 +98,33 @@ public class UuidGeneratorImplTest { assertThat(iterator.next()).startsWith(base); } } + + @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); + } } |