oficina / equipo Public

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

Plain git — no account needed to clone.

Download

Download this file

1package equipotest
2
3import (
4 "testing"
5 "time"
6
7 "amadan.net/oficina/equipo/internal/equipo"
8 "amadan.net/oficina/equipo/internal/runnerproto"
9)
10
11func TestQuestionRenewedLease(t *testing.T) {
12 f, q := questionFixture(t)
13 original := f.claim.LeaseDeadline
14 f.clock.Advance(original.Sub(f.clock.Now()) / 2)
15 r := f.request()
16 r.RequestID = "renew-question"
17 result, err := f.x.Mutate(connectionContext, f.principal, "renew", r)
18 executionMust(t, err)
19 renewed := result.(time.Time)
20 f.clock.Advance(original.Sub(f.clock.Now()) + time.Millisecond)
21 answer := runnerproto.QuestionAnswer{Behavior: "deny"}
22 executionMust(t, f.s.AnswerQuestion(connectionContext, ownerActor, q.ID, 1, answer))
23 r.RequestID = "poll-renewed"
24 result, err = f.x.Mutate(connectionContext, f.principal, "answers", r)
25 executionMust(t, err)
26 answers := result.([]runnerproto.Question)
27 if len(answers) != 1 || !answers[0].ExpiresAt.Equal(renewed) {
28 t.Fatal("poll did not return current lease bound", answers)
29 }
30 f.clock.Advance(renewed.Sub(f.clock.Now()))
31 if f.s.AnswerQuestion(connectionContext, ownerActor, q.ID, 1, answer) == nil {
32 t.Fatal("lost lease accepted answer")
33 }
34 if _, err = f.x.Mutate(connectionContext, f.principal, "answers", r); err == nil {
35 t.Fatal("lost lease polled answer")
36 }
37}
38
39func TestQuestionAttemptDeadline(t *testing.T) {
40 f, q := questionFixture(t)
41 mustSQL(t, f.d, "UPDATE attempts SET created_at=? WHERE id=?", f.clock.Now().Add(-24*time.Hour).Format(time.RFC3339Nano), f.claim.Handle.Attempt)
42 if f.s.AnswerQuestion(connectionContext, ownerActor, q.ID, 1, runnerproto.QuestionAnswer{Behavior: "deny"}) == nil {
43 t.Fatal("attempt deadline ignored")
44 }
45}
46
47func TestQuestionStaleAnswerDoesNotBlockPoll(t *testing.T) {
48 for _, change := range []string{"demoted", "removed", "missing"} {
49 t.Run(change, func(t *testing.T) {
50 f, q := questionFixture(t)
51 actor := equipo.Actor{Address: "answerer@example.com"}
52 mustSQL(t, f.d, "INSERT INTO members(address,role,active,revision,session_epoch) VALUES(?,'owner',1,1,0)", actor.Address)
53 executionMust(t, f.s.AnswerQuestion(connectionContext, actor, q.ID, 1, runnerproto.QuestionAnswer{Behavior: "deny"}))
54 // Seed a second answered row to exercise a poll batch with a stale first row.
55 mustSQL(t, f.d, `INSERT INTO questions SELECT 'z-valid', assignment_id, task_id, server_id, agent_id, session_id, 'second-permission', kind, attempt, fence, revision, replace(raw_request,'permission','second-permission'), input_hash, state, answer, ?, expires_at FROM questions WHERE id=?`, ownerAddress, q.ID)
56 switch change {
57 case "demoted":
58 mustSQL(t, f.d, "UPDATE members SET role='member' WHERE address=?", actor.Address)
59 case "removed":
60 mustSQL(t, f.d, "UPDATE members SET active=0 WHERE address=?", actor.Address)
61 case "missing":
62 mustSQL(t, f.d, "DELETE FROM members WHERE address=?", actor.Address)
63 }
64 r := f.request()
65 r.RequestID = "poll-batch"
66 result, err := f.x.Mutate(connectionContext, f.principal, "answers", r)
67 executionMust(t, err)
68 answers := result.([]runnerproto.Question)
69 if len(answers) != 1 || answers[0].ID != "z-valid" {
70 t.Fatal("stale row blocked valid answer", answers)
71 }
72 if scalar(t, f.d, "SELECT count(*) FROM questions WHERE id='question' AND state='pending' AND answer='' AND answered_by=''") != 1 {
73 t.Fatal("stale answer not reopened")
74 }
75 executionMust(t, f.s.AnswerQuestion(connectionContext, ownerActor, q.ID, 1, runnerproto.QuestionAnswer{Behavior: "deny"}))
76 })
77 }
78}
79