File: queue_test.go

package info (click to toggle)
golang-github-lestrrat-go-httprc 1.0.6-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 168 kB
  • sloc: perl: 56; sh: 6; makefile: 2
file content (77 lines) | stat: -rw-r--r-- 1,752 bytes parent folder | download
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package httprc

import (
	"context"
	"fmt"
	"net/http"
	"net/http/httptest"
	"testing"
	"time"

	"github.com/stretchr/testify/require"
)

// Sanity check for the queue portion

type dummyFetcher struct {
	srv *httptest.Server
}

func (f *dummyFetcher) Fetch(_ context.Context, _ string, _ ...FetchOption) (*http.Response, error) {
	panic("unimplemented")
}

// URLs must be for f.srv
func (f *dummyFetcher) fetch(_ context.Context, fr *fetchRequest) (*http.Response, error) {
	//nolint:noctx
	return f.srv.Client().Get(fr.url)
}

type noErrorSink struct {
	t *testing.T
}

func (s *noErrorSink) Error(err error) {
	s.t.Errorf(`unexpected error: %s`, err)
}

func TestQueue(t *testing.T) {
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

	srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.WriteHeader(http.StatusOK)
	}))
	defer srv.Close()

	q := newQueue(ctx, 15*time.Minute, &dummyFetcher{srv: srv}, &noErrorSink{t: t})

	base := time.Now()
	q.clock = clockFunc(func() time.Time {
		return base
	})

	// 0..4
	for i := 0; i < 4; i++ {
		q.Enqueue(fmt.Sprintf("%s/%d", srv.URL, i), time.Duration(i)*time.Second)
	}

	// 0..4, 6..9
	for i := 7; i < 10; i++ {
		q.Enqueue(fmt.Sprintf("%s/%d", srv.URL, i), time.Duration(i)*time.Second)
	}

	// 0...9
	for i := 4; i < 7; i++ {
		q.Enqueue(fmt.Sprintf("%s/%d", srv.URL, i), time.Duration(i)*time.Second)
	}

	var prevTm time.Time
	for i, rqe := range q.list {
		require.True(t, prevTm.Before(rqe.fireAt), `entry %d must have fireAt before %s (got %s)`, i, prevTm, rqe.fireAt)
		u := fmt.Sprintf("%s/%d", srv.URL, i)
		require.Equal(t, u, rqe.url, `entry %d must have url same as %s (got %s)`, i, u, rqe.url)

		prevTm = rqe.fireAt
	}
}