71 lines
1.7 KiB
Go
71 lines
1.7 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"testing"
|
|
|
|
"github.com/segmentio/kafka-go"
|
|
|
|
"lingniu-vehicle-ingest/go/vehicle-gateway/internal/envelope"
|
|
)
|
|
|
|
func TestProcessHistoryMessageUsesUncancelledContextForAppendAndCommit(t *testing.T) {
|
|
parent, cancel := context.WithCancel(context.Background())
|
|
cancel()
|
|
|
|
env := envelope.FrameEnvelope{
|
|
Protocol: envelope.ProtocolJT808,
|
|
MessageID: "0x0200",
|
|
Phone: "13307795425",
|
|
EventTimeMS: 1782918600000,
|
|
ReceivedAtMS: 1782918600000,
|
|
ParseStatus: envelope.ParseOK,
|
|
}
|
|
payload, err := json.Marshal(env)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
appender := &contextCheckingHistoryAppender{}
|
|
committer := &contextCheckingHistoryCommitter{}
|
|
|
|
processHistoryMessage(parent, discardHistoryLogger{}, appender, committer, kafka.Message{Value: payload})
|
|
|
|
if appender.ctxErr != nil {
|
|
t.Fatalf("appender saw cancelled context: %v", appender.ctxErr)
|
|
}
|
|
if committer.ctxErr != nil {
|
|
t.Fatalf("committer saw cancelled context: %v", committer.ctxErr)
|
|
}
|
|
if appender.count != 1 || committer.count != 1 {
|
|
t.Fatalf("appends=%d commits=%d", appender.count, committer.count)
|
|
}
|
|
}
|
|
|
|
type contextCheckingHistoryAppender struct {
|
|
ctxErr error
|
|
count int
|
|
}
|
|
|
|
func (a *contextCheckingHistoryAppender) AppendAll(ctx context.Context, _ envelope.FrameEnvelope) error {
|
|
a.ctxErr = ctx.Err()
|
|
a.count++
|
|
return a.ctxErr
|
|
}
|
|
|
|
type contextCheckingHistoryCommitter struct {
|
|
ctxErr error
|
|
count int
|
|
}
|
|
|
|
func (c *contextCheckingHistoryCommitter) CommitMessages(ctx context.Context, _ ...kafka.Message) error {
|
|
c.ctxErr = ctx.Err()
|
|
c.count++
|
|
return c.ctxErr
|
|
}
|
|
|
|
type discardHistoryLogger struct{}
|
|
|
|
func (discardHistoryLogger) Error(string, ...any) {}
|
|
func (discardHistoryLogger) Warn(string, ...any) {}
|