internal/packlimit/watch_test.go

bd49b87fce895e9f0a7588152548fb6e1821ac7d
gitbay/internal/packlimit/watch_test.go history · blame · raw

40 lines · 793 bytes

 1package packlimit
 2
 3import (
 4	"io"
 5	"testing"
 6	"time"
 7)
 8
 9func TestWatchStalls(t *testing.T) {
10	old := StallDeadline
11	StallDeadline = 100 * time.Millisecond
12	t.Cleanup(func() { StallDeadline = old })
13	l := New(1, 0, 0, time.Second)
14	w, stalled, stop := l.Watch(io.Discard)
15	defer stop()
16	// Writes keep it alive past the deadline.
17	for range 6 {
18		time.Sleep(40 * time.Millisecond)
19		w.Write([]byte("x"))
20		select {
21		case <-stalled:
22			t.Fatal("stalled while writing")
23		default:
24		}
25	}
26	select {
27	case <-stalled:
28	case <-time.After(2 * time.Second):
29		t.Fatal("no stall after writes stopped")
30	}
31}
32
33func TestWatchWithoutLimit(t *testing.T) {
34	var l *Limiter
35	w, stalled, stop := l.Watch(io.Discard)
36	defer stop()
37	if w != io.Discard || stalled != nil {
38		t.Fatal("a nil limiter watched")
39	}
40}