| 1 | package runner |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "testing" |
| 7 | "time" |
| 8 | |
| 9 | "amadan.net/oficina/equipo/internal/runnerproto" |
| 10 | ) |
| 11 | |
| 12 | type resilientSource struct { |
| 13 | polls int |
| 14 | attempt bool |
| 15 | } |
| 16 | |
| 17 | func (s *resilientSource) Poll(context.Context) ([]runnerproto.Assignment, error) { |
| 18 | s.polls++ |
| 19 | if s.polls > 1 { |
| 20 | return nil, context.Canceled |
| 21 | } |
| 22 | if s.attempt { |
| 23 | return []runnerproto.Assignment{{ID: "work", Revision: 1}}, nil |
| 24 | } |
| 25 | return nil, ErrTransient |
| 26 | } |
| 27 | func (s *resilientSource) Claim(context.Context, runnerproto.ClaimRequest) (runnerproto.ClaimResponse, error) { |
| 28 | return runnerproto.ClaimResponse{}, nil |
| 29 | } |
| 30 | func (s *resilientSource) RecoverClaim(context.Context, runnerproto.ClaimRequest) (runnerproto.ClaimResponse, error) { |
| 31 | return runnerproto.ClaimResponse{}, ErrNotClaimed |
| 32 | } |
| 33 | func TestServiceSurvivesPollAndAttemptErrors(t *testing.T) { |
| 34 | for _, attempt := range []bool{false, true} { |
| 35 | t.Run(map[bool]string{false: "poll", true: "attempt"}[attempt], func(t *testing.T) { |
| 36 | s, record, _, _ := cleanupFixture(t) |
| 37 | record.Phase = "contained" |
| 38 | mustJournal(t, s.save(context.Background(), record)) |
| 39 | s.workflow = &BugWorkflow{} |
| 40 | source := &resilientSource{attempt: attempt} |
| 41 | service := Service{Supervisor: s, Source: source} |
| 42 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 43 | defer cancel() |
| 44 | err := service.Run(ctx) |
| 45 | if !errors.Is(err, context.Canceled) || source.polls != 2 { |
| 46 | t.Fatal("service stopped on recoverable error", err, source.polls) |
| 47 | } |
| 48 | }) |
| 49 | } |
| 50 | } |
| 51 | |