Skip to content

Commit 3fc08ae

Browse files
AchoArnoldCopilot
andcommitted
test: retry inbound receive while phone api key associates
The phone-to-API-key association runs asynchronously via the phone_api_key event listener, so a freshly provisioned phone can return 401 on receive until the auth cache clears. Retry receiveInbound on that transient 401 instead of failing immediately. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: bf78429e-573c-406c-9f7c-1d6e1bddbbb5
1 parent f355239 commit 3fc08ae

1 file changed

Lines changed: 26 additions & 12 deletions

File tree

tests/unarchive_thread_integration_test.go

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ func setUnarchiveThread(ctx context.Context, t *testing.T, phoneNumber string, e
5454
}
5555

5656
// receiveInbound submits an inbound message as the phone and returns the message ID.
57+
// The phone-to-API-key association is applied asynchronously after setupPhone (via
58+
// the phone_api_key event listener), so a freshly provisioned phone can briefly return
59+
// 401 until the auth cache clears. Retry on that transient authorization failure.
5760
func receiveInbound(ctx context.Context, t *testing.T, phoneAPIKey, from, to, content string, ts time.Time) string {
5861
t.Helper()
5962

@@ -67,18 +70,29 @@ func receiveInbound(ctx context.Context, t *testing.T, phoneAPIKey, from, to, co
6770
body, err := json.Marshal(payload)
6871
require.NoError(t, err)
6972

70-
req, err := http.NewRequestWithContext(ctx, http.MethodPost, apiBaseURL+"/v1/messages/receive", bytes.NewReader(body))
71-
require.NoError(t, err)
72-
req.Header.Set("Content-Type", "application/json")
73-
req.Header.Set("x-api-key", phoneAPIKey)
74-
75-
resp, err := http.DefaultClient.Do(req)
76-
require.NoError(t, err)
77-
defer resp.Body.Close()
78-
79-
respBody, err := io.ReadAll(resp.Body)
80-
require.NoError(t, err)
81-
require.Equal(t, http.StatusOK, resp.StatusCode, "receive failed: %s", string(respBody))
73+
var respBody []byte
74+
deadline := time.Now().Add(20 * time.Second)
75+
for {
76+
req, err := http.NewRequestWithContext(ctx, http.MethodPost, apiBaseURL+"/v1/messages/receive", bytes.NewReader(body))
77+
require.NoError(t, err)
78+
req.Header.Set("Content-Type", "application/json")
79+
req.Header.Set("x-api-key", phoneAPIKey)
80+
81+
resp, err := http.DefaultClient.Do(req)
82+
require.NoError(t, err)
83+
respBody, err = io.ReadAll(resp.Body)
84+
resp.Body.Close()
85+
require.NoError(t, err)
86+
87+
if resp.StatusCode == http.StatusOK {
88+
break
89+
}
90+
if resp.StatusCode == http.StatusUnauthorized && time.Now().Before(deadline) {
91+
time.Sleep(500 * time.Millisecond)
92+
continue
93+
}
94+
require.Equal(t, http.StatusOK, resp.StatusCode, "receive failed: %s", string(respBody))
95+
}
8296

8397
var result httpsms.MessageResponse
8498
require.NoError(t, json.Unmarshal(respBody, &result))

0 commit comments

Comments
 (0)