<commit_before>package de.is24.util.monitoring;

import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import java.text.NumberFormat;
import java.util.Random;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;


public class ConcurrentcyTest {
  private static final NumberFormat NUMBER_FORMAT = NumberFormat.getInstance();

  static {
    NUMBER_FORMAT.setGroupingUsed(true);
    NUMBER_FORMAT.setMinimumFractionDigits(3);
    NUMBER_FORMAT.setMaximumFractionDigits(3);
  }

  private static final int THREADS = 10, RUNS = 50;

  private ThreadPoolExecutor executor;
  private Job observerJob, timerJob;
  private Random rnd;

  private AtomicLong timerSum;

  @BeforeClass
  public static void beforeClass() {
    InApplicationMonitor.resetInstanceForTesting();
  }

  @Before
  public void setUp() throws Exception {
    this.observerJob = new ObserverJob();
    this.timerJob = new TimerJob();
    this.rnd = new Random();
    this.timerSum = new AtomicLong();
  }

  @Test
  public void testConcurency() throws InterruptedException {
    createExecutor(10);

    executeJobs(timerJob, observerJob);

  }

  private void createExecutor(int threads) {
    this.executor = new ScheduledThreadPoolExecutor(threads);
    this.executor.setMaximumPoolSize(threads);
  }


  private void executeJobs(Runnable... jobs) throws InterruptedException {
    for (int i = 0; i < RUNS; i++) {
      for (Runnable job : jobs) {
        executor.execute(job);
      }
    }
    executor.shutdown();

    executor.awaitTermination(100, TimeUnit.SECONDS);
  }

  private abstract class Job implements Runnable {
    public void run() {
      doJob();
    }

    protected abstract void doJob();
  }


  private class ObserverJob extends Job {
    @Override
    protected void doJob() {
      try {
        InApplicationMonitor.getInstance().addReportableObserver(new ReportableObserver() {
            @Override
            public void addNewReportable(Reportable reportable) {
              try {
                Thread.sleep(1);
              } catch (InterruptedException e) {
                Thread.interrupted();
              }
            }
          });
      } catch (RuntimeException e) {
        e.printStackTrace();
      }
    }

  }

  private class TimerJob extends Job {
    @Override
    protected void doJob() {
      try {
        String key = "t" + rnd.nextInt(100000);
        InApplicationMonitor.getInstance().addTimerMeasurement(key, 1);
      } catch (RuntimeException e) {
        e.printStackTrace();
      }
    }

  }
}<commit_msg>Test repdoducably foud two issues that have now been fixed (CorePlugin reprtable abserver handling and InApplicationMonitor getInstance /initInstance duplicate JMX mean registration)<commit_after>package de.is24.util.monitoring;

import org.junit.Before;
import org.junit.Test;
import java.text.NumberFormat;
import java.util.Random;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;
import static org.junit.Assert.assertThat;
import static org.hamcrest.CoreMatchers.is;


public class ConcurrentcyTest {
  private static final NumberFormat NUMBER_FORMAT = NumberFormat.getInstance();

  static {
    NUMBER_FORMAT.setGroupingUsed(true);
    NUMBER_FORMAT.setMinimumFractionDigits(3);
    NUMBER_FORMAT.setMaximumFractionDigits(3);
  }

  private static final int THREADS = 10, RUNS = 10, LOOPS = 10;

  private ThreadPoolExecutor executor;
  private Job observerJob, timerJob;
  private Random rnd;

  private AtomicLong exceptions;

  @Before
  public void setUp() throws Exception {
    this.observerJob = new ObserverJob();
    this.timerJob = new TimerJob();
    this.rnd = new Random();
    this.exceptions = new AtomicLong();
  }

  @Test
  public void testConcurrency() throws InterruptedException {
    for (int i = 0; i < LOOPS; i++) {
      InApplicationMonitor.resetInstanceForTesting();
      createExecutor(THREADS);

      executeJobs(timerJob, observerJob);
    }
    assertThat(exceptions.get(), is(0L));

  }

  private void createExecutor(int threads) {
    this.executor = new ScheduledThreadPoolExecutor(threads);
    this.executor.setMaximumPoolSize(threads);
  }


  private void executeJobs(Runnable... jobs) throws InterruptedException {
    for (int i = 0; i < RUNS; i++) {
      for (Runnable job : jobs) {
        executor.execute(job);
      }
    }
    executor.shutdown();

    executor.awaitTermination(100, TimeUnit.SECONDS);
  }

  private abstract class Job implements Runnable {
    public void run() {
      try {
        doJob();
      } catch (Exception e) {
        exceptions.incrementAndGet();
      }
    }

    protected abstract void doJob();
  }


  private class ObserverJob extends Job {
    @Override
    protected void doJob() {
      InApplicationMonitor.getInstance().addReportableObserver(new ReportableObserver() {
          @Override
          public void addNewReportable(Reportable reportable) {
            try {
              Thread.sleep(1);
            } catch (InterruptedException e) {
              Thread.interrupted();
            }
          }
        });
    }

  }

  private class TimerJob extends Job {
    @Override
    protected void doJob() {
      String key = "t" + rnd.nextInt(100000);
      InApplicationMonitor.getInstance().addTimerMeasurement(key, 1);
    }

  }
}
{'fraction_non_alphanumeric': 0.09440371893438226, 'fraction_numerical': 0.008224566422313607, 'mean_word_length': 3.0832116788321167, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6200535', 'n_tokens_mistral': 1681, 'n_tokens_neox': 1561, 'n_words': 380}