PageRenderTime 25ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/syndor-feedbot/src/test/scala/syndor/feedbot/FeedBotSpec.scala

http://github.com/HendraWijaya/syndor
Scala | 77 lines | 57 code | 17 blank | 3 comment | 0 complexity | 32fb1f2de0a83c8b8d182ea1f08e0503 MD5 | raw file
  1. package syndor.feedbot
  2. import org.scalatest.FlatSpec
  3. import org.scalatest.BeforeAndAfterEach
  4. import org.scalatest.matchers.ShouldMatchers
  5. import org.scalatest.BeforeAndAfterAll
  6. import syndor.model.Source
  7. import syndor.EnvironmentSupport._
  8. import akka.actor.Actor.actorOf
  9. import syndor.model.FeedItem
  10. import syndor.model.Feed
  11. import com.mongodb.casbah.commons.MongoDBObject
  12. import java.text.SimpleDateFormat
  13. class FeedBotSpec extends FlatSpec
  14. with ShouldMatchers
  15. with BeforeAndAfterAll
  16. with BeforeAndAfterEach
  17. with TestFeedSupport
  18. with TestHttpServerSupport {
  19. private val feedBot = actorOf(new FeedBot(nrOfFeeds = 10, maxNrOfFetching = 100))
  20. override def beforeAll() {
  21. feedBot.start()
  22. super.beforeAll()
  23. }
  24. override def afterAll() {
  25. try {
  26. super.afterAll()
  27. } finally {
  28. feedBot.stop()
  29. }
  30. }
  31. behavior of "feed bot"
  32. it should "update feeds concurrently" in {
  33. Feed.collection.count should be(0)
  34. // Insert 3 feeds
  35. insertFeed(httpServer.resolve("/resource/reuters_2011_10_23.xml").toExternalForm()) // 10 items
  36. insertFeed(httpServer.resolve("/resource/tempo_fokus_2011_10_29.xml").toExternalForm()) // 15 items
  37. insertFeed(httpServer.resolve("/resource/techcrunch_2011_10_31.xml").toExternalForm()) // 20 items
  38. Feed.collection.count should be(3)
  39. FeedItem.collection.count should be(0)
  40. feedBot ! FeedBot.UpdateFeeds
  41. // Sleep as long as timeout to make sure it has finished and add 100 to make sure data
  42. // are written to mongo
  43. Thread.sleep(FeedBotConfig.fetcherTimeoutInMillis + 100)
  44. FeedItem.collection.count should be(45)
  45. val title = "Spain, Italy under pressure as EU frames bank deal"
  46. val cursor = FeedItem.find(ref = MongoDBObject("title" -> title))
  47. val item = cursor.next
  48. item.title should be(title)
  49. item.publishedDate should be (new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z").parse("Sun, 23 Oct 2011 03:42:14 -0400").getTime)
  50. cursor.hasNext should be(false)
  51. }
  52. def insertFeed(url: String): Feed = {
  53. val feed = Feed(
  54. sourceId = source.id,
  55. title = "Test",
  56. url = url)
  57. Feed.insert(feed)
  58. return feed
  59. }
  60. }