Package

com.thoughtworks

raii

Permalink

package raii

Visibility
  1. Public
  2. All

Type Members

  1. trait AsynchronousSemaphore extends AnyRef

    Permalink

    Author:

    杨博 (Yang Bo) <[email protected]>

Value Members

  1. object AsynchronousSemaphore

    Permalink
  2. object asynchronous

    Permalink

    The namespace that contains Do.

    The namespace that contains Do.

    Author:

    杨博 (Yang Bo) <[email protected]>

  3. object asynchronouspool

    Permalink

    The namespace that contains the implementation of the asynchronous resource pool.

    The namespace that contains the implementation of the asynchronous resource pool.

    Example:
    1. Given a factory that creates resources,

      import scalaz.Tags.Parallel
      import scalaz._
      import scalaz.syntax.tag._
      import scalaz.syntax.all._
      import com.thoughtworks.future._
      import com.thoughtworks.raii.asynchronous._
      import com.thoughtworks.raii.asynchronouspool._
      import java.lang.AutoCloseable
      trait MyResource extends AutoCloseable {
        def inUse(): Unit
      }
      val myResourceStub0 = stub[MyResource]
      println(s"myResourceStub0: ${myResourceStub0.isInstanceOf[MyResource]}")
      println(s"myResourceStub0: ${myResourceStub0.isInstanceOf[MyResource]}")
      val myResourceStub1 = stub[MyResource]
      val myResourceStub2 = stub[MyResource]
      val myResourceFactoryMock = mockFunction[MyResource]
      myResourceFactoryMock.expects().returns(myResourceStub0)
      myResourceFactoryMock.expects().returns(myResourceStub1)
      myResourceFactoryMock.expects().returns(myResourceStub2)

      then it can be converted to a resource pool, which holds some instances of MyResource.

      def w = myResourceFactoryMock()
      val myResourcePool: Do[Do[MyResource]] = pool(Do.autoCloseable(w), capacity = 3)

      When some clients are using the resource pool,

      def client(doMyResource: Do[MyResource], operationsPerClient: Int): Do[Unit] = {
        Do.nested[Unit](doMyResource.flatMap { myResource =>
          Do.execute {
              myResource.inUse
            }
            .replicateM_(operationsPerClient)
        })
      }
      def allClients(doMyResource: Do[MyResource], numberOfClients: Int, operationsPerClient: Int): ParallelDo[Unit] = {
        implicit def keepLastException = new Semigroup[Throwable] {
          override def append(f1: Throwable, f2: => Throwable) = f2
        }
        Applicative[ParallelDo].replicateM_(numberOfClients, Parallel(client(doMyResource, operationsPerClient)))
      }
      def usingPool(numberOfClients: Int, operationsPerClient: Int) = {
        myResourcePool.flatMap { doMyResource =>
          allClients(doMyResource, numberOfClients, operationsPerClient).unwrap
        }
      }

      then the operations from these clients should be distributed on those MyResources, and those MyResources should be closed after being used.

      usingPool(numberOfClients = 10, operationsPerClient = 10).run.map { _: Unit =>
        ((myResourceStub0.inUse _): () => Unit).verify().repeated(30 to 40)
        ((myResourceStub0.close _): () => Unit).verify().once()
        ((myResourceStub1.inUse _): () => Unit).verify().repeated(30 to 40)
        ((myResourceStub1.close _): () => Unit).verify().once()
        ((myResourceStub2.inUse _): () => Unit).verify().repeated(30 to 40)
        ((myResourceStub2.close _): () => Unit).verify().once()
        succeed
      }.toScalaFuture
  4. object covariant extends CovariantResourceTInstances0

    Permalink

    The namespace that contains the covariant ResourceT.

    The namespace that contains the covariant ResourceT.

    Usage:

    import com.thoughtworks.raii.covariant._
  5. object invariant extends InvariantResourceTInstances0

    Permalink

    The namespace that contains the invariant ResourceT.

    The namespace that contains the invariant ResourceT.

    Usage:

    import com.thoughtworks.raii.invariant._
  6. object shared

    Permalink

    Author:

    杨博 (Yang Bo) <[email protected]>

Ungrouped