Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 102 lines (89 sloc) 3.275 kB
546e4bb @havocp Add an IndexStorageActor to cache in mongodb
havocp authored
1 package com.typesafe.webwords.common
2
3 import org.scalatest.matchers._
4 import org.scalatest._
5 import akka.actor._
52a06b6 @havocp import actorOf rather than using Actor.actorOf
havocp authored
6 import akka.actor.Actor.actorOf
546e4bb @havocp Add an IndexStorageActor to cache in mongodb
havocp authored
7 import java.net.URL
8
9 class IndexStorageActorSpec extends FlatSpec with ShouldMatchers {
53043e3 @havocp Convert the Index class to a case class
havocp authored
10 private val sampleIndex = Index(
546e4bb @havocp Add an IndexStorageActor to cache in mongodb
havocp authored
11 links = Seq(
12 "dogs" -> "http://dogs.com/",
13 "cats" -> "http://cats.com/"),
14 wordCounts = Seq(
15 "hello" -> 10,
16 "world" -> 5,
17 "quick" -> 4,
18 "brown" -> 3))
53043e3 @havocp Convert the Index class to a case class
havocp authored
19 private val anotherIndex = Index(
546e4bb @havocp Add an IndexStorageActor to cache in mongodb
havocp authored
20 links = Seq(
21 "pigs" -> "http://pigs.com/",
22 "cows" -> "http://cows.com/"),
23 wordCounts = Seq(
24 "hello" -> 7,
25 "world" -> 1,
26 "quick" -> 4,
27 "brown" -> 2))
53043e3 @havocp Convert the Index class to a case class
havocp authored
28 private val emptyIndex = Index(Nil, Nil)
546e4bb @havocp Add an IndexStorageActor to cache in mongodb
havocp authored
29 private val exampleUrl = new URL("http://example.com/")
30 private val exampleUrl2 = new URL("http://example2.com/")
31
52a06b6 @havocp import actorOf rather than using Actor.actorOf
havocp authored
32 private def newActor = actorOf(new IndexStorageActor(Some("mongodb://localhost/webwordstest"))).start
546e4bb @havocp Add an IndexStorageActor to cache in mongodb
havocp authored
33
34 behavior of "IndexStorageActor"
35
36 private def cacheIndex(storage: ActorRef, url: URL, index: Index) = {
37 storage ! CacheIndex(url.toExternalForm, index)
38 }
39
40 private def fetchIndex(storage: ActorRef, url: URL): Index = {
41 (storage ? FetchCachedIndex(url.toExternalForm)).get match {
42 case CachedIndexFetched(Some(index)) =>
43 index
44 case whatever =>
45 throw new Exception("failed to get index, got: " + whatever)
46 }
47 }
48
49 private def cacheSize(storage: ActorRef): Long = {
50 (storage ? GetCacheSize).get match {
51 case CacheSize(x) => x
52 case whatever =>
53 throw new Exception("failed to get cache size, got: " + whatever)
54 }
55 }
56
57 it should "drop the cache in case of leftovers" in {
58 val storage = newActor
59 storage ! DropCache
60 cacheSize(storage) should be(0)
61 storage.stop
62 }
63
64 it should "store and retrieve an index" in {
65 val storage = newActor
66 cacheIndex(storage, exampleUrl, sampleIndex)
67 val fetched = fetchIndex(storage, exampleUrl)
68 fetched should be(sampleIndex)
69 storage.stop
70 }
71
72 it should "store and retrieve an empty index" in {
73 val storage = newActor
74 cacheIndex(storage, exampleUrl2, emptyIndex)
75 val fetched = fetchIndex(storage, exampleUrl2)
76 fetched should be(emptyIndex)
77 storage.stop
78 }
79
80 it should "use the newest entry" in {
81 val storage = newActor
82 // check we have leftovers from previous test
83 val fetched = fetchIndex(storage, exampleUrl)
84 fetched should be(sampleIndex)
85 // now replace the leftovers
86 cacheIndex(storage, exampleUrl, anotherIndex)
87 val newIndex = fetchIndex(storage, exampleUrl)
88 newIndex should be(anotherIndex)
89 storage.stop
90 }
91
92 it should "drop the cache" in {
93 val storage = newActor
94 // check we have leftovers from a previous test
95 val fetched = fetchIndex(storage, exampleUrl)
96 fetched should be(anotherIndex)
97 storage ! DropCache
98 cacheSize(storage) should be(0)
99 storage.stop
100 }
101 }
Something went wrong with that request. Please try again.