notify_test.go 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package rss
  2. import (
  3. "context"
  4. "fmt"
  5. "io/ioutil"
  6. "net/http"
  7. "net/http/httptest"
  8. "sync/atomic"
  9. "testing"
  10. "time"
  11. "github.com/stretchr/testify/assert"
  12. "github.com/stretchr/testify/require"
  13. )
  14. func TestNotify(t *testing.T) {
  15. var n int32
  16. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  17. fnum := atomic.AddInt32(&n, int32(1))
  18. if fnum > 2 {
  19. fnum = 2
  20. }
  21. data, err := ioutil.ReadFile(fmt.Sprintf("testdata/f%d.xml", fnum))
  22. require.NoError(t, err)
  23. w.WriteHeader(200)
  24. w.Write(data)
  25. }))
  26. defer ts.Close()
  27. notify := New(context.Background(), ts.URL, time.Millisecond*250)
  28. ch := notify.Go()
  29. defer notify.Shutdown()
  30. st := time.Now()
  31. e := <-ch
  32. t.Logf("%+v", e)
  33. assert.Equal(t, Event{ChanTitle: "Радио-Т", Title: "Радио-Т 626",
  34. Link: "https://radio-t.com/p/2018/12/01/podcast-626/", guid: "https://radio-t.com/p/2018/12/01//podcast-626/"}, e)
  35. assert.True(t, time.Since(st) >= time.Millisecond*250)
  36. select {
  37. case <-ch:
  38. t.Fatal("should not get any more")
  39. default:
  40. }
  41. }