package com.red.circle.game.fruit.core.memory;

import java.util.ArrayList;
import java.util.List;
import org.junit.Assert;
import org.junit.Test;

/**
 * @author pengliang on 2023/12/6
 */
public class TypeCounterTest {

  @Test
  public void test_increment_result_eq_10000() throws InterruptedException {
    TypeCounter typeCounter = new TypeCounter();
    Runnable incrementTask = () -> {
      for (int i = 0; i < 1000; i++) {
        typeCounter.increment("TypeA", 1);
        typeCounter.increment("TypeB", 1);
      }
    };

    List<Thread> threads = new ArrayList<>();
    for (int index = 0; index < 10; index++) {
      Thread thread = new Thread(incrementTask);
      thread.setName("thread-" + index);
      threads.add(thread);
    }

    for (Thread thread : threads) {
      thread.start();
    }

    for (Thread thread : threads) {
      thread.join();
    }

    Assert.assertEquals(typeCounter.getCount("TypeA"), 10000);
    Assert.assertEquals(typeCounter.getCount("TypeB"), 10000);
  }
}
