carlos / platform Public

Clone
git clone https://amadan.net/carlos/platform

Plain git — no account needed to clone.

Download

Download this file

1package registry
2
3import (
4 "fmt"
5 "reflect"
6 "testing"
7)
8
9func TestArtifactHosts(t *testing.T) {
10 for _, mode := range []string{"empty", "union", "missing-timers", "closed", "limit", "overflow"} {
11 t.Run(mode, func(t *testing.T) {
12 r := openTestRegistry(t)
13 defer r.Close()
14 var want []string
15 switch mode {
16 case "union":
17 for _, host := range []string{"route-only", "both"} {
18 if err := r.Upsert(Route{Host: host}); err != nil {
19 t.Fatal(err)
20 }
21 }
22 for _, host := range []string{"orphan", "both"} {
23 if err := r.ReplaceTimers(host, []Timer{{Host: host, Name: "paused", Kind: "cron", NextAt: 0}, {Host: host, Name: "done", Kind: "once", NextAt: -1}}); err != nil {
24 t.Fatal(err)
25 }
26 }
27 want = []string{"both", "orphan", "route-only"}
28 case "missing-timers":
29 if _, err := r.sql.Exec("DROP TABLE timers"); err != nil {
30 t.Fatal(err)
31 }
32 case "closed":
33 if err := r.Close(); err != nil {
34 t.Fatal(err)
35 }
36 case "limit", "overflow":
37 n := 8192
38 if mode == "overflow" {
39 n++
40 }
41 tx, err := r.sql.Begin()
42 if err != nil {
43 t.Fatal(err)
44 }
45 defer tx.Rollback()
46 for i := 0; i < n; i++ {
47 host := fmt.Sprintf("host-%05d", i)
48 if _, err := tx.Exec("INSERT INTO timers (host,name,kind,spec,path,next_at,created_at) VALUES (?, 'paused', 'cron', '', '', 0, 1)", host); err != nil {
49 t.Fatal(err)
50 }
51 want = append(want, host)
52 }
53 if err := tx.Commit(); err != nil {
54 t.Fatal(err)
55 }
56 }
57 got, err := r.ArtifactHosts()
58 if mode == "closed" || mode == "missing-timers" || mode == "overflow" {
59 if err == nil || got != nil {
60 t.Fatalf("incomplete discovery: count=%d error=%v", len(got), err)
61 }
62 return
63 }
64 if err != nil || !reflect.DeepEqual(got, want) {
65 t.Fatalf("host discovery: count=%d want=%d error=%v", len(got), len(want), err)
66 }
67 })
68 }
69}
70