package com.qmth.boot.test.tools.uuid;

import com.qmth.boot.tools.uuid.FastUUID;

import java.util.HashSet;
import java.util.Queue;
import java.util.Set;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class FastUUIDTest {

    //@Test
    public void test() throws InterruptedException {
        final Queue<String> queue = new ConcurrentLinkedQueue<>();
        ExecutorService executor = Executors.newFixedThreadPool(4);
        for (int i = 0; i < 4; i++) {
            executor.submit(() -> {
                for (int x = 0; x < 1000000; x++) {
                    String id = FastUUID.get();
                    queue.add(id);
                }
            });
        }
        executor.shutdown();
        executor.awaitTermination(10000, TimeUnit.SECONDS);
        int conflict = 0;
        Set<String> set = new HashSet<>();
        for (String id : queue) {
            if (set.contains(id)) {
                conflict++;
            } else {
                set.add(id);
            }
        }
        System.out.println("queue size: " + queue.size());
        System.out.println("valid size: " + set.size());
        System.out.println("conflict: " + conflict);
    }
}
