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 "path/filepath"
5 "testing"
6)
7
8func TestClaimTimerRegistrationWritesKeepOwnership(t *testing.T) {
9 reg, err := Open(filepath.Join(t.TempDir(), "registry.db"))
10 if err != nil {
11 t.Fatal(err)
12 }
13 defer reg.Close()
14 owner := ClaimOwnership{"inc", 3, "owner", "route"}
15 host := "shop.example"
16 if err = reg.Upsert(Route{Host: host, ClaimOwnership: owner}); err != nil {
17 t.Fatal(err)
18 }
19 timer := Timer{Host: host, Name: "tick", Kind: "schedule", Spec: "every 1h0m0s", Path: "/tick", NextAt: 100}
20 first, err := reg.UpsertTimerRegistration(timer, "")
21 if err != nil {
22 t.Fatal(err)
23 }
24 if first.ClaimOwner != owner.ClaimOwner || first.ClaimStamp != 3 || first.ClaimRowID != first.Registration {
25 t.Fatalf("unowned timer: %+v", first)
26 }
27 timer.NextAt = 0
28 unchanged, err := reg.UpsertTimerRegistration(timer, first.Registration)
29 if err != nil {
30 t.Fatal(err)
31 }
32 if unchanged.Registration != first.Registration || unchanged.ClaimRowID != first.ClaimRowID {
33 t.Fatal("no-op replaced registration")
34 }
35 if err = reg.FireTimerNowRegistration(host, "tick", first.Registration, 200); err != nil {
36 t.Fatal(err)
37 }
38 current, err := reg.GetTimer(host, "tick")
39 if err != nil {
40 t.Fatal(err)
41 }
42 if current.Registration == first.Registration || current.ClaimRowID != current.Registration || current.ClaimOwner != owner.ClaimOwner {
43 t.Fatalf("lost ownership: %+v", current)
44 }
45 if _, err = reg.UpsertTimerRegistration(timer, first.Registration); err == nil {
46 t.Fatal("stale registration accepted")
47 }
48}
49