Skip to content

Commit 70af9af

Browse files
committed
test: tolerate transient actor resume routing
1 parent cba5c0a commit 70af9af

1 file changed

Lines changed: 51 additions & 17 deletions

File tree

internal/e2e/suites/demo/demo_test.go

Lines changed: 51 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import (
2020
"io"
2121
"net/http"
2222
"os"
23+
"regexp"
24+
"strconv"
2325
"strings"
2426
"testing"
2527
"time"
@@ -330,11 +332,7 @@ func pauseActor(ctx context.Context, t *testing.T, clients *e2e.Clients, nsObj *
330332
}
331333
waitForActorStatus(ctx, t, clients, actorID, ateapipb.Actor_STATUS_RUNNING)
332334

333-
resp, err := callActor(t, demoAtespace, actorID)
334-
if err != nil {
335-
t.Fatalf("failed to call actor: %v", err)
336-
}
337-
335+
resp := callActorUntilCountAtLeast(t, demoAtespace, actorID, 1)
338336
if isMicroVMEnvironment() {
339337
validateCounterResponse(t, resp, "after creation", 1, -1)
340338
} else {
@@ -359,10 +357,7 @@ func pauseActor(ctx context.Context, t *testing.T, clients *e2e.Clients, nsObj *
359357
}
360358
waitForActorStatus(ctx, t, clients, actorID, ateapipb.Actor_STATUS_RUNNING)
361359

362-
resp, err = callActor(t, demoAtespace, actorID)
363-
if err != nil {
364-
t.Fatalf("failed to call actor again: %v", err)
365-
}
360+
resp = callActorUntilCountAtLeast(t, demoAtespace, actorID, 2)
366361
if isMicroVMEnvironment() {
367362
validateCounterResponse(t, resp, "after pause", 2, -1)
368363
} else {
@@ -418,10 +413,7 @@ func suspendActor(ctx context.Context, t *testing.T, clients *e2e.Clients, nsObj
418413
}
419414
waitForActorStatus(ctx, t, clients, actorID, ateapipb.Actor_STATUS_RUNNING)
420415

421-
resp, err := callActor(t, demoAtespace, actorID)
422-
if err != nil {
423-
t.Fatalf("failed to call actor: %v", err)
424-
}
416+
resp := callActorUntilCountAtLeast(t, demoAtespace, actorID, 1)
425417
if isMicroVMEnvironment() {
426418
validateCounterResponse(t, resp, "after creation", 1, -1)
427419
} else {
@@ -446,10 +438,7 @@ func suspendActor(ctx context.Context, t *testing.T, clients *e2e.Clients, nsObj
446438
}
447439
waitForActorStatus(ctx, t, clients, actorID, ateapipb.Actor_STATUS_RUNNING)
448440

449-
resp, err = callActor(t, demoAtespace, actorID)
450-
if err != nil {
451-
t.Fatalf("failed to call actor again: %v", err)
452-
}
441+
resp = callActorUntilCountAtLeast(t, demoAtespace, actorID, 2)
453442
if isMicroVMEnvironment() {
454443
validateCounterResponse(t, resp, "after suspend", 2, -1)
455444
} else {
@@ -619,6 +608,51 @@ func waitForActorStatus(ctx context.Context, t *testing.T, clients *e2e.Clients,
619608
t.Fatalf("timed out waiting for actor %q to reach status %v", actorID, expectedStatus)
620609
}
621610

611+
var preservedCountRe = regexp.MustCompile(`preserved memory count: ([0-9]+)`)
612+
613+
func callActorUntilCountAtLeast(t *testing.T, atespace, actorID string, minCount int) string {
614+
t.Helper()
615+
616+
var lastErr error
617+
var lastResp string
618+
deadline := time.Now().Add(20 * time.Second)
619+
for time.Now().Before(deadline) {
620+
resp, err := callActor(t, atespace, actorID)
621+
if err != nil {
622+
lastErr = err
623+
} else {
624+
lastResp = resp
625+
count, err := preservedCount(resp)
626+
if err != nil {
627+
lastErr = err
628+
} else if count >= minCount {
629+
return resp
630+
} else {
631+
lastErr = fmt.Errorf("expected preserved memory count >= %d, got %d in response: %s", minCount, count, resp)
632+
}
633+
}
634+
time.Sleep(500 * time.Millisecond)
635+
}
636+
637+
if lastResp != "" {
638+
t.Fatalf("timed out calling actor %q; last response: %s; last error: %v", actorID, lastResp, lastErr)
639+
}
640+
t.Fatalf("timed out calling actor %q; last error: %v", actorID, lastErr)
641+
return ""
642+
}
643+
644+
func preservedCount(resp string) (int, error) {
645+
matches := preservedCountRe.FindStringSubmatch(resp)
646+
if matches == nil {
647+
return 0, fmt.Errorf("response does not include preserved memory count: %s", resp)
648+
}
649+
count, err := strconv.Atoi(matches[1])
650+
if err != nil {
651+
return 0, fmt.Errorf("parse preserved memory count %q: %w", matches[1], err)
652+
}
653+
return count, nil
654+
}
655+
622656
func callActor(t *testing.T, atespace, actorID string) (string, error) {
623657
t.Helper()
624658

0 commit comments

Comments
 (0)