@@ -20,6 +20,8 @@ import (
2020 "io"
2121 "net/http"
2222 "os"
23+ "regexp"
24+ "strconv"
2325 "strings"
2426 "testing"
2527 "time"
@@ -273,24 +275,14 @@ func createActor(ctx context.Context, t *testing.T, clients *e2e.Clients, nsObj
273275 })
274276 }()
275277
276- listResp , err := clients .SubstrateAPI .ListActors (ctx , & ateapipb.ListActorsRequest {Atespace : demoAtespace })
278+ getResp , err := clients .SubstrateAPI .GetActor (ctx , & ateapipb.GetActorRequest {
279+ ActorRef : & ateapipb.ActorRef {Atespace : demoAtespace , Name : actorID },
280+ })
277281 if err != nil {
278- t .Fatalf ("ListActors RPC failed: %v" , err )
279- }
280-
281- var myActors []* ateapipb.Actor
282- for _ , actor := range listResp .GetActors () {
283- if actor .GetActorTemplateNamespace () == nsObj .Name && actor .GetActorId () == actorID {
284- myActors = append (myActors , actor )
285- }
282+ t .Fatalf ("GetActor RPC failed: %v" , err )
286283 }
287284
288- // Check that we have our Actor created.
289- if len (myActors ) != 1 {
290- t .Fatalf ("expected actor %s in namespace %s, got %d actors: %v" , actorID , nsObj .Name , len (myActors ), myActors )
291- }
292-
293- actor := myActors [0 ]
285+ actor := getResp .GetActor ()
294286 if actor .GetActorId () != actorID {
295287 t .Errorf ("expected actor ID %s, got %s" , actorID , actor .GetActorId ())
296288 }
@@ -301,8 +293,7 @@ func createActor(ctx context.Context, t *testing.T, clients *e2e.Clients, nsObj
301293 t .Errorf ("expected actor status to be SUSPENDED, got %v" , actor .Status )
302294 }
303295
304- t .Logf ("Successfully queried Substrate API. Found %d active actors total, %d in our namespace %s." ,
305- len (listResp .GetActors ()), len (myActors ), nsObj .Name )
296+ t .Logf ("Successfully queried Substrate API. Found actor %s in namespace %s." , actorID , nsObj .Name )
306297
307298 return nil
308299}
@@ -330,11 +321,7 @@ func pauseActor(ctx context.Context, t *testing.T, clients *e2e.Clients, nsObj *
330321 }
331322 waitForActorStatus (ctx , t , clients , actorID , ateapipb .Actor_STATUS_RUNNING )
332323
333- resp , err := callActor (t , demoAtespace , actorID )
334- if err != nil {
335- t .Fatalf ("failed to call actor: %v" , err )
336- }
337-
324+ resp := callActorUntilCountAtLeast (t , demoAtespace , actorID , 1 )
338325 if isMicroVMEnvironment () {
339326 validateCounterResponse (t , resp , "after creation" , 1 , - 1 )
340327 } else {
@@ -359,10 +346,7 @@ func pauseActor(ctx context.Context, t *testing.T, clients *e2e.Clients, nsObj *
359346 }
360347 waitForActorStatus (ctx , t , clients , actorID , ateapipb .Actor_STATUS_RUNNING )
361348
362- resp , err = callActor (t , demoAtespace , actorID )
363- if err != nil {
364- t .Fatalf ("failed to call actor again: %v" , err )
365- }
349+ resp = callActorUntilCountAtLeast (t , demoAtespace , actorID , 2 )
366350 if isMicroVMEnvironment () {
367351 validateCounterResponse (t , resp , "after pause" , 2 , - 1 )
368352 } else {
@@ -418,10 +402,7 @@ func suspendActor(ctx context.Context, t *testing.T, clients *e2e.Clients, nsObj
418402 }
419403 waitForActorStatus (ctx , t , clients , actorID , ateapipb .Actor_STATUS_RUNNING )
420404
421- resp , err := callActor (t , demoAtespace , actorID )
422- if err != nil {
423- t .Fatalf ("failed to call actor: %v" , err )
424- }
405+ resp := callActorUntilCountAtLeast (t , demoAtespace , actorID , 1 )
425406 if isMicroVMEnvironment () {
426407 validateCounterResponse (t , resp , "after creation" , 1 , - 1 )
427408 } else {
@@ -446,10 +427,7 @@ func suspendActor(ctx context.Context, t *testing.T, clients *e2e.Clients, nsObj
446427 }
447428 waitForActorStatus (ctx , t , clients , actorID , ateapipb .Actor_STATUS_RUNNING )
448429
449- resp , err = callActor (t , demoAtespace , actorID )
450- if err != nil {
451- t .Fatalf ("failed to call actor again: %v" , err )
452- }
430+ resp = callActorUntilCountAtLeast (t , demoAtespace , actorID , 2 )
453431 if isMicroVMEnvironment () {
454432 validateCounterResponse (t , resp , "after suspend" , 2 , - 1 )
455433 } else {
@@ -619,6 +597,51 @@ func waitForActorStatus(ctx context.Context, t *testing.T, clients *e2e.Clients,
619597 t .Fatalf ("timed out waiting for actor %q to reach status %v" , actorID , expectedStatus )
620598}
621599
600+ var preservedCountRe = regexp .MustCompile (`preserved memory count: ([0-9]+)` )
601+
602+ func callActorUntilCountAtLeast (t * testing.T , atespace , actorID string , minCount int ) string {
603+ t .Helper ()
604+
605+ var lastErr error
606+ var lastResp string
607+ deadline := time .Now ().Add (20 * time .Second )
608+ for time .Now ().Before (deadline ) {
609+ resp , err := callActor (t , atespace , actorID )
610+ if err != nil {
611+ lastErr = err
612+ } else {
613+ lastResp = resp
614+ count , err := preservedCount (resp )
615+ if err != nil {
616+ lastErr = err
617+ } else if count >= minCount {
618+ return resp
619+ } else {
620+ lastErr = fmt .Errorf ("expected preserved memory count >= %d, got %d in response: %s" , minCount , count , resp )
621+ }
622+ }
623+ time .Sleep (500 * time .Millisecond )
624+ }
625+
626+ if lastResp != "" {
627+ t .Fatalf ("timed out calling actor %q; last response: %s; last error: %v" , actorID , lastResp , lastErr )
628+ }
629+ t .Fatalf ("timed out calling actor %q; last error: %v" , actorID , lastErr )
630+ return ""
631+ }
632+
633+ func preservedCount (resp string ) (int , error ) {
634+ matches := preservedCountRe .FindStringSubmatch (resp )
635+ if matches == nil {
636+ return 0 , fmt .Errorf ("response does not include preserved memory count: %s" , resp )
637+ }
638+ count , err := strconv .Atoi (matches [1 ])
639+ if err != nil {
640+ return 0 , fmt .Errorf ("parse preserved memory count %q: %w" , matches [1 ], err )
641+ }
642+ return count , nil
643+ }
644+
622645func callActor (t * testing.T , atespace , actorID string ) (string , error ) {
623646 t .Helper ()
624647
0 commit comments