<commit_before>package net.liftweb.actor

import net.liftweb.common.{Failure, Box}
import org.specs2.mutable.Specification

class LAFutureSpec extends Specification {
  val timeout = 5000L

  "LAFuture" should {
    "map to failing future if transforming function throws an Exception" in {
      val future = LAFuture { () =>
        1
      }
      def tranformThrowingException(input: Int) = {
        throw new Exception("fail")
      }

      val transformedFuture = future.map(tranformThrowingException)

      var notifiedAboutFailure: Boolean = false
      transformedFuture.onFail { _ =>
        notifiedAboutFailure = true
      }

      transformedFuture.get(timeout)
      notifiedAboutFailure shouldEqual true
    }

    "flatMap to failing future if transforming function throws an Exception" in {
      val future = LAFuture { () =>
        1
      }
      def tranformThrowingException(input: Int): LAFuture[Int] = {
        throw new Exception("fail")
      }

      val transformedFuture = future.flatMap(tranformThrowingException)

      var notifiedAboutFailure: Boolean = false
      transformedFuture.onFail { _ =>
        notifiedAboutFailure = true
      }

      transformedFuture.get(timeout)
      notifiedAboutFailure shouldEqual true
    }

    "return original Failure after timeout" in {
      val future = new LAFuture()
      val givenFailure = Failure("fooFailure")
      LAScheduler.execute { () =><commit_msg>Execute map/flatMap LAFuture tests on same thread for reliability.<commit_after>package net.liftweb.actor

import net.liftweb.common.{Failure, Box}
import org.specs2.mutable.Specification
import java.util.concurrent.atomic.AtomicBoolean

class LAFutureSpec extends Specification {
  sequential
  val timeout = 20000L
  LAScheduler

  "LAFuture" should {
    val futureSpecScheduler = new LAScheduler {
      override def execute(f: ()=>Unit): Unit = f()
    }

    "map to failing future if transforming function throws an Exception" in {
      val future = LAFuture(() => 1, futureSpecScheduler)
      def tranformThrowingException(input: Int) = {
        throw new Exception("fail")
      }

      val transformedFuture = future.map(tranformThrowingException)

      var notifiedAboutFailure: Boolean = false
      transformedFuture.onFail { _ =>
        notifiedAboutFailure = true
      }

      transformedFuture.get(timeout)
      notifiedAboutFailure shouldEqual true
    }

    "flatMap to failing future if transforming function throws an Exception" in {
      val future = LAFuture(() => 1, futureSpecScheduler)
      def tranformThrowingException(input: Int): LAFuture[Int] = {
        throw new Exception("fail")
      }

      val transformedFuture = future.flatMap(tranformThrowingException)

      var notifiedAboutFailure: Boolean = false
      transformedFuture.onFail { _ =>
        notifiedAboutFailure = true
      }

      transformedFuture.get(timeout)
      notifiedAboutFailure shouldEqual true
    }

    "return original Failure after timeout" in {
      val future = new LAFuture()
      val givenFailure = Failure("fooFailure")
      LAScheduler.execute { () =>
{'fraction_non_alphanumeric': 0.07788062559846792, 'fraction_numerical': 0.0047877433769549956, 'mean_word_length': 3.0282776349614395, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 14, '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': '7319802', 'n_tokens_mistral': 869, 'n_tokens_neox': 820, 'n_words': 226}