Skip to content

Commit 7c445a0

Browse files
committed
test: tolerate transient actor resume routing
1 parent aef0645 commit 7c445a0

1 file changed

Lines changed: 51 additions & 29 deletions

File tree

internal/e2e/suites/demo/demo_test.go

Lines changed: 51 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ import (
1919
"fmt"
2020
"io"
2121
"net/http"
22-
"strings"
22+
"regexp"
23+
"strconv"
2324
"testing"
2425
"time"
2526

@@ -152,13 +153,7 @@ func pauseActor(ctx context.Context, t *testing.T, clients *e2e.Clients, nsObj *
152153
}
153154
waitForActorStatus(ctx, t, clients, actorID, ateapipb.Actor_STATUS_RUNNING)
154155

155-
resp, err := callActor(t, actorID)
156-
if err != nil {
157-
t.Fatalf("failed to call actor: %v", err)
158-
}
159-
if !strings.Contains(resp, "preserved memory count: 1") {
160-
t.Fatalf("expected count 1, got response: %s", resp)
161-
}
156+
callActorUntilCountAtLeast(t, actorID, 1)
162157

163158
// Pausing the actor
164159
t.Logf("Pausing Actor %q...", actorID)
@@ -178,13 +173,7 @@ func pauseActor(ctx context.Context, t *testing.T, clients *e2e.Clients, nsObj *
178173
}
179174
waitForActorStatus(ctx, t, clients, actorID, ateapipb.Actor_STATUS_RUNNING)
180175

181-
resp, err = callActor(t, actorID)
182-
if err != nil {
183-
t.Fatalf("failed to call actor again: %v", err)
184-
}
185-
if !strings.Contains(resp, "preserved memory count: 2") {
186-
t.Fatalf("expected count 2, got response: %s", resp)
187-
}
176+
callActorUntilCountAtLeast(t, actorID, 2)
188177

189178
// Suspending the actor before deletion
190179
t.Logf("Suspending Actor %q before deletion...", actorID)
@@ -235,13 +224,7 @@ func suspendActor(ctx context.Context, t *testing.T, clients *e2e.Clients, nsObj
235224
}
236225
waitForActorStatus(ctx, t, clients, actorID, ateapipb.Actor_STATUS_RUNNING)
237226

238-
resp, err := callActor(t, actorID)
239-
if err != nil {
240-
t.Fatalf("failed to call actor: %v", err)
241-
}
242-
if !strings.Contains(resp, "preserved memory count: 1") {
243-
t.Fatalf("expected count 1, got response: %s", resp)
244-
}
227+
callActorUntilCountAtLeast(t, actorID, 1)
245228

246229
// Suspending the actor
247230
t.Logf("Suspending Actor %q...", actorID)
@@ -261,13 +244,7 @@ func suspendActor(ctx context.Context, t *testing.T, clients *e2e.Clients, nsObj
261244
}
262245
waitForActorStatus(ctx, t, clients, actorID, ateapipb.Actor_STATUS_RUNNING)
263246

264-
resp, err = callActor(t, actorID)
265-
if err != nil {
266-
t.Fatalf("failed to call actor again: %v", err)
267-
}
268-
if !strings.Contains(resp, "preserved memory count: 2") {
269-
t.Fatalf("expected count 2, got response: %s", resp)
270-
}
247+
callActorUntilCountAtLeast(t, actorID, 2)
271248

272249
// Suspending the actor before deletion
273250
t.Logf("Suspending Actor %q before deletion...", actorID)
@@ -404,6 +381,51 @@ func waitForActorStatus(ctx context.Context, t *testing.T, clients *e2e.Clients,
404381
t.Fatalf("timed out waiting for actor %q to reach status %v", actorID, expectedStatus)
405382
}
406383

384+
var preservedCountRe = regexp.MustCompile(`preserved memory count: ([0-9]+)`)
385+
386+
func callActorUntilCountAtLeast(t *testing.T, actorID string, minCount int) string {
387+
t.Helper()
388+
389+
var lastErr error
390+
var lastResp string
391+
deadline := time.Now().Add(20 * time.Second)
392+
for time.Now().Before(deadline) {
393+
resp, err := callActor(t, actorID)
394+
if err != nil {
395+
lastErr = err
396+
} else {
397+
lastResp = resp
398+
count, err := preservedCount(resp)
399+
if err != nil {
400+
lastErr = err
401+
} else if count >= minCount {
402+
return resp
403+
} else {
404+
lastErr = fmt.Errorf("expected preserved memory count >= %d, got %d in response: %s", minCount, count, resp)
405+
}
406+
}
407+
time.Sleep(500 * time.Millisecond)
408+
}
409+
410+
if lastResp != "" {
411+
t.Fatalf("timed out calling actor %q; last response: %s; last error: %v", actorID, lastResp, lastErr)
412+
}
413+
t.Fatalf("timed out calling actor %q; last error: %v", actorID, lastErr)
414+
return ""
415+
}
416+
417+
func preservedCount(resp string) (int, error) {
418+
matches := preservedCountRe.FindStringSubmatch(resp)
419+
if matches == nil {
420+
return 0, fmt.Errorf("response does not include preserved memory count: %s", resp)
421+
}
422+
count, err := strconv.Atoi(matches[1])
423+
if err != nil {
424+
return 0, fmt.Errorf("parse preserved memory count %q: %w", matches[1], err)
425+
}
426+
return count, nil
427+
}
428+
407429
func callActor(t *testing.T, actorID string) (string, error) {
408430
t.Helper()
409431
clients := e2e.GetClients()

0 commit comments

Comments
 (0)