alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Scala example source code file (IOTest.scala)

This example Scala source code file (IOTest.scala) is included in the alvinalexander.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Scala by Example" TM.

Learn more about this Scala project at its project page.

Java - Scala tags/keywords

catchable, error, iotest, long, speclite, throwable

The IOTest.scala Scala example source code

package scalaz
package effect

import syntax.foldable._
import std.stream._

object IOTest extends SpecLite {
  "IO" should {
    // as reported in <https://groups.google.com/d/msg/scalaz/BIhItmdejeI/zZ-fSH7ZzfwJ>
    // fix in bb4ebd650
    "not stack overflow" in {
      var counter: Long = 0L
      val action: IO[Unit] = Stream.from(1).take(40000).traverse_(i => IO { counter += i })
      action.unsafePerformIO()
      counter must_== 800020000L
    }
  }

  "Catchable[IO]" should {

    val C = Catchable[IO]
    val err = new Error("oh noes")
    val bad = C.fail[Int](err)

    "throw exceptions captured via fail()" in {
      try {
        bad.unsafePerformIO
        fail("should have thrown")
      } catch {
        case t: Throwable => t must_== err
      }
    }

    "catch exceptions captured via fail()" in {
      C.attempt(bad).unsafePerformIO must_== -\/(err)
    }

    "catch ambient exceptions" in {
      C.attempt(IO(throw err)).unsafePerformIO must_== -\/(err)
    }

    "properly handle success" in {
      C.attempt(IO(3)).unsafePerformIO must_== \/-(3)
    }

  }

}

// vim: expandtab:ts=2:sw=2

Other Scala examples (source code examples)

Here is a short list of links related to this Scala IOTest.scala source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.