Skip to content

Commit f0d8590

Browse files
authored
Check that CheckpointRepublishInterval is valid (#818)
1 parent 24a1d38 commit f0d8590

2 files changed

Lines changed: 62 additions & 0 deletions

File tree

append_lifecycle.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,9 @@ func (o AppendOptions) valid() error {
585585
if o.newCP == nil {
586586
return errors.New("invalid AppendOptions: WithCheckpointSigner must be set")
587587
}
588+
if o.checkpointRepublishInterval > 0 && o.checkpointRepublishInterval < o.checkpointInterval {
589+
return fmt.Errorf("invalid AppendOptions: WithCheckpointRepublishInterval (%d) is smaller than WithCheckpointInterval (%d)", o.checkpointRepublishInterval, o.checkpointInterval)
590+
}
588591
return nil
589592
}
590593

append_lifecycle_test.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@ package tessera
1616

1717
import (
1818
"context"
19+
"strings"
1920
"testing"
21+
"time"
22+
23+
"golang.org/x/mod/sumdb/note"
2024
)
2125

2226
func TestMemoize(t *testing.T) {
@@ -54,3 +58,58 @@ func TestMemoize(t *testing.T) {
5458
t.Fatalf("c(=%d) != d(=%d)", c.Index, d.Index)
5559
}
5660
}
61+
62+
const testSignerKey = "PRIVATE+KEY+example.com/log/testdata+33d7b496+AeymY/SZAX0jZcJ8enZ5FY1Dz+wTML2yWSkK+9DSF3eg"
63+
64+
func TestAppendOptionsValid(t *testing.T) {
65+
for _, test := range []struct {
66+
name string
67+
opts *AppendOptions
68+
wantErrContains string
69+
}{
70+
{
71+
name: "Valid",
72+
opts: NewAppendOptions().WithCheckpointSigner(mustCreateSigner(t, testSignerKey)),
73+
}, {
74+
name: "Valid: CheckpointRepublishInterval == CheckpointInterval",
75+
opts: NewAppendOptions().
76+
WithCheckpointSigner(mustCreateSigner(t, testSignerKey)).
77+
WithCheckpointInterval(10 * time.Second).
78+
WithCheckpointRepublishInterval(10 * time.Second),
79+
}, {
80+
name: "Error: CheckpointRepublishInterval < CheckpointInterval",
81+
opts: NewAppendOptions().
82+
WithCheckpointSigner(mustCreateSigner(t, testSignerKey)).
83+
WithCheckpointInterval(10 * time.Second).
84+
WithCheckpointRepublishInterval(9 * time.Second),
85+
wantErrContains: "WithCheckpointRepublishInterval",
86+
}, {
87+
name: "Error: No CheckpointSigner",
88+
opts: NewAppendOptions(),
89+
wantErrContains: "WithCheckpointSigner",
90+
},
91+
} {
92+
t.Run(test.name, func(t *testing.T) {
93+
err := test.opts.valid()
94+
switch gotErr, wantErr := err != nil, test.wantErrContains != ""; {
95+
case gotErr && !wantErr:
96+
t.Fatalf("Got unexpected error %q, want no error", err)
97+
case !gotErr && wantErr:
98+
t.Fatalf("Got no error, expected error")
99+
case gotErr:
100+
if !strings.Contains(err.Error(), test.wantErrContains) {
101+
t.Fatalf("Got err %q, want error containing %q", err.Error(), test.wantErrContains)
102+
}
103+
}
104+
})
105+
}
106+
}
107+
108+
func mustCreateSigner(t *testing.T, k string) note.Signer {
109+
t.Helper()
110+
s, err := note.NewSigner(k)
111+
if err != nil {
112+
t.Fatalf("Failed to create signer: %v", err)
113+
}
114+
return s
115+
}

0 commit comments

Comments
 (0)