@@ -18,6 +18,7 @@ import (
1818 "gopkg.d7z.net/cache-proxy/pkg/config"
1919 "gopkg.d7z.net/cache-proxy/pkg/proxy/file"
2020 proxyruntime "gopkg.d7z.net/cache-proxy/pkg/runtime"
21+ "gopkg.d7z.net/cache-proxy/pkg/scheduler"
2122)
2223
2324func TestValidateRejectsConflictingPaths (t * testing.T ) {
@@ -71,7 +72,6 @@ func TestFileProxyCachesImmutableObjects(t *testing.T) {
7172 require .Equal (t , int64 (1 ), upstreamRequests .Load ())
7273}
7374
74-
7575func TestMetricsRequireBearerToken (t * testing.T ) {
7676 ctx , cancel := context .WithTimeout (context .Background (), 10 * time .Second )
7777 defer cancel ()
@@ -247,7 +247,6 @@ instances:
247247 require .ErrorContains (t , err , "field default_polciy not found" )
248248}
249249
250-
251250func TestAppCloseRespectsContextWhenHandlerStopBlocks (t * testing.T ) {
252251 app := & App {
253252 stopRuntime : func () {},
@@ -419,6 +418,29 @@ func TestBindHomePageHeadReturnsOK(t *testing.T) {
419418 require .Equal (t , "text/html; charset=utf-8" , rec .Header ().Get ("Content-Type" ))
420419}
421420
421+ func TestOpenStopsSchedulerWhenPrepareHandlersFails (t * testing.T ) {
422+ ctx , cancel := context .WithTimeout (context .Background (), 10 * time .Second )
423+ defer cancel ()
424+
425+ var runs atomic.Int32
426+ prev := driverSet
427+ driverSet = func () map [string ]proxyruntime.ModeDriver {
428+ drivers := prev ()
429+ drivers [config .ModeFile ] = startFailingDriver {runs : & runs }
430+ return drivers
431+ }
432+ defer func () { driverSet = prev }()
433+
434+ doc := testDocument (t .TempDir (), []config.Instance {
435+ fileInstance (t , "files" , "/files" , "https://example.com" , file.Policy {}),
436+ })
437+
438+ _ , err := Open (ctx , doc , "" )
439+ require .ErrorContains (t , err , "boom" )
440+ first := runs .Load ()
441+ time .Sleep (200 * time .Millisecond )
442+ require .Equal (t , first , runs .Load ())
443+ }
422444
423445func openApp (t * testing.T , ctx context.Context , doc * config.Document ) * App {
424446 return openAppWithConfig (t , ctx , doc , "" )
@@ -594,3 +616,25 @@ func (s *cleanupContextInstance) Stop(context.Context) error {
594616 s .stopped .Store (true )
595617 return s .ctx .Err ()
596618}
619+
620+ type startFailingDriver struct { runs * atomic.Int32 }
621+
622+ func (startFailingDriver ) Mode () string { return config .ModeFile }
623+
624+ func (d startFailingDriver ) Plan (_ context.Context , plan * proxyruntime.InstancePlan ) error {
625+ plan .Scheduler ().Register (scheduler.TaskDef {
626+ Key : scheduler .NewTaskKey (plan .Name (), scheduler .TypeExpireCleanup , "" ),
627+ Interval : 10 * time .Millisecond ,
628+ Handler : func (context.Context ) error {
629+ if d .runs != nil {
630+ d .runs .Add (1 )
631+ }
632+ return nil
633+ },
634+ })
635+ return plan .BindPath ("/files" , config .Expiration (time .Hour ), startContextInstance {
636+ onStart : func (context.Context ) error {
637+ return fmt .Errorf ("boom" )
638+ },
639+ })
640+ }
0 commit comments