oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1package runner
2
3import (
4 "context"
5 "errors"
6 "testing"
7 "time"
8
9 "amadan.net/oficina/equipo/internal/runnerproto"
10)
11
12type refusedSource struct {
13 claims []time.Time
14 cancel context.CancelFunc
15}
16
17func (s *refusedSource) Poll(context.Context) ([]runnerproto.Assignment, error) {
18 return []runnerproto.Assignment{{ID: "refused", Revision: 1}}, nil
19}
20func (s *refusedSource) Claim(context.Context, runnerproto.ClaimRequest) (runnerproto.ClaimResponse, error) {
21 s.claims = append(s.claims, time.Now())
22 if len(s.claims) == 3 {
23 s.cancel()
24 }
25 return runnerproto.ClaimResponse{}, ErrClaimRefused
26}
27func TestClaimRefusalsBackOff(t *testing.T) {
28 s, record, _, _ := cleanupFixture(t)
29 record.Phase = "contained"
30 mustJournal(t, s.save(context.Background(), record))
31 ctx, cancel := context.WithTimeout(context.Background(), 8*time.Second)
32 defer cancel()
33 source := &refusedSource{cancel: cancel}
34 service := Service{Supervisor: s, Source: source}
35 err := service.Run(ctx)
36 if !errors.Is(err, context.Canceled) || len(source.claims) != 3 {
37 t.Fatalf("refusal loop: %v %d", err, len(source.claims))
38 }
39 for i, want := range []time.Duration{time.Second, 2 * time.Second} {
40 if got := source.claims[i+1].Sub(source.claims[i]); got < want {
41 t.Fatalf("claim refusal did not back off: delay=%s want>=%s", got, want)
42 }
43 }
44}
45
46func TestClaimBackoffBounds(t *testing.T) {
47 for failures := 1; failures <= 20; failures++ {
48 base := time.Second * time.Duration(1<<min(failures-1, 5))
49 if base > 30*time.Second {
50 base = 30 * time.Second
51 }
52 for range 20 {
53 got := claimBackoffDelay(failures)
54 if got < base || got > min(30*time.Second, base+base/4) {
55 t.Fatalf("backoff %d = %s outside bounded exponential jitter", failures, got)
56 }
57 }
58 }
59}
60