carlos / platform Public

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

Plain git — no account needed to clone.

Download

Download this file

1package host
2
3import (
4 "context"
5 "errors"
6 "testing"
7
8 "github.com/carlosframework/platform/internal/hostwrite"
9)
10
11type assemblyTestWork struct {
12 Before func()
13 Wait bool
14}
15
16func (w assemblyTestWork) With(ctx context.Context, _ hostwrite.Identity, fn func() error) error {
17 if w.Before != nil {
18 w.Before()
19 }
20 if w.Wait {
21 <-ctx.Done()
22 return ctx.Err()
23 }
24 return fn()
25}
26func TestClaimLeaseWorkRechecksAfterGuardWait(t *testing.T) {
27 lost := false
28 ran := false
29 denial := errors.New("lease lost")
30 w := claimLeaseWork{Work: assemblyTestWork{Before: func() { lost = true }}, Alive: context.Background(), Check: func() error {
31 if lost {
32 return denial
33 }
34 return nil
35 }}
36 if err := w.With(context.Background(), hostwrite.Identity{}, func() error { ran = true; return nil }); !errors.Is(err, denial) || ran {
37 t.Fatalf("effect=%v err=%v", ran, err)
38 }
39}
40func TestClaimLeaseWorkCancellationInterruptsGuardWait(t *testing.T) {
41 live, cancel := context.WithCancel(context.Background())
42 w := claimLeaseWork{Work: assemblyTestWork{Before: cancel, Wait: true}, Alive: live, Check: func() error { return nil }}
43 if err := w.With(context.Background(), hostwrite.Identity{}, func() error { t.Fatal("effect after cancellation"); return nil }); !errors.Is(err, context.Canceled) {
44 t.Fatal(err)
45 }
46}
47