71 lines
2.7 KiB
Go
71 lines
2.7 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
"time"
|
|
|
|
agentpb "git.ipao.vip/rogee/go-sip/gen/agent"
|
|
"git.ipao.vip/rogee/go-sip/internal/agent"
|
|
"git.ipao.vip/rogee/go-sip/internal/config"
|
|
)
|
|
|
|
func TestExplicitUploadRetryIsOneNewRequestAndOnePUT(t *testing.T) {
|
|
puts := 0
|
|
server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
puts++
|
|
if _, err := io.Copy(io.Discard, r.Body); err != nil {
|
|
t.Error(err)
|
|
}
|
|
w.WriteHeader(http.StatusOK)
|
|
}))
|
|
defer server.Close()
|
|
root := t.TempDir()
|
|
path := filepath.Join(root, "audio.wav")
|
|
if err := os.WriteFile(path, []byte("audio"), 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
sum := sha256.Sum256([]byte("audio"))
|
|
binding := &agentpb.ExecutionBinding{TenantId: "tenant-a", TenantKey: "tenant-a", ExecutionId: "execution-a", CallId: "call-a"}
|
|
asset := &agentpb.AssetDescriptor{AssetId: "recording-a", SizeBytes: 5, ChecksumSha256: hex.EncodeToString(sum[:])}
|
|
remote := &uploadRPCStub{grant: &agentpb.UploadGrant{ObjectKey: "recording-a", Bucket: "mock-bucket", TargetUrl: server.URL, MaxBytes: 5, RequiredChecksumSha256: asset.ChecksumSha256, ExpiresAtUnixMs: time.Now().Add(-time.Minute).UnixMilli()}}
|
|
spool, err := agent.NewSpool(root, nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
uploader := agent.UploadClient{HTTPClient: server.Client()}
|
|
if _, err := uploadRecording(context.Background(), config.Config{}, remote, uploader, spool, binding, asset, path); err == nil {
|
|
t.Fatal("expired token accepted")
|
|
}
|
|
if puts != 0 || remote.requests != 1 {
|
|
t.Fatal("expired grant caused PUT or auto renewal")
|
|
}
|
|
remote.grant.ExpiresAtUnixMs = time.Now().Add(15 * time.Minute).UnixMilli()
|
|
id := stableUploadID(binding, asset)
|
|
if _, err := uploadRecording(context.Background(), config.Config{}, remote, uploader, spool, binding, asset, path); err == nil {
|
|
t.Fatal("ordinary recovery retried a failed attempt")
|
|
}
|
|
if _, err := retryUploadRecording(context.Background(), config.Config{}, remote, uploader, spool, id, "11111111-1111-4111-8111-111111111111", path); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if puts != 1 || remote.requests != 2 || remote.notifications != 1 {
|
|
t.Fatalf("PUT=%d grants=%d notifications=%d", puts, remote.requests, remote.notifications)
|
|
}
|
|
if _, err := retryUploadRecording(context.Background(), config.Config{}, remote, uploader, spool, id, "22222222-2222-4222-8222-222222222222", path); err == nil {
|
|
t.Fatal("completed upload retried")
|
|
}
|
|
if puts != 1 || remote.requests != 2 {
|
|
t.Fatal("duplicate retry repeated effects")
|
|
}
|
|
if _, err := os.Stat(path); err != nil {
|
|
t.Fatal("retry removed source file")
|
|
}
|
|
}
|