| 1 | package relay |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "testing" |
| 7 | "time" |
| 8 | ) |
| 9 | |
| 10 | func TestAuthorityRoundTrip(t *testing.T) { |
| 11 | s := newMemStore(t) |
| 12 | ctx := context.Background() |
| 13 | a, _, err := ReadAuthority(ctx, s) |
| 14 | if err != nil || a != nil { |
| 15 | t.Fatalf("absent = %v %v, want nil,nil", a, err) |
| 16 | } |
| 17 | body, _ := json.Marshal(Authority{Apps: []AuthorityApp{{Account: "abc", App: "meet", Incarnation: "0123456789abcdef", Regions: []string{"eu-west-1"}}}, WrittenAt: time.Unix(1_800_000_000, 0).UTC()}) |
| 18 | if _, err := s.Put(ctx, AuthorityKey, body, ""); err != nil { |
| 19 | t.Fatal(err) |
| 20 | } |
| 21 | a, etag, err := ReadAuthority(ctx, s) |
| 22 | if err != nil || etag == "" { |
| 23 | t.Fatal(err) |
| 24 | } |
| 25 | if got, ok := a.Lookup("abc", "meet"); !ok || got.Incarnation != "0123456789abcdef" { |
| 26 | t.Fatalf("lookup = %+v %v", got, ok) |
| 27 | } |
| 28 | if _, ok := a.Lookup("abc", "other"); ok { |
| 29 | t.Fatal("unknown app found") |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | func TestWriteHeartbeatTwice(t *testing.T) { |
| 34 | s := newMemStore(t) |
| 35 | ctx := context.Background() |
| 36 | t0 := time.Unix(1_800_000_000, 0).UTC() |
| 37 | if err := WriteHeartbeat(ctx, s, Heartbeat{Box: "box1", Region: "eu-west-1", ReadAt: t0}); err != nil { |
| 38 | t.Fatalf("first write: %v", err) |
| 39 | } |
| 40 | t1 := t0.Add(30 * time.Second) |
| 41 | if err := WriteHeartbeat(ctx, s, Heartbeat{Box: "box1", Region: "eu-west-1", ReadAt: t1}); err != nil { |
| 42 | t.Fatalf("second write: %v", err) |
| 43 | } |
| 44 | body, _, err := s.Get(ctx, HeartbeatKey("box1")) |
| 45 | if err != nil { |
| 46 | t.Fatal(err) |
| 47 | } |
| 48 | var got Heartbeat |
| 49 | if err := json.Unmarshal(body, &got); err != nil { |
| 50 | t.Fatal(err) |
| 51 | } |
| 52 | if !got.ReadAt.Equal(t1) { |
| 53 | t.Fatalf("read_at = %v, want %v", got.ReadAt, t1) |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | func TestLease(t *testing.T) { |
| 58 | var l Lease |
| 59 | t0 := time.Unix(1_800_000_000, 0) |
| 60 | if l.Live(t0) { |
| 61 | t.Fatal("never renewed should not be live") |
| 62 | } |
| 63 | l.Renew(t0) |
| 64 | if !l.Live(t0.Add(14 * time.Minute)) { |
| 65 | t.Fatal("14m should be live") |
| 66 | } |
| 67 | if l.Live(t0.Add(16 * time.Minute)) { |
| 68 | t.Fatal("16m should not be live") |
| 69 | } |
| 70 | } |
| 71 | |