@@ -16,6 +16,8 @@ package tessera
1616
1717import (
1818 "context"
19+ "errors"
20+ "fmt"
1921 "strings"
2022 "testing"
2123 "time"
@@ -150,22 +152,70 @@ func mustCreateSigner(t *testing.T, k string) note.Signer {
150152 return s
151153}
152154
153- func TestShutdownTimeout (t * testing.T ) {
155+ func TestShutdownBehavior (t * testing.T ) {
156+ tests := []struct {
157+ name string
158+ wantTreeSize uint64
159+ cpSize uint64
160+ expectWait bool
161+ }{
162+ {
163+ name : "no work done" ,
164+ wantTreeSize : 0 ,
165+ expectWait : false ,
166+ },
167+ {
168+ name : "wait for index 0" ,
169+ wantTreeSize : 1 ,
170+ cpSize : 0 ,
171+ expectWait : true ,
172+ },
173+ {
174+ name : "already caught up" ,
175+ wantTreeSize : 1 ,
176+ cpSize : 1 ,
177+ expectWait : false ,
178+ },
179+ }
180+
181+ for _ , test := range tests {
182+ t .Run (test .name , func (t * testing.T ) {
183+ term := & terminator {
184+ readCheckpoint : func (ctx context.Context ) ([]byte , error ) {
185+ // Return a valid checkpoint string that parse.CheckpointUnsafe can parse.
186+ return []byte (fmt .Sprintf ("example.com\n %d\n qINS1GRFhWHwdkUeqLEoP4yEMkTBBzxBkGwGQlVlVcs=\n " , test .cpSize )), nil
187+ },
188+ shutdownTimeout : 10 * time .Millisecond ,
189+ }
190+ term .wantTreeSize .Store (test .wantTreeSize )
191+
192+ // If we've added an entry, then the terminator should wait for a checkpoint covering it.
193+ // Since we don't provide any checkpoints, we can detect this by waiting for it to timeout.
194+ err := term .Shutdown (t .Context ())
195+ if gotTimeout := errors .Is (err , context .DeadlineExceeded ); gotTimeout != test .expectWait {
196+ t .Fatalf ("Expected timeout error from waiting for checkpoint to catch up: %v, got timeout: %v, err: %v" , test .expectWait , gotTimeout , err )
197+ }
198+ })
199+ }
200+ }
201+
202+ func TestAddUpdatesWantTreeSize (t * testing.T ) {
203+ wantIdx := uint64 (5 )
154204 term := & terminator {
155- readCheckpoint : func (ctx context.Context ) ([] byte , error ) {
156- // simulate a call that doesn't finish, just block or wait
157- <- ctx . Done ()
158- return nil , ctx . Err ()
205+ delegate : func (_ context.Context , _ * Entry ) IndexFuture {
206+ return func () ( Index , error ) {
207+ return Index { Index : wantIdx }, nil
208+ }
159209 },
160210 }
161- term .largestIssued .Store (1 )
162- term .shutdownTimeout = 10 * time .Millisecond
163211
164- err := term .Shutdown ( context . Background () )
165- if err = = nil {
166- t .Fatal ("Expected timeout error, got nil" )
212+ f := term .Add ( t . Context (), nil )
213+ if _ , err := f (); err ! = nil {
214+ t .Fatal (err )
167215 }
168- if ! strings .Contains (err .Error (), "deadline exceeded" ) && ! strings .Contains (err .Error (), "context canceled" ) {
169- t .Fatalf ("Expected context deadline exceeded or canceled error, got: %v" , err )
216+
217+ if got := term .wantTreeSize .Load (); got != wantIdx + 1 {
218+ t .Fatalf ("wantTreeSize should be %d after adding index %d, got %d" , wantIdx + 1 , wantIdx , got )
170219 }
171220}
221+
0 commit comments