Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tag: heroku-devcent…
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 102 lines (89 sloc) 3.275 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
package com.typesafe.webwords.common

import org.scalatest.matchers._
import org.scalatest._
import akka.actor._
import akka.actor.Actor.actorOf
import java.net.URL

class IndexStorageActorSpec extends FlatSpec with ShouldMatchers {
    private val sampleIndex = Index(
        links = Seq(
            "dogs" -> "http://dogs.com/",
            "cats" -> "http://cats.com/"),
        wordCounts = Seq(
            "hello" -> 10,
            "world" -> 5,
            "quick" -> 4,
            "brown" -> 3))
    private val anotherIndex = Index(
        links = Seq(
            "pigs" -> "http://pigs.com/",
            "cows" -> "http://cows.com/"),
        wordCounts = Seq(
            "hello" -> 7,
            "world" -> 1,
            "quick" -> 4,
            "brown" -> 2))
    private val emptyIndex = Index(Nil, Nil)
    private val exampleUrl = new URL("http://example.com/")
    private val exampleUrl2 = new URL("http://example2.com/")

    private def newActor = actorOf(new IndexStorageActor(Some("mongodb://localhost/webwordstest"))).start

    behavior of "IndexStorageActor"

    private def cacheIndex(storage: ActorRef, url: URL, index: Index) = {
        storage ! CacheIndex(url.toExternalForm, index)
    }

    private def fetchIndex(storage: ActorRef, url: URL): Index = {
        (storage ? FetchCachedIndex(url.toExternalForm)).get match {
            case CachedIndexFetched(Some(index)) =>
                index
            case whatever =>
                throw new Exception("failed to get index, got: " + whatever)
        }
    }

    private def cacheSize(storage: ActorRef): Long = {
        (storage ? GetCacheSize).get match {
            case CacheSize(x) => x
            case whatever =>
                throw new Exception("failed to get cache size, got: " + whatever)
        }
    }

    it should "drop the cache in case of leftovers" in {
        val storage = newActor
        storage ! DropCache
        cacheSize(storage) should be(0)
        storage.stop
    }

    it should "store and retrieve an index" in {
        val storage = newActor
        cacheIndex(storage, exampleUrl, sampleIndex)
        val fetched = fetchIndex(storage, exampleUrl)
        fetched should be(sampleIndex)
        storage.stop
    }

    it should "store and retrieve an empty index" in {
        val storage = newActor
        cacheIndex(storage, exampleUrl2, emptyIndex)
        val fetched = fetchIndex(storage, exampleUrl2)
        fetched should be(emptyIndex)
        storage.stop
    }

    it should "use the newest entry" in {
        val storage = newActor
        // check we have leftovers from previous test
        val fetched = fetchIndex(storage, exampleUrl)
        fetched should be(sampleIndex)
        // now replace the leftovers
        cacheIndex(storage, exampleUrl, anotherIndex)
        val newIndex = fetchIndex(storage, exampleUrl)
        newIndex should be(anotherIndex)
        storage.stop
    }

    it should "drop the cache" in {
        val storage = newActor
        // check we have leftovers from a previous test
        val fetched = fetchIndex(storage, exampleUrl)
        fetched should be(anotherIndex)
        storage ! DropCache
        cacheSize(storage) should be(0)
        storage.stop
    }
}
Something went wrong with that request. Please try again.