okami.mom/albedo
git clone https://git.nkp.pet/okami.mom/albedo
2f6697a
main
1// Polling a completed action must retain the original deadline and cancellation. 2// A controlled peer gates delivery so these boundaries can be exercised reliably. 3package daemon 4 5import ( 6"context" 7"fmt" 8"net/http" 9"testing" 10"time" 11) 12 13func TestCompletedActionsRetainTheirDeadlineAndCallerCancellation (t * testing.T ) { 14cases := []struct { 15name ,path ,body string 16status int 17long bool 18invoke func (context.Context ,* Connection )error 19 }{ 20 {"reload" ,"/sessions/s/reload" ,`{"session":{"state":"applied","loaded_revision":"loaded-current","restart_required":false,"warnings":[],"failure":null},"models":[],"cache_policy":{"state":"refreshed","failure":null}}` ,200 ,true ,func (ctx context.Context ,conn * Connection )error { 21_ ,err := ReloadSession (ctx ,conn ,"s" ,ReloadRequest {Target :"both" }) 22return err 23 }}, 24 {"upgrade" ,"/sessions/s/kernel/upgrade" ,`{"old_build":null,"new_build":null,"state":"unchanged","stopped_jobs":[],"warnings":[],"failure":null,"old_kernel_id":null,"new_kernel_id":null}` ,200 ,true ,func (ctx context.Context ,conn * Connection )error {_ ,err := UpgradeKernel (ctx ,conn ,"s" );return err }}, 25 {"compaction" ,"/sessions/s/compaction" ,`{"selection_applied":false,"effective_strategy":null,"state":"unchanged","observation":null,"failure":null}` ,200 ,true ,func (ctx context.Context ,conn * Connection )error { 26_ ,err := CompactSession (ctx ,conn ,"s" ,"" ) 27return err 28 }}, 29 {"shutdown" ,"/server/shutdown" ,`{"instance_id":"instance","state":"draining"}` ,202 ,false ,StopDaemon }, 30 } 31for _ ,test := range cases { 32for _ ,shorter := range []bool {false ,true } { 33t .Run (fmt .Sprintf ("%s/shorter=%t" ,test .name ,shorter ),func (t * testing.T ) { 34ctx := t .Context () 35if shorter { 36var cancel context.CancelFunc 37ctx ,cancel = context .WithTimeout (ctx ,time .Second ) 38defer cancel () 39 } 40transport := captureTransport {entered :make (chan * http.Request ),release :make (chan struct {}),body :test .body ,status :test .status } 41conn := NewConnection (ConnectionSnapshot {Port :12345 ,Token :"token" ,Version :ProtocolVersion ,InstanceID :"instance" },nil ) 42conn .HTTPClient ().Transport = transport 43finished := make (chan error ,1 ) 44go func () {finished <- test .invoke (ctx ,conn ) }() 45request := <- transport .entered 46deadline ,ok := request .Context ().Deadline () 47close (transport .release ) 48if err := <- finished ;err != nil { 49t .Fatal (err ) 50 } 51if ! ok || request .URL .Path != test .path || request .GetBody != nil { 52t .Fatalf ("wrong action transport contract: %s deadline=%t replay=%t" ,request .URL .Path ,ok ,request .GetBody != nil ) 53 } 54if shorter { 55parentDeadline ,_ := ctx .Deadline () 56if ! deadline .Equal (parentDeadline ) { 57t .Fatal ("action extended the caller deadline" ) 58 } 59 }else if remaining := time .Until (deadline );test .long && remaining < 185 * time .Second || ! test .long && remaining > 20 * time .Second { 60t .Fatalf ("action used wrong deadline budget: %s" ,remaining ) 61 } 62 }) 63 } 64 } 65}