|
| 1 | +package io.github.cooperlyt.cloud.uid; |
| 2 | + |
| 3 | +import io.github.cooperlyt.cloud.uid.impl.CachedUidGenerator; |
| 4 | +import org.apache.commons.lang.StringUtils; |
| 5 | +import org.junit.jupiter.api.Assertions; |
| 6 | +import org.junit.jupiter.api.Disabled; |
| 7 | +import org.junit.jupiter.api.Test; |
| 8 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 9 | +import org.springframework.beans.factory.annotation.Autowired; |
| 10 | +import org.springframework.boot.test.context.SpringBootTest; |
| 11 | +import org.springframework.test.context.junit.jupiter.SpringExtension; |
| 12 | + |
| 13 | +import java.io.IOException; |
| 14 | +import java.util.ArrayList; |
| 15 | +import java.util.HashSet; |
| 16 | +import java.util.List; |
| 17 | +import java.util.Set; |
| 18 | +import java.util.concurrent.ConcurrentSkipListSet; |
| 19 | +import java.util.concurrent.atomic.AtomicInteger; |
| 20 | + |
| 21 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 22 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 23 | + |
| 24 | +/** |
| 25 | + * Test for {@link CachedUidGenerator} |
| 26 | + * |
| 27 | + * @author yutianbao |
| 28 | + * @author wujun |
| 29 | + */ |
| 30 | + |
| 31 | +@Disabled |
| 32 | +@ExtendWith(SpringExtension.class) |
| 33 | +@SpringBootTest |
| 34 | +public class CachedUidGeneratorTest { |
| 35 | + private static final int SIZE = 7000; //7000000 700w |
| 36 | + private static final boolean VERBOSE = false; |
| 37 | + private static final int THREADS = Runtime.getRuntime().availableProcessors() << 1; |
| 38 | + |
| 39 | + @Autowired |
| 40 | + private UidGenerator cachedUidGenerator; |
| 41 | + |
| 42 | + /** |
| 43 | + * Test for serially generate |
| 44 | + * |
| 45 | + * @throws IOException |
| 46 | + */ |
| 47 | + @Test |
| 48 | + public void testSerialGenerate() throws IOException { |
| 49 | + // Generate UID serially |
| 50 | + Set<Long> uidSet = new HashSet<>(SIZE); |
| 51 | + for (int i = 0; i < SIZE; i++) { |
| 52 | + doGenerate(uidSet, i); |
| 53 | + } |
| 54 | + |
| 55 | + // Check UIDs are all unique |
| 56 | + checkUniqueID(uidSet); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Test for parallel generate |
| 61 | + * |
| 62 | + * @throws InterruptedException |
| 63 | + * @throws IOException |
| 64 | + */ |
| 65 | + @Test |
| 66 | + public void testParallelGenerate() throws InterruptedException, IOException { |
| 67 | + AtomicInteger control = new AtomicInteger(-1); |
| 68 | + Set<Long> uidSet = new ConcurrentSkipListSet<>(); |
| 69 | + |
| 70 | + // Initialize threads |
| 71 | + List<Thread> threadList = new ArrayList<>(THREADS); |
| 72 | + for (int i = 0; i < THREADS; i++) { |
| 73 | + Thread thread = new Thread(() -> workerRun(uidSet, control)); |
| 74 | + thread.setName("UID-generator-" + i); |
| 75 | + |
| 76 | + threadList.add(thread); |
| 77 | + thread.start(); |
| 78 | + } |
| 79 | + |
| 80 | + // Wait for worker done |
| 81 | + for (Thread thread : threadList) { |
| 82 | + thread.join(); |
| 83 | + } |
| 84 | + |
| 85 | + // Check generate 700w times |
| 86 | + assertEquals(SIZE, control.get()); |
| 87 | + |
| 88 | + // Check UIDs are all unique |
| 89 | + checkUniqueID(uidSet); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Woker run |
| 94 | + */ |
| 95 | + private void workerRun(Set<Long> uidSet, AtomicInteger control) { |
| 96 | + for (;;) { |
| 97 | + int myPosition = control.updateAndGet(old -> (old == SIZE ? SIZE : old + 1)); |
| 98 | + if (myPosition == SIZE) { |
| 99 | + return; |
| 100 | + } |
| 101 | + |
| 102 | + doGenerate(uidSet, myPosition); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Do generating |
| 108 | + */ |
| 109 | + private void doGenerate(Set<Long> uidSet, int index) { |
| 110 | + long uid = cachedUidGenerator.getUID().block() ; |
| 111 | + String parsedInfo = cachedUidGenerator.parseUID(uid); |
| 112 | + System.out.println("gen id: " + parsedInfo); |
| 113 | + boolean existed = !uidSet.add(uid); |
| 114 | + if (existed) { |
| 115 | + System.out.println("Found duplicate UID " + uid); |
| 116 | + } |
| 117 | + |
| 118 | + // Check UID is positive, and can be parsed |
| 119 | + assertTrue(uid > 0L); |
| 120 | + Assertions.assertTrue(StringUtils.isNotBlank(parsedInfo)); |
| 121 | + |
| 122 | + if (VERBOSE) { |
| 123 | + System.out.println(Thread.currentThread().getName() + " No." + index + " >>> " + parsedInfo); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Check UIDs are all unique |
| 129 | + */ |
| 130 | + private void checkUniqueID(Set<Long> uidSet) throws IOException { |
| 131 | + System.out.println(uidSet.size()); |
| 132 | + assertEquals(SIZE, uidSet.size()); |
| 133 | + } |
| 134 | + |
| 135 | +} |
0 commit comments