Files
kmsvc-sdk/longpoll_test.go
T
riotpiaoleandClaude Haiku 4.5 e4017aea9e feat: add CI/release automation and complete SDK implementation
- Add Forgejo CI workflow: gofmt checks, module caching, coverage reporting
- Add release workflow: auto-tag-triggered release with changelog extraction
- Update module paths from rock/ to homelab/ namespace
- Enhance test coverage and documentation (PLAN.md, README.md)

Co-Authored-By: Claude Haiku 4.5 <[email protected]>
2026-06-29 13:34:22 -07:00

63 lines
1.8 KiB
Go

package kmsvc
import (
"context"
"testing"
"time"
kafkamgmtv1 "forgejo.riotpiao.homelab.com/homelab/kmsvc-proto/gen/kafkamgmt/v1"
)
func TestReceiveMessageMapsResponse(t *testing.T) {
var gotReq *kafkamgmtv1.ReceiveMessageRequest
fake := &fakeQueueService{
receiveMessage: func(ctx context.Context, req *kafkamgmtv1.ReceiveMessageRequest) (*kafkamgmtv1.ReceiveMessageResponse, error) {
gotReq = req
return &kafkamgmtv1.ReceiveMessageResponse{
Messages: []*kafkamgmtv1.Message{
{MessageId: "m1", ReceiptHandle: "rh1", Body: []byte("hi"), ReceiveCount: 1},
},
}, nil
},
}
client := newTestClient(t, fake)
msgs, err := client.ReceiveMessage(context.Background(), "q", ReceiveOptions{
MaxNumberOfMessages: 5,
WaitTimeSeconds: 2,
})
if err != nil {
t.Fatalf("ReceiveMessage: %v", err)
}
if len(msgs) != 1 || msgs[0].MessageID != "m1" || msgs[0].ReceiptHandle != "rh1" {
t.Fatalf("unexpected messages: %+v", msgs)
}
if gotReq.QueueName != "q" || gotReq.MaxNumberOfMessages != 5 || gotReq.WaitTimeSeconds != 2 {
t.Errorf("unexpected request: %+v", gotReq)
}
}
func TestReceiveMessageRespectsCallerDeadline(t *testing.T) {
fake := &fakeQueueService{
receiveMessage: func(ctx context.Context, req *kafkamgmtv1.ReceiveMessageRequest) (*kafkamgmtv1.ReceiveMessageResponse, error) {
<-ctx.Done()
return nil, ctx.Err()
},
}
client := newTestClient(t, fake)
ctx, cancel := context.WithTimeout(context.Background(), 200*time.Millisecond)
defer cancel()
start := time.Now()
_, err := client.ReceiveMessage(ctx, "q", ReceiveOptions{WaitTimeSeconds: 20})
elapsed := time.Since(start)
if err == nil {
t.Fatal("expected error from cancelled context")
}
if elapsed > 2*time.Second {
t.Errorf("ReceiveMessage took %v, want it to respect the caller's 200ms deadline, not the 20s+margin internal one", elapsed)
}
}