71 lines
3.0 KiB
Go
71 lines
3.0 KiB
Go
package rpc
|
|
|
|
import (
|
|
"context"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
agentpb "git.ipao.vip/rogee/go-sip/gen/agent"
|
|
ossclient "git.ipao.vip/rogee/go-sip/internal/oss"
|
|
"git.ipao.vip/rogee/go-sip/internal/store"
|
|
"google.golang.org/grpc/codes"
|
|
"google.golang.org/grpc/status"
|
|
)
|
|
|
|
func TestUploadFactWaitsOnlyForMQPublication(t *testing.T) {
|
|
st, err := store.Open(filepath.Join(t.TempDir(), "dispatcher.db"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer st.Close()
|
|
if err := st.BindDispatcherID("11111111-1111-4111-8111-111111111111"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
client, err := ossclient.NewClient(ossclient.Config{Endpoint: "https://oss.invalid", Region: "cn-beijing", Bucket: "local-test", AccessKeyID: "local-test", AccessKeySecret: "local-test", GrantTTL: 15 * time.Minute, MaxAssetBytes: 1024})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
now := time.Now().UTC()
|
|
server, err := NewDispatcherUploadServer(st, client, func() time.Time { return now }, false)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
meta := &agentpb.RequestMeta{AgentId: "agent-a", CellId: "cell-a", OperationId: "op-a", IdempotencyKey: "key-a", TraceId: "trace-a"}
|
|
binding := &agentpb.ExecutionBinding{TenantId: "tenant-a", TenantKey: "tenant-a", TaskId: "task-a", ExecutionId: "execution-a", CallId: "call-a"}
|
|
asset := &agentpb.AssetDescriptor{Kind: agentpb.AssetKind_ASSET_KIND_RECORDING, AssetId: "recording-a", CallId: "call-a", Format: "wav", Channels: 1, SampleRateHz: 8000, DurationMs: 10, SizeBytes: 4, ChecksumSha256: strings.Repeat("a", 64)}
|
|
grant, err := server.RequestUpload(context.Background(), &agentpb.RequestUploadRequest{Meta: meta, Binding: binding, Asset: asset, UploadId: "upload-a"})
|
|
if err != nil || grant.GetGrant() == nil {
|
|
t.Fatalf("grant: %v", err)
|
|
}
|
|
// A delayed notification is not a request for a fresh PUT token.
|
|
now = now.Add(20 * time.Minute)
|
|
request := &agentpb.CompleteUploadRequest{Meta: meta, Binding: binding, Asset: asset, UploadId: "upload-a", UploadedSizeBytes: 4, UploadedChecksumSha256: asset.ChecksumSha256}
|
|
for i := 0; i < 2; i++ {
|
|
_, err := server.CompleteUpload(context.Background(), request)
|
|
if status.Code(err) != codes.Unavailable {
|
|
t.Fatalf("unpublished notification must remain pending, got %v", err)
|
|
}
|
|
}
|
|
var id int64
|
|
var count int
|
|
if err := st.DB().QueryRow(`SELECT COUNT(*), MIN(id) FROM outbox`).Scan(&count, &id); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if count != 1 {
|
|
t.Fatalf("notifications=%d", count)
|
|
}
|
|
if err := st.MarkOutboxPublished(id); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
completed, err := server.CompleteUpload(context.Background(), request)
|
|
if err != nil || completed.GetState() != agentpb.UploadState_UPLOAD_STATE_COMPLETED {
|
|
t.Fatalf("completion: %v %v", completed, err)
|
|
}
|
|
again, err := server.RequestUpload(context.Background(), &agentpb.RequestUploadRequest{Meta: meta, Binding: binding, Asset: asset, UploadId: "upload-a"})
|
|
if err != nil || again.GetGrant() != nil || again.GetState() != agentpb.UploadState_UPLOAD_STATE_COMPLETED {
|
|
t.Fatal("completed upload was reauthorized")
|
|
}
|
|
}
|