63 lines
1.8 KiB
Go
63 lines
1.8 KiB
Go
package kmsvc
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
"time"
|
|
|
|
kafkamgmtv1 "forgejo.riotpiao.com/rock/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)
|
|
}
|
|
}
|