oficina / equipo Public

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

Plain git — no account needed to clone.

Download

Download this file

1package equipo
2
3import (
4 "context"
5 "io"
6 "log/slog"
7 "net/http"
8 "net/http/httptest"
9 "strings"
10 "testing"
11 "time"
12
13 "amadan.net/rastrillo/rastrillo/db"
14 "amadan.net/rastrillo/rastrillo/dbtest"
15 "amadan.net/rastrillo/rastrillo/live"
16)
17
18func TestTaskLiveTopicKeysAndRestartSchedules(t *testing.T) {
19 keys := map[string]bool{}
20 for _, id := range []string{"a/b", "a", "a/collection", "team"} {
21 for _, job := range []JobID{0, 1, 2} {
22 key := jobTopic(id, job)
23 if len(key) != 69 || strings.Trim(key[5:], "0123456789abcdef") != "" || keys[key] {
24 t.Fatal("noncanonical topic", key)
25 }
26 keys[key] = true
27 }
28 }
29 schema := dbtest.FromSet(BootSchema)
30 defer schema.Remove()
31 d, err := db.Open(schema.Path(t), slog.New(slog.NewTextHandler(io.Discard, nil)))
32 if err != nil {
33 t.Fatal(err)
34 }
35 defer d.Close()
36 s := NewStore(d)
37 if err = s.Bootstrap(context.Background(), []string{"owner@example.com"}); err != nil {
38 t.Fatal(err)
39 }
40 var instance Team
41 if err = d.G.First(&instance).Error; err != nil {
42 t.Fatal(err)
43 }
44 if err = d.G.Create(&Task{ID: "deadline-task", TeamID: instance.ID}).Error; err != nil {
45 t.Fatal(err)
46 }
47 if err = d.G.Create(&Task{ID: "foreign", TeamID: "foreign", RequestID: "foreign"}).Error; err != nil {
48 t.Fatal(err)
49 }
50 at := time.Now().Add(500 * time.Millisecond)
51 if err = d.G.Create(&questionRecord{ID: "foreign-deadline", TaskID: "foreign", State: "pending", ExpiresAt: at.Add(-300 * time.Millisecond)}).Error; err != nil {
52 t.Fatal(err)
53 }
54 if err = d.G.Create(&questionRecord{ID: "deadline", TaskID: "deadline-task", State: "pending", ExpiresAt: at}).Error; err != nil {
55 t.Fatal(err)
56 }
57 var prior string
58 for i := 0; i < 2; i++ {
59 hub, e := live.New(live.Config{Path: "/live", Topics: map[string]live.Allow{"jobs": func(*http.Request, string) bool { return true }}, Lifetime: 30 * time.Second})
60 if e != nil {
61 t.Fatal(e)
62 }
63 if hub.Boot() == prior {
64 t.Fatal("restart retained boot ID")
65 }
66 prior = hub.Boot()
67 s.SetJobNotifier(hub)
68 if err = s.restoreLiveSchedules(context.Background()); err != nil {
69 hub.Close()
70 t.Fatal(err)
71 }
72 if i == 0 {
73 hub.Close()
74 continue
75 }
76 defer hub.Close()
77 deadline := time.After(2 * time.Second)
78 tick := time.NewTicker(10 * time.Millisecond)
79 defer tick.Stop()
80 for {
81 req := httptest.NewRequest("GET", "/journey", nil)
82 var attrs string
83 hub.Middleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
84 attrs = string(hub.View(r).Attrs(jobTopic(instance.ID, 0)))
85 })).ServeHTTP(httptest.NewRecorder(), req)
86 if strings.Contains(attrs, "@1") {
87 if time.Now().Before(at) {
88 t.Fatal("foreign-team expiry woke collection")
89 }
90 break
91 }
92 select {
93 case <-deadline:
94 t.Fatal("boot did not reconstruct expiry notification")
95 case <-tick.C:
96 }
97 }
98 }
99}
100