func TestSemaphoreDoesNotAcquireAtCapacity(t *testing.T) { s := semaphore.New(2) s.Acquire(2) acquired := make(chan struct{}) go func() { s.Acquire(1) acquired <- struct{}{} }() select { case <-acquired: t.Errorf("semaphore acquired") case <-time.After(1 * time.Second): } }
func TestSemaphoreAcquires(t *testing.T) { s := semaphore.New(1) acquired := make(chan struct{}) go func() { s.Acquire(1) acquired <- struct{}{} }() select { case <-acquired: case <-time.After(1 * time.Second): t.Errorf("semaphore not acquired") } }
func main() { arguments, err := docopt.Parse(usage, nil, true, version, false) check(err) hnClient := hn.New() instapaperClient := instapaper.New(arguments["<username>"].(string), arguments["<password>"].(string)) limit, err := strconv.Atoi(arguments["--limit"].(string)) check(err) stories, err := hnClient.TopStories() check(err) s := semaphore.New(10) var wg sync.WaitGroup for i, id := range stories { if i >= limit { break } wg.Add(1) s.Acquire(1) go func(id int) { defer wg.Done() defer s.Release(1) story, err := hnClient.GetPost(id) check(err) if story.URL == nil { fmt.Println("Skipping", *story.Title) return } _, err = instapaperClient.Add(instapaper.AddParams{ URL: *story.URL, Title: story.Title, }) check(err) fmt.Println("Saved", *story.Title) }(id) } wg.Wait() }