<commit_before>package org.threadly.concurrent.future;

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

import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.threadly.concurrent.PriorityScheduledExecutor;
import org.threadly.concurrent.StrictPriorityScheduledExecutor;
import org.threadly.test.concurrent.TestRunnable;

@SuppressWarnings("javadoc")
public class ListenableFutureResultTest {
  private ListenableFutureResult<String> lfr;
  
  @Before
  public void setup() {
    lfr = new ListenableFutureResult<String>();
  }
  
  @After
  public void tearDown() {
    lfr = null;
  }
  
  @Test (expected = IllegalStateException.class)
  public void setResultResultFail() {
    lfr.setResult(null);
    lfr.setResult(null);
  }
  
  @Test (expected = IllegalStateException.class)
  public void setFailureResultFail() {
    lfr.setFailure(null);
    lfr.setResult(null);
  }
  
  @Test (expected = IllegalStateException.class)
  public void setResultFailureFail() {
    lfr.setResult(null);
    lfr.setFailure(null);
  }
  
  @Test (expected = IllegalStateException.class)
  public void setFailureFailureFail() {
    lfr.setFailure(null);
    lfr.setFailure(null);
  }
  
  @Test
  public void listenersCalledOnResultTest() {
    TestRunnable tr = new TestRunnable();
    lfr.addListener(tr);
    
    lfr.setResult(null);
    
    assertTrue(tr.ranOnce());
    
    // verify new additions also get called
    tr = new TestRunnable();
    lfr.addListener(tr);
    assertTrue(tr.ranOnce());
  }
  
  @Test
  public void listenersCalledOnFailureTest() {
    TestRunnable tr = new TestRunnable();
    lfr.addListener(tr);
    
    lfr.setFailure(null);
    
    assertTrue(tr.ranOnce());
    
    // verify new additions also get called
    tr = new TestRunnable();
    lfr.addListener(tr);
    assertTrue(tr.ranOnce());
  }
  
  @Test
  public void cancelTest() {
    assertFalse(lfr.cancel(false));
    assertFalse(lfr.cancel(true));
    assertFalse(lfr.isCancelled());
    assertFalse(lfr.isDone());
  }
  
  @Test
  public void isDoneTest() {
    assertFalse(lfr.isDone());
    
    lfr.setResult(null);

    assertTrue(lfr.isDone());
  }
  
  @Test
  public void getResultTest() throws InterruptedException, ExecutionException {
    final String testResult = "getResultTest";
    
    PriorityScheduledExecutor scheduler = new StrictPriorityScheduledExecutor(1, 1, 100);
    try {
      scheduler.schedule(new Runnable() {
        @Override
        public void run() {
          lfr.setResult(testResult);
        }
      }, SCHEDULE_DELAY);
      
      assertTrue(lfr.get() == testResult);
    } finally {
      scheduler.shutdownNow();
    }
  }
  
  @Test
  public void getWithTimeoutResultTest() throws InterruptedException, 
                                                ExecutionException, 
                                                TimeoutException {
    final String testResult = "getWithTimeoutResultTest";
    
    PriorityScheduledExecutor scheduler = new StrictPriorityScheduledExecutor(1, 1, 100);
    try {
      scheduler.schedule(new Runnable() {
        @Override
        public void run() {
          lfr.setResult(testResult);
        }
      }, SCHEDULE_DELAY);
      
      assertTrue(lfr.get(SCHEDULE_DELAY * 4, TimeUnit.MILLISECONDS) == testResult);
    } finally {
      scheduler.shutdownNow();
    }
  }

  @Test
  public void getTimeoutTest() throws InterruptedException, 
                                      ExecutionException {
    long startTime = System.currentTimeMillis();
    try {
      lfr.get(DELAY_TIME, TimeUnit.MILLISECONDS);
      fail("Exception should have thrown");
    } catch (TimeoutException e) {
      // expected
    }<commit_msg>Attempt to fix a unit test which fails on slow systems (hopefully due to slow to start threads)<commit_after>package org.threadly.concurrent.future;

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

import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.threadly.concurrent.PriorityScheduledExecutor;
import org.threadly.concurrent.StrictPriorityScheduledExecutor;
import org.threadly.test.concurrent.TestRunnable;

@SuppressWarnings("javadoc")
public class ListenableFutureResultTest {
  private ListenableFutureResult<String> lfr;
  
  @Before
  public void setup() {
    lfr = new ListenableFutureResult<String>();
  }
  
  @After
  public void tearDown() {
    lfr = null;
  }
  
  @Test (expected = IllegalStateException.class)
  public void setResultResultFail() {
    lfr.setResult(null);
    lfr.setResult(null);
  }
  
  @Test (expected = IllegalStateException.class)
  public void setFailureResultFail() {
    lfr.setFailure(null);
    lfr.setResult(null);
  }
  
  @Test (expected = IllegalStateException.class)
  public void setResultFailureFail() {
    lfr.setResult(null);
    lfr.setFailure(null);
  }
  
  @Test (expected = IllegalStateException.class)
  public void setFailureFailureFail() {
    lfr.setFailure(null);
    lfr.setFailure(null);
  }
  
  @Test
  public void listenersCalledOnResultTest() {
    TestRunnable tr = new TestRunnable();
    lfr.addListener(tr);
    
    lfr.setResult(null);
    
    assertTrue(tr.ranOnce());
    
    // verify new additions also get called
    tr = new TestRunnable();
    lfr.addListener(tr);
    assertTrue(tr.ranOnce());
  }
  
  @Test
  public void listenersCalledOnFailureTest() {
    TestRunnable tr = new TestRunnable();
    lfr.addListener(tr);
    
    lfr.setFailure(null);
    
    assertTrue(tr.ranOnce());
    
    // verify new additions also get called
    tr = new TestRunnable();
    lfr.addListener(tr);
    assertTrue(tr.ranOnce());
  }
  
  @Test
  public void cancelTest() {
    assertFalse(lfr.cancel(false));
    assertFalse(lfr.cancel(true));
    assertFalse(lfr.isCancelled());
    assertFalse(lfr.isDone());
  }
  
  @Test
  public void isDoneTest() {
    assertFalse(lfr.isDone());
    
    lfr.setResult(null);

    assertTrue(lfr.isDone());
  }
  
  @Test
  public void getResultTest() throws InterruptedException, ExecutionException {
    final String testResult = "getResultTest";
    
    PriorityScheduledExecutor scheduler = new StrictPriorityScheduledExecutor(1, 1, 100);
    try {
      scheduler.schedule(new Runnable() {
        @Override
        public void run() {
          lfr.setResult(testResult);
        }
      }, SCHEDULE_DELAY);
      
      assertTrue(lfr.get() == testResult);
    } finally {
      scheduler.shutdownNow();
    }
  }
  
  @Test
  public void getWithTimeoutResultTest() throws InterruptedException, 
                                                ExecutionException, 
                                                TimeoutException {
    final String testResult = "getWithTimeoutResultTest";
    
    PriorityScheduledExecutor scheduler = new StrictPriorityScheduledExecutor(1, 1, 100);
    try {
      scheduler.prestartAllCoreThreads();
      scheduler.schedule(new Runnable() {
        @Override
        public void run() {
          lfr.setResult(testResult);
        }
      }, SCHEDULE_DELAY);
      
      assertTrue(lfr.get(SCHEDULE_DELAY * 5, TimeUnit.MILLISECONDS) == testResult);
    } finally {
      scheduler.shutdownNow();
    }
  }

  @Test
  public void getTimeoutTest() throws InterruptedException, 
                                      ExecutionException {
    long startTime = System.currentTimeMillis();
    try {
      lfr.get(DELAY_TIME, TimeUnit.MILLISECONDS);
      fail("Exception should have thrown");
    } catch (TimeoutException e) {
      // expected
    }
{'fraction_non_alphanumeric': 0.10382375284882249, 'fraction_numerical': 0.002785515320334262, 'mean_word_length': 2.8758586849852796, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '5328128', 'n_tokens_mistral': 2397, 'n_tokens_neox': 2363, 'n_words': 460}