-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhackernews_mock.go
58 lines (46 loc) · 1.16 KB
/
hackernews_mock.go
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
package mtp
import (
"sync"
)
// HackerNewsMock contains a list of articles
type HackerNewsMock struct {
articlesLock sync.RWMutex
articles map[string]Article
waiter Waiter
failer Failer
logger Logger
}
// GetLatestArticles gets the homepage of hacker news
func (s *HackerNewsMock) GetLatestArticles() ([]Article, error) {
s.waiter.Wait()
err := s.failer.Fails()
if err != nil {
s.logger.Errorf("GetLatestArticles failed with err=%s", err)
return nil, err
}
var articles []Article
s.articlesLock.RLock()
defer s.articlesLock.RUnlock()
// Get all articles in the map
for _, article := range s.articles {
articles = append(articles, article)
}
return articles, nil
}
// FeedArticles stores articles in the mock HackerNews map
// here for testing purposes
func (s *HackerNewsMock) FeedArticles(articles []Article) error {
s.waiter.Wait()
err := s.failer.Fails()
if err != nil {
s.logger.Errorf("FeedArticles failed with err=%s", err)
return err
}
s.articlesLock.Lock()
defer s.articlesLock.Unlock()
for _, article := range articles {
s.articles[article.Link] = article
}
s.logger.Infof("Fed %d articles", len(articles))
return nil
}