Exemple #1
0
func loadDatastore() (data.Database, error) {
	if *boltdbPath != "" {
		return boltdata.Open(*boltdbPath)
	}

	return memdata.Open(), nil
}
Exemple #2
0
func TestRiver(t *testing.T) {
	assert := assert.New(t)
	db := memdata.Open()

	riv, err := NewRiver(db, -time.Minute)
	assert.Nil(err)

	now := time.Now().Round(time.Second)
	feeds := []models.Feed{
		{FeedTitle: "cool", FeedUrl: "http://cool", WhenLastUpdate: models.RssTime{now}},
		{FeedTitle: "what", FeedUrl: "http://what", WhenLastUpdate: models.RssTime{now}},
		{FeedTitle: "hey", FeedUrl: "http://hey", WhenLastUpdate: models.RssTime{now}},
		{FeedTitle: "hey2", FeedUrl: "http://hey", WhenLastUpdate: models.RssTime{now.Add(-10 * time.Second)}},
		{FeedTitle: "hey", FeedUrl: "http://hey", WhenLastUpdate: models.RssTime{now.Add(-2 * time.Second)}},
	}
	for _, feed := range feeds {
		riv.Add(feed)
	}

	// old feed, ignored
	oldfeed := models.Feed{FeedTitle: "out", FeedUrl: "out", WhenLastUpdate: models.RssTime{time.Now().Add(-2 * time.Minute)}}
	riv.Add(oldfeed)

	latest := riv.Latest()
	if assert.Len(latest, len(feeds)) {
		// ordered by date, then reverse alphabetically on FeedUrl
		assert.Equal(feeds[1], latest[0])
		assert.Equal(feeds[2], latest[1])
		assert.Equal(feeds[0], latest[2])
		assert.Equal(feeds[4], latest[3])
		assert.Equal(feeds[3], latest[4])
	}

	intriv, _ := riv.(*river)
	intriv.truncate()

	// make sure old feed has been deleted
	intriv.View(func(tx data.ReadTx) error {
		for _, v := range tx.All() {
			var feed models.Feed
			json.Unmarshal(v, &feed)
			assert.NotEqual(oldfeed.FeedTitle, feed.FeedTitle)
		}
		return nil
	})
}
Exemple #3
0
func TestBucket(t *testing.T) {
	assert := assert.New(t)
	db := memdata.Open()

	bucket, err := NewBucket(db, "test")
	assert.Nil(err)

	const key = "1"
	assert.False(bucket.Contains(key))
	assert.True(bucket.Contains(key))

	bucket2, err := NewBucket(db, "test2")
	assert.Nil(err)

	assert.False(bucket2.Contains(key))
	assert.True(bucket2.Contains(key))
}
Exemple #4
0
func TestRiver(t *testing.T) {
	db := memdata.Open()

	r := New(db, Options{})

	var buf bytes.Buffer
	r.WriteTo(&buf)

	var v models.River
	json.Unmarshal(buf.Bytes(), &v)

	assert := assert.New(t)

	assert.Equal(docsPath, v.Metadata.Docs)
	assert.WithinDuration(time.Now(), v.Metadata.WhenGMT.Time, time.Second)
	assert.WithinDuration(time.Now(), v.Metadata.WhenLocal.Time, time.Second)
	assert.Equal("3", v.Metadata.Version)
	assert.Equal(float64(0), v.Metadata.Secs)

	assert.Equal(models.Feeds{[]models.Feed{}}, v.UpdatedFeeds)
}
Exemple #5
0
func TestTributaryAtomWithRelativeLinks(t *testing.T) {
	s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte(`<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
 <title>GitHub Engineering</title>
 <link href="/atom.xml" rel="self"/>
 <link href=""/>
 <updated>2015-06-15T16:58:27+00:00</updated>
 <id></id>
 <author>
   <name>GitHub Engineering</name>
 </author>

 <entry>
   <title>Brubeck, a statsd-compatible metrics aggregator</title>
   <link href="/brubeck/"/>
   <updated>2015-06-15T00:00:00+00:00</updated>
   <id>/brubeck</id>

     <author>
       <name>vmg</name>
       <uri>https://github.com/vmg</uri>
     </author>

   <content type="html">&lt;p&gt;One of the key points of GitHub&amp;#39;s engineering culture —and I believe, of any
good engineering culture— is our obsession with aggressively measuring everything.&lt;/p&gt;</content>
  </entry>
</feed>`))
	}))
	defer s.Close()

	db := memdata.Open()
	bucket, _ := persistence.NewBucket(db, "-")

	tributary := newTributary(bucket, s.URL, time.Minute, DefaultMapping)
	tributary.Start()

	expected := models.Feed{
		FeedUrl:         s.URL + "/atom.xml",
		WebsiteUrl:      s.URL,
		FeedTitle:       "GitHub Engineering",
		FeedDescription: "",
		WhenLastUpdate:  models.RssTime{time.Now()},
		Items: []models.Item{{
			Title:      "Brubeck, a statsd-compatible metrics aggregator",
			Link:       s.URL + "/brubeck/",
			PermaLink:  s.URL + "/brubeck/",
			Id:         "/brubeck",
			PubDate:    models.RssTime{time.Date(2015, 06, 15, 0, 0, 0, 0, time.FixedZone("", 0))},
			Body:       "One of the key points of GitHub's engineering culture —and I believe, of any good engineering culture— is our obsession with aggressively measuring everything.\n",
			Enclosures: []models.Enclosure{},
		}},
	}

	assert := assert.New(t)

	feeds := make(chan models.Feed)
	tributary.Feeds(feeds)

	select {
	case f := <-feeds:
		assert.Equal(expected.FeedUrl, f.FeedUrl)
		assert.Equal(expected.WebsiteUrl, f.WebsiteUrl)
		assert.Equal(expected.FeedTitle, f.FeedTitle)
		assert.Equal(expected.FeedDescription, f.FeedDescription)
		assert.WithinDuration(expected.WhenLastUpdate.Time, f.WhenLastUpdate.Time, time.Second)

		assert.Equal(1, len(f.Items))
		assertItemEqual(t, expected.Items[0], f.Items[0])

	case <-time.After(time.Second):
		t.Fatal("timeout")
	}
}
Exemple #6
0
func TestTributary(t *testing.T) {
	s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte(`<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <title>Boing Boing</title>
    <link>http://boingboing.net</link>
    <description>Brain candy for Happy Mutants</description>
    <language>en-US</language>
    <lastBuildDate>Wed, 27 Mar 2013 12:30:18 WHAT</lastBuildDate>
    <item>
      <title>Save Noisebridge!</title>
      <link>http://feedproxy.google.com/~r/boingboing/iBag/~3/EKKb-61Ismc/story01.htm</link>
      <pubDate>Wed, 27 Mar 2013 12:40:18 UTC</pubDate>
      <guid isPermaLink="false">http://boingboing.net/?p=221544</guid>
      <description>A reader writes, "Noisebridge, San Francisco's Hackerspace, is having some hard times, so we're throwing an epic benefit and party this Saturday, to include eclectic performers, interactive art, a raffle and more! For more details, if any BBers want to put on demos or ideas share them.&lt;img width='1' height='1' src='http://rss.feedsportal.com/c/35208/f/653965/s/2a105a0e/mf.gif' border='0'/&gt;&lt;div class='mf-viral'&gt;&lt;table border='0'&gt;&lt;tr&gt;&lt;td valign='middle'&gt;&lt;a href="http://share.feedsportal.com/viral/sendEmail.cfm?lang=en&amp;title=Save+Noisebridge%21&amp;link=http%3A%2F%2Fboingboing.net%2F2013%2F03%2F27%2Fsave-noisebridge.html" target="_blank"&gt;&lt;img src="http://res3.feedsportal.com/images/emailthis2.gif" border="0" /&gt;&lt;/a&gt;&lt;/td&gt;&lt;td valign='middle'&gt;&lt;a href="http://res.feedsportal.com/viral/bookmark.cfm?title=Save+Noisebridge%21&amp;link=http%3A%2F%2Fboingboing.net%2F2013%2F03%2F27%2Fsave-noisebridge.html" target="_blank"&gt;&lt;img src="http://res3.feedsportal.com/images/bookmark.gif" border="0" /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;</description>
    </item>
  </channel>
</rss>`))
	}))
	defer s.Close()

	db := memdata.Open()
	bucket, _ := persistence.NewBucket(db, "-")

	tributary := newTributary(bucket, s.URL, time.Minute, DefaultMapping)
	tributary.Start()

	expected := models.Feed{
		FeedUrl:         s.URL,
		WebsiteUrl:      "http://boingboing.net",
		FeedTitle:       "Boing Boing",
		FeedDescription: "Brain candy for Happy Mutants",
		WhenLastUpdate:  models.RssTime{time.Now()},
		Items: []models.Item{{
			Title:      "Save Noisebridge!",
			Link:       "http://feedproxy.google.com/~r/boingboing/iBag/~3/EKKb-61Ismc/story01.htm",
			PermaLink:  "http://feedproxy.google.com/~r/boingboing/iBag/~3/EKKb-61Ismc/story01.htm",
			Id:         "http://boingboing.net/?p=221544",
			PubDate:    models.RssTime{time.Date(2013, 03, 27, 12, 40, 18, 0, time.UTC)},
			Body:       "A reader writes, \"Noisebridge, San Francisco's Hackerspace, is having some hard times, so we're throwing an epic benefit and party this Saturday, to include eclectic performers, interactive art, a raffle and more! For more details, if any BBers want to put on demos or ideas shar…",
			Enclosures: []models.Enclosure{},
		}},
	}

	assert := assert.New(t)

	feeds := make(chan models.Feed)
	tributary.Feeds(feeds)

	select {
	case f := <-feeds:
		assert.Equal(expected.FeedUrl, f.FeedUrl)
		assert.Equal(expected.WebsiteUrl, f.WebsiteUrl)
		assert.Equal(expected.FeedTitle, f.FeedTitle)
		assert.Equal(expected.FeedDescription, f.FeedDescription)
		assert.WithinDuration(expected.WhenLastUpdate.Time, f.WhenLastUpdate.Time, time.Second)
		assert.Equal(expected.Items, f.Items)

	case <-time.After(time.Second):
		t.Fatal("timeout")
	}
}