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

Akka/Scala example source code file (IndexSpec.scala)

This example Akka source code file (IndexSpec.scala) is included in my "Source Code Warehouse" project. The intent of this project is to help you more easily find Akka and Scala source code examples by using tags.

All credit for the original source code belongs to akka.io; I'm just trying to make examples easier to find. (For my Scala work, see my Scala examples and tutorials.)

Akka tags/keywords

akka, akkaspec, an, concurrent, defaulttimeout, future, index, indexspec, int, matchers, none, some, test, testing, testkit, utilities

The IndexSpec.scala Akka example source code

/**
 * Copyright (C) 2009-2014 Typesafe Inc. <http://www.typesafe.com>
 */
package akka.util

import org.scalatest.Matchers
import scala.concurrent.Future
import akka.testkit.AkkaSpec
import scala.concurrent.Await
import scala.util.Random
import akka.testkit.DefaultTimeout

class IndexSpec extends AkkaSpec with Matchers with DefaultTimeout {
  implicit val ec = system.dispatcher
  private def emptyIndex = new Index[String, Int](100, _ compareTo _)

  private def indexWithValues = {
    val index = emptyIndex
    index.put("s1", 1)
    index.put("s1", 2)
    index.put("s1", 3)
    index.put("s2", 1)
    index.put("s2", 2)
    index.put("s3", 2)

    index
  }

  "An Index" must {

    "take and return a value" in {
      val index = emptyIndex
      index.put("s1", 1)
      index.valueIterator("s1").toSet should be(Set(1))
    }
    "take and return several values" in {
      val index = emptyIndex
      index.put("s1", 1) should be(true)
      index.put("s1", 1) should be(false)
      index.put("s1", 2)
      index.put("s1", 3)
      index.put("s2", 4)
      index.valueIterator("s1").toSet should be(Set(1, 2, 3))
      index.valueIterator("s2").toSet should be(Set(4))
    }
    "remove values" in {
      val index = emptyIndex
      index.put("s1", 1)
      index.put("s1", 2)
      index.put("s2", 1)
      index.put("s2", 2)
      //Remove value
      index.remove("s1", 1) should be(true)
      index.remove("s1", 1) should be(false)
      index.valueIterator("s1").toSet should be(Set(2))
      //Remove key
      index.remove("s2") match {
        case Some(iter) ⇒ iter.toSet should be(Set(1, 2))
        case None       ⇒ fail()
      }
      index.remove("s2") should be(None)
      index.valueIterator("s2").toSet should be(Set())
    }
    "remove the specified value" in {
      val index = emptyIndex
      index.put("s1", 1)
      index.put("s1", 2)
      index.put("s1", 3)
      index.put("s2", 1)
      index.put("s2", 2)
      index.put("s3", 2)

      index.removeValue(1)
      index.valueIterator("s1").toSet should be(Set(2, 3))
      index.valueIterator("s2").toSet should be(Set(2))
      index.valueIterator("s3").toSet should be(Set(2))
    }
    "apply a function for all key-value pairs and find every value" in {
      val index = indexWithValues

      var valueCount = 0
      index.foreach((key, value) ⇒ {
        valueCount = valueCount + 1
        index.findValue(key)(_ == value) should be(Some(value))
      })
      valueCount should be(6)
    }
    "be cleared" in {
      val index = indexWithValues
      index.isEmpty should be(false)
      index.clear()
      index.isEmpty should be(true)
    }
    "be able to be accessed in parallel" in {
      val index = new Index[Int, Int](100, _ compareTo _)
      val nrOfTasks = 10000
      val nrOfKeys = 10
      val nrOfValues = 10
      //Fill index
      for (key ← 0 until nrOfKeys; value ← 0 until nrOfValues)
        index.put(key, value)
      //Tasks to be executed in parallel
      def putTask() = Future {
        index.put(Random.nextInt(nrOfKeys), Random.nextInt(nrOfValues))
      }
      def removeTask1() = Future {
        index.remove(Random.nextInt(nrOfKeys / 2), Random.nextInt(nrOfValues))
      }
      def removeTask2() = Future {
        index.remove(Random.nextInt(nrOfKeys / 2))
      }
      def readTask() = Future {
        val key = Random.nextInt(nrOfKeys)
        val values = index.valueIterator(key)
        if (key >= nrOfKeys / 2) {
          values.isEmpty should be(false)
        }
      }

      def executeRandomTask() = Random.nextInt(4) match {
        case 0 ⇒ putTask()
        case 1 ⇒ removeTask1()
        case 2 ⇒ removeTask2()
        case 3 ⇒ readTask()
      }

      val tasks = List.fill(nrOfTasks)(executeRandomTask)

      tasks.foreach(Await.result(_, timeout.duration))
    }
  }
}

Other Akka source code examples

Here is a short list of links related to this Akka IndexSpec.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.