<commit_before>package org.threadly.concurrent;

import static org.junit.Assert.*;
import static org.threadly.TestConstants.*;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeoutException;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.threadly.concurrent.NoThreadScheduler;
import org.threadly.concurrent.future.ListenableFuture;
import org.threadly.test.concurrent.AsyncVerifier;
import org.threadly.test.concurrent.TestRunnable;

@SuppressWarnings("javadoc")
public class NoThreadSchedulerTest {
  private NoThreadScheduler blockingScheduler;
  private NoThreadScheduler nonblockingScheduler;
  
  @Before
  public void setup() {
    blockingScheduler = new NoThreadScheduler(true);
    nonblockingScheduler = new NoThreadScheduler(false);
  }
  
  @After
  public void tearDown() {
    blockingScheduler = null;
    nonblockingScheduler = null;
  }
  
  private static List<TestRunnable> getRunnableList() {
    List<TestRunnable> result = new ArrayList<TestRunnable>(TEST_QTY);
    for (int i = 0; i < TEST_QTY; i++) {
      result.add(new TestRunnable());
    }
    
    return result;
  }
  
  private static List<TestCallable> getCallableList() {
    List<TestCallable> result = new ArrayList<TestCallable>(TEST_QTY);
    for (int i = 0; i < TEST_QTY; i++) {
      result.add(new TestCallable());
    }
    
    return result;
  }
  
  @Test
  public void isShutdownTest() {
    assertFalse(blockingScheduler.isShutdown());
    assertFalse(nonblockingScheduler.isShutdown());
  }
  
  @Test
  public void executeTest() throws InterruptedException {
    executeTest(blockingScheduler);
    executeTest(nonblockingScheduler);
  }
  
  private void executeTest(NoThreadScheduler scheduler) throws InterruptedException {
    List<TestRunnable> runnables = getRunnableList();
    Iterator<TestRunnable> it = runnables.iterator();
    while (it.hasNext()) {
      scheduler.execute(it.next());
    }
    
    // all should run now
    assertEquals(TEST_QTY, scheduler.tick());
    
    it = runnables.iterator();
    while (it.hasNext()) {
      assertEquals(1, it.next().getRunCount());
    }
    
    if (scheduler == nonblockingScheduler) {
      // verify no more run after a second tick
      assertEquals(scheduler.tick(), 0);
      
      it = runnables.iterator();
      while (it.hasNext()) {
        assertEquals(1, it.next().getRunCount());
      }
    }
  }
  
  @Test
  public void submitRunnableTest() throws InterruptedException {
    submitRunnableTest(blockingScheduler);
    submitRunnableTest(nonblockingScheduler);
  }
  
  private void submitRunnableTest(NoThreadScheduler scheduler) throws InterruptedException {
    List<TestRunnable> runnables = getRunnableList();
    List<Future<?>> futures = new ArrayList<Future<?>>(runnables.size());
    Iterator<TestRunnable> it = runnables.iterator();
    while (it.hasNext()) {
      Future<?> future = scheduler.submit(it.next());
      assertNotNull(future);
      futures.add(future);
    }
    
    // all should run now
    assertEquals(TEST_QTY, scheduler.tick());
    
    it = runnables.iterator();
    while (it.hasNext()) {
      assertEquals(1, it.next().getRunCount());
    }
    
    if (scheduler == nonblockingScheduler) {
      // verify no more run after a second tick
      assertEquals(0, scheduler.tick());
      
      it = runnables.iterator();
      while (it.hasNext()) {
        assertEquals(1, it.next().getRunCount());
      }
    }
    
    Iterator<Future<?>> futureIt = futures.iterator();
    while (futureIt.hasNext()) {
      assertTrue(futureIt.next().isDone());
    }
  }
  
  @Test
  public void submitCallableTest() throws InterruptedException, 
                                          ExecutionException {
    submitCallableTest(blockingScheduler);
    submitCallableTest(nonblockingScheduler);
  }
  
  private static void submitCallableTest(NoThreadScheduler scheduler) throws InterruptedException, 
                                                                             ExecutionException {
    List<TestCallable> callables = getCallableList();
    List<Future<Object>> futures = new ArrayList<Future<Object>>(callables.size());
    Iterator<TestCallable> it = callables.iterator();
    while (it.hasNext()) {
      Future<Object> future = scheduler.submit(it.next());
      assertNotNull(future);
      futures.add(future);
    }
    
    // all should run now
    assertEquals(TEST_QTY, scheduler.tick());
    
    it = callables.iterator();
    while (it.hasNext()) {
      assertTrue(it.next().isDone());
    }

    it = callables.iterator();
    Iterator<Future<Object>> futureIt = futures.iterator();
    while (futureIt.hasNext()) {
      Future<Object> future = futureIt.next();
      TestCallable tc = it.next();
      
      assertTrue(future.isDone());
      assertTrue(tc.getReturnedResult() == future.get());
    }
  }
  
  @Test
  public void scheduleRunnableTest() throws InterruptedException {
    scheduleRunnableTest(blockingScheduler);
    scheduleRunnableTest(nonblockingScheduler);
  }
  
  private static void scheduleRunnableTest(NoThreadScheduler scheduler) throws InterruptedException {
    TestRunnable tr = new TestRunnable();
    scheduler.schedule(tr, SCHEDULE_DELAY);
    long scheduleTime = System.currentTimeMillis();
    
    int runCount = 0;
    while (runCount == 0) {
      runCount = scheduler.tick();
    }
    long runTime = System.currentTimeMillis();
    
    assertEquals(1, runCount);
    
    assertTrue(tr.ranOnce());
    assertTrue((runTime - scheduleTime) >= SCHEDULE_DELAY);
  }
  
  @Test
  public void submitScheduledRunnableTest() throws InterruptedException {
    submitScheduledRunnableTest(blockingScheduler);
    submitScheduledRunnableTest(nonblockingScheduler);
  }
  
  private static void submitScheduledRunnableTest(NoThreadScheduler scheduler) throws InterruptedException {
    TestRunnable tr = new TestRunnable();
    ListenableFuture<?> future = scheduler.submitScheduled(tr, SCHEDULE_DELAY);
    
    int runCount = 0;
    while (runCount == 0) {
      runCount = scheduler.tick();
    }<commit_msg>We need to record the schedule time in this test before we schedule it<commit_after>package org.threadly.concurrent;

import static org.junit.Assert.*;
import static org.threadly.TestConstants.*;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeoutException;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.threadly.concurrent.NoThreadScheduler;
import org.threadly.concurrent.future.ListenableFuture;
import org.threadly.test.concurrent.AsyncVerifier;
import org.threadly.test.concurrent.TestRunnable;

@SuppressWarnings("javadoc")
public class NoThreadSchedulerTest {
  private NoThreadScheduler blockingScheduler;
  private NoThreadScheduler nonblockingScheduler;
  
  @Before
  public void setup() {
    blockingScheduler = new NoThreadScheduler(true);
    nonblockingScheduler = new NoThreadScheduler(false);
  }
  
  @After
  public void tearDown() {
    blockingScheduler = null;
    nonblockingScheduler = null;
  }
  
  private static List<TestRunnable> getRunnableList() {
    List<TestRunnable> result = new ArrayList<TestRunnable>(TEST_QTY);
    for (int i = 0; i < TEST_QTY; i++) {
      result.add(new TestRunnable());
    }
    
    return result;
  }
  
  private static List<TestCallable> getCallableList() {
    List<TestCallable> result = new ArrayList<TestCallable>(TEST_QTY);
    for (int i = 0; i < TEST_QTY; i++) {
      result.add(new TestCallable());
    }
    
    return result;
  }
  
  @Test
  public void isShutdownTest() {
    assertFalse(blockingScheduler.isShutdown());
    assertFalse(nonblockingScheduler.isShutdown());
  }
  
  @Test
  public void executeTest() throws InterruptedException {
    executeTest(blockingScheduler);
    executeTest(nonblockingScheduler);
  }
  
  private void executeTest(NoThreadScheduler scheduler) throws InterruptedException {
    List<TestRunnable> runnables = getRunnableList();
    Iterator<TestRunnable> it = runnables.iterator();
    while (it.hasNext()) {
      scheduler.execute(it.next());
    }
    
    // all should run now
    assertEquals(TEST_QTY, scheduler.tick());
    
    it = runnables.iterator();
    while (it.hasNext()) {
      assertEquals(1, it.next().getRunCount());
    }
    
    if (scheduler == nonblockingScheduler) {
      // verify no more run after a second tick
      assertEquals(scheduler.tick(), 0);
      
      it = runnables.iterator();
      while (it.hasNext()) {
        assertEquals(1, it.next().getRunCount());
      }
    }
  }
  
  @Test
  public void submitRunnableTest() throws InterruptedException {
    submitRunnableTest(blockingScheduler);
    submitRunnableTest(nonblockingScheduler);
  }
  
  private void submitRunnableTest(NoThreadScheduler scheduler) throws InterruptedException {
    List<TestRunnable> runnables = getRunnableList();
    List<Future<?>> futures = new ArrayList<Future<?>>(runnables.size());
    Iterator<TestRunnable> it = runnables.iterator();
    while (it.hasNext()) {
      Future<?> future = scheduler.submit(it.next());
      assertNotNull(future);
      futures.add(future);
    }
    
    // all should run now
    assertEquals(TEST_QTY, scheduler.tick());
    
    it = runnables.iterator();
    while (it.hasNext()) {
      assertEquals(1, it.next().getRunCount());
    }
    
    if (scheduler == nonblockingScheduler) {
      // verify no more run after a second tick
      assertEquals(0, scheduler.tick());
      
      it = runnables.iterator();
      while (it.hasNext()) {
        assertEquals(1, it.next().getRunCount());
      }
    }
    
    Iterator<Future<?>> futureIt = futures.iterator();
    while (futureIt.hasNext()) {
      assertTrue(futureIt.next().isDone());
    }
  }
  
  @Test
  public void submitCallableTest() throws InterruptedException, 
                                          ExecutionException {
    submitCallableTest(blockingScheduler);
    submitCallableTest(nonblockingScheduler);
  }
  
  private static void submitCallableTest(NoThreadScheduler scheduler) throws InterruptedException, 
                                                                             ExecutionException {
    List<TestCallable> callables = getCallableList();
    List<Future<Object>> futures = new ArrayList<Future<Object>>(callables.size());
    Iterator<TestCallable> it = callables.iterator();
    while (it.hasNext()) {
      Future<Object> future = scheduler.submit(it.next());
      assertNotNull(future);
      futures.add(future);
    }
    
    // all should run now
    assertEquals(TEST_QTY, scheduler.tick());
    
    it = callables.iterator();
    while (it.hasNext()) {
      assertTrue(it.next().isDone());
    }

    it = callables.iterator();
    Iterator<Future<Object>> futureIt = futures.iterator();
    while (futureIt.hasNext()) {
      Future<Object> future = futureIt.next();
      TestCallable tc = it.next();
      
      assertTrue(future.isDone());
      assertTrue(tc.getReturnedResult() == future.get());
    }
  }
  
  @Test
  public void scheduleRunnableTest() throws InterruptedException {
    scheduleRunnableTest(blockingScheduler);
    scheduleRunnableTest(nonblockingScheduler);
  }
  
  private static void scheduleRunnableTest(NoThreadScheduler scheduler) throws InterruptedException {
    TestRunnable tr = new TestRunnable();
    long scheduleTime = System.currentTimeMillis();
    scheduler.schedule(tr, SCHEDULE_DELAY);
    
    int runCount = 0;
    while (runCount == 0) {
      runCount = scheduler.tick();
    }
    long runTime = System.currentTimeMillis();
    
    assertEquals(1, runCount);
    
    assertTrue(tr.ranOnce());
    assertTrue((runTime - scheduleTime) >= SCHEDULE_DELAY);
  }
  
  @Test
  public void submitScheduledRunnableTest() throws InterruptedException {
    submitScheduledRunnableTest(blockingScheduler);
    submitScheduledRunnableTest(nonblockingScheduler);
  }
  
  private static void submitScheduledRunnableTest(NoThreadScheduler scheduler) throws InterruptedException {
    TestRunnable tr = new TestRunnable();
    ListenableFuture<?> future = scheduler.submitScheduled(tr, SCHEDULE_DELAY);
    
    int runCount = 0;
    while (runCount == 0) {
      runCount = scheduler.tick();
    }
{'fraction_non_alphanumeric': 0.11148835373075405, 'fraction_numerical': 0.0020529016975917884, 'mean_word_length': 3.352577319587629, 'pattern_counts': {'":': 0, '<': 63, '<?xml version=': 0, '>': 61, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2014524', 'n_tokens_mistral': 3766, 'n_tokens_neox': 3672, 'n_words': 739}