@@ -17,7 +17,7 @@ import (
1717)
1818
1919func TestAgentAuthRecycledVMTriggersDeleteOnce (t * testing.T ) {
20- vmClient := & vmDeleterStub {}
20+ vmClient := & vmDeleterStub {ch : make ( chan struct {}, 1 ) }
2121 handler := & InternalHostHandler {
2222 logger : slog .New (slog .NewTextHandler (io .Discard , nil )),
2323 getAgentToken : func (context.Context , string ) (string , error ) { return "" , redis .Nil },
@@ -34,23 +34,23 @@ func TestAgentAuthRecycledVMTriggersDeleteOnce(t *testing.T) {
3434 vmDeleter : vmClient ,
3535 limiter : & setNXLimiterStub {result : true },
3636 skipSoftDelete : func (ctx context.Context ) context.Context { return ctx },
37- runAsync : func (fn func ()) { fn () },
3837 }
3938
4039 _ , err := handler .agentAuth (context .Background (), "agent_1" , "machine-1" )
4140 if ! errors .Is (err , errAgentVMRecycled ) {
4241 t .Fatalf ("agent auth error = %v, want %v" , err , errAgentVMRecycled )
4342 }
44- if len (vmClient .reqs ) != 1 {
43+ reqs := vmClient .waitReqs (t , time .Second )
44+ if len (reqs ) != 1 {
4545 t .Fatalf ("delete calls = %d, want 1" , len (vmClient .reqs ))
4646 }
47- if vmClient . reqs [0 ].ID != "env_1" {
48- t .Fatalf ("delete env id = %q, want env_1" , vmClient . reqs [0 ].ID )
47+ if reqs [0 ].ID != "env_1" {
48+ t .Fatalf ("delete env id = %q, want env_1" , reqs [0 ].ID )
4949 }
5050}
5151
5252func TestAgentAuthRecycledVMLimitedSkipsDelete (t * testing.T ) {
53- vmClient := & vmDeleterStub {}
53+ vmClient := & vmDeleterStub {ch : make ( chan struct {}, 1 ) }
5454 handler := & InternalHostHandler {
5555 logger : slog .New (slog .NewTextHandler (io .Discard , nil )),
5656 getAgentToken : func (context.Context , string ) (string , error ) { return "" , redis .Nil },
@@ -67,20 +67,19 @@ func TestAgentAuthRecycledVMLimitedSkipsDelete(t *testing.T) {
6767 vmDeleter : vmClient ,
6868 limiter : & setNXLimiterStub {result : false },
6969 skipSoftDelete : func (ctx context.Context ) context.Context { return ctx },
70- runAsync : func (fn func ()) { fn () },
7170 }
7271
7372 _ , err := handler .agentAuth (context .Background (), "agent_2" , "machine-2" )
7473 if ! errors .Is (err , errAgentVMRecycled ) {
7574 t .Fatalf ("agent auth error = %v, want %v" , err , errAgentVMRecycled )
7675 }
77- if len ( vmClient .reqs ) != 0 {
76+ if vmClient .hasReqWithin ( 50 * time . Millisecond ) {
7877 t .Fatalf ("delete calls = %d, want 0" , len (vmClient .reqs ))
7978 }
8079}
8180
8281func TestAgentAuthSoftDeletedRecycledVMStillTriggersDelete (t * testing.T ) {
83- vmClient := & vmDeleterStub {}
82+ vmClient := & vmDeleterStub {ch : make ( chan struct {}, 1 ) }
8483 skipCalled := false
8584 type testSkipMarkerKey struct {}
8685 markerKey := testSkipMarkerKey {}
@@ -107,7 +106,6 @@ func TestAgentAuthSoftDeletedRecycledVMStillTriggersDelete(t *testing.T) {
107106 skipCalled = true
108107 return context .WithValue (ctx , markerKey , markerValue )
109108 },
110- runAsync : func (fn func ()) { fn () },
111109 }
112110
113111 _ , err := handler .agentAuth (context .Background (), "agent_deleted" , "machine-deleted" )
@@ -117,7 +115,7 @@ func TestAgentAuthSoftDeletedRecycledVMStillTriggersDelete(t *testing.T) {
117115 if ! skipCalled {
118116 t .Fatal ("expected skipSoftDelete to be called" )
119117 }
120- if len (vmClient .reqs ) != 1 {
118+ if len (vmClient .waitReqs ( t , time . Second ) ) != 1 {
121119 t .Fatalf ("delete calls = %d, want 1" , len (vmClient .reqs ))
122120 }
123121}
@@ -129,6 +127,14 @@ type internalHostRepoStub struct {
129127 skipMarkerValue string
130128}
131129
130+ func (s * internalHostRepoStub ) List (context.Context , uuid.UUID ) ([]* db.Host , error ) {
131+ return nil , errors .New ("not implemented" )
132+ }
133+
134+ func (s * internalHostRepoStub ) GetHost (context.Context , uuid.UUID , string ) (* domain.Host , error ) {
135+ return nil , errors .New ("not implemented" )
136+ }
137+
132138func (s * internalHostRepoStub ) UpsertHost (context.Context , * taskflow.Host ) error {
133139 return nil
134140}
@@ -162,6 +168,38 @@ func (s *internalHostRepoStub) GetVirtualMachineByEnvID(context.Context, string)
162168 return nil , errors .New ("vm not found" )
163169}
164170
171+ func (s * internalHostRepoStub ) GetVirtualMachineWithUser (context.Context , uuid.UUID , string ) (* db.VirtualMachine , error ) {
172+ return nil , errors .New ("vm not found" )
173+ }
174+
175+ func (s * internalHostRepoStub ) CreateVirtualMachine (context.Context , * domain.User , * domain.CreateVMReq , func (context.Context ) (string , error ), func (* db.Model , * db.Image ) (* domain.VirtualMachine , error )) (* domain.VirtualMachine , error ) {
176+ return nil , errors .New ("not implemented" )
177+ }
178+
179+ func (s * internalHostRepoStub ) PastHourVirtualMachine (context.Context ) ([]* db.VirtualMachine , error ) {
180+ return nil , errors .New ("not implemented" )
181+ }
182+
183+ func (s * internalHostRepoStub ) AllCountDownVirtualMachine (context.Context ) ([]* db.VirtualMachine , error ) {
184+ return nil , errors .New ("not implemented" )
185+ }
186+
187+ func (s * internalHostRepoStub ) DeleteVirtualMachine (context.Context , uuid.UUID , string , string , func (* db.VirtualMachine ) error ) error {
188+ return errors .New ("not implemented" )
189+ }
190+
191+ func (s * internalHostRepoStub ) DeleteHost (context.Context , uuid.UUID , string ) error {
192+ return errors .New ("not implemented" )
193+ }
194+
195+ func (s * internalHostRepoStub ) UpdateHost (context.Context , uuid.UUID , * domain.UpdateHostReq ) error {
196+ return errors .New ("not implemented" )
197+ }
198+
199+ func (s * internalHostRepoStub ) UpdateVM (context.Context , domain.UpdateVMReq , func (* db.VirtualMachine ) error ) (* db.VirtualMachine , int64 , error ) {
200+ return nil , 0 , errors .New ("not implemented" )
201+ }
202+
165203func (s * internalHostRepoStub ) GetGitCredentialByTask (context.Context , string ) (* domain.GitCredentialInfo , error ) {
166204 return nil , errors .New ("task not found" )
167205}
@@ -182,10 +220,37 @@ func (s *setNXLimiterStub) SetNX(_ context.Context, key string, _ interface{}, t
182220type vmDeleterStub struct {
183221 reqs []* taskflow.DeleteVirtualMachineReq
184222 err error
223+ ch chan struct {}
185224}
186225
187226func (s * vmDeleterStub ) Delete (_ context.Context , req * taskflow.DeleteVirtualMachineReq ) error {
188227 cp := * req
189228 s .reqs = append (s .reqs , & cp )
229+ if s .ch != nil {
230+ select {
231+ case s .ch <- struct {}{}:
232+ default :
233+ }
234+ }
190235 return s .err
191236}
237+
238+ func (s * vmDeleterStub ) waitReqs (t * testing.T , timeout time.Duration ) []* taskflow.DeleteVirtualMachineReq {
239+ t .Helper ()
240+ select {
241+ case <- s .ch :
242+ return s .reqs
243+ case <- time .After (timeout ):
244+ t .Fatal ("timed out waiting for delete call" )
245+ return nil
246+ }
247+ }
248+
249+ func (s * vmDeleterStub ) hasReqWithin (timeout time.Duration ) bool {
250+ select {
251+ case <- s .ch :
252+ return true
253+ case <- time .After (timeout ):
254+ return false
255+ }
256+ }
0 commit comments