133 lines
3.8 KiB
Go
133 lines
3.8 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/segmentio/kafka-go"
|
|
|
|
"lingniu-vehicle-ingest/go/vehicle-gateway/internal/envelope"
|
|
"lingniu-vehicle-ingest/go/vehicle-gateway/internal/metrics"
|
|
)
|
|
|
|
func TestProcessRealtimeMessageUsesUncancelledContextForUpdateAndCommit(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)
|
|
}
|
|
updater := &contextCheckingRealtimeUpdater{}
|
|
committer := &contextCheckingMessageCommitter{}
|
|
|
|
processRealtimeMessage(parent, discardRealtimeLogger{}, nil, updater, committer, kafka.Message{Value: payload})
|
|
|
|
if updater.ctxErr != nil {
|
|
t.Fatalf("updater saw cancelled context: %v", updater.ctxErr)
|
|
}
|
|
if committer.ctxErr != nil {
|
|
t.Fatalf("committer saw cancelled context: %v", committer.ctxErr)
|
|
}
|
|
if updater.count != 1 || committer.count != 1 {
|
|
t.Fatalf("updates=%d commits=%d", updater.count, committer.count)
|
|
}
|
|
}
|
|
|
|
func TestProcessRealtimeMessageRecordsMetrics(t *testing.T) {
|
|
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)
|
|
}
|
|
registry := metrics.NewRegistry()
|
|
|
|
processRealtimeMessage(
|
|
context.Background(),
|
|
discardRealtimeLogger{},
|
|
registry,
|
|
&contextCheckingRealtimeUpdater{},
|
|
&contextCheckingMessageCommitter{},
|
|
kafka.Message{Topic: "vehicle.event.go.unified.v1", Partition: 2, Offset: 10, HighWaterMark: 16, Value: payload},
|
|
)
|
|
|
|
text := registry.Render()
|
|
for _, want := range []string{
|
|
`vehicle_realtime_kafka_messages_total{status="received",topic="vehicle.event.go.unified.v1"} 1`,
|
|
`vehicle_realtime_updates_total{status="ok",topic="vehicle.event.go.unified.v1"} 1`,
|
|
`vehicle_realtime_kafka_commits_total{status="ok",topic="vehicle.event.go.unified.v1"} 1`,
|
|
`vehicle_realtime_kafka_lag{partition="2",topic="vehicle.event.go.unified.v1"} 5`,
|
|
} {
|
|
if !strings.Contains(text, want) {
|
|
t.Fatalf("metrics missing %s:\n%s", want, text)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestCompositeRealtimeUpdaterUpdatesBothStores(t *testing.T) {
|
|
env := envelope.FrameEnvelope{Protocol: envelope.ProtocolGB32960, VIN: "VIN001"}
|
|
redisUpdater := &contextCheckingRealtimeUpdater{}
|
|
snapshotUpdater := &contextCheckingRealtimeUpdater{}
|
|
updater := compositeRealtimeUpdater{primary: redisUpdater, secondary: snapshotUpdater}
|
|
|
|
if err := updater.Update(context.Background(), env); err != nil {
|
|
t.Fatalf("Update() error = %v", err)
|
|
}
|
|
if redisUpdater.count != 1 || snapshotUpdater.count != 1 {
|
|
t.Fatalf("updates primary=%d secondary=%d", redisUpdater.count, snapshotUpdater.count)
|
|
}
|
|
}
|
|
|
|
func TestKafkaTopicsFromEnvDefaultsToGoUnifiedTopic(t *testing.T) {
|
|
got := strings.Join(kafkaTopicsFromEnv(), ",")
|
|
want := "vehicle.event.go.unified.v1"
|
|
if got != want {
|
|
t.Fatalf("topics = %q, want %q", got, want)
|
|
}
|
|
}
|
|
|
|
type contextCheckingRealtimeUpdater struct {
|
|
ctxErr error
|
|
count int
|
|
}
|
|
|
|
func (u *contextCheckingRealtimeUpdater) Update(ctx context.Context, _ envelope.FrameEnvelope) error {
|
|
u.ctxErr = ctx.Err()
|
|
u.count++
|
|
return u.ctxErr
|
|
}
|
|
|
|
type contextCheckingMessageCommitter struct {
|
|
ctxErr error
|
|
count int
|
|
}
|
|
|
|
func (c *contextCheckingMessageCommitter) CommitMessages(ctx context.Context, _ ...kafka.Message) error {
|
|
c.ctxErr = ctx.Err()
|
|
c.count++
|
|
return c.ctxErr
|
|
}
|
|
|
|
type discardRealtimeLogger struct{}
|
|
|
|
func (discardRealtimeLogger) Info(string, ...any) {}
|
|
func (discardRealtimeLogger) Error(string, ...any) {}
|
|
func (discardRealtimeLogger) Warn(string, ...any) {}
|