206 lines
6.1 KiB
Go
206 lines
6.1 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"os"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
|
|
"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.raw.go.jt808.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.raw.go.jt808.v1"} 1`,
|
|
`vehicle_realtime_updates_total{status="ok",topic="vehicle.raw.go.jt808.v1"} 1`,
|
|
`vehicle_realtime_kafka_commits_total{status="ok",topic="vehicle.raw.go.jt808.v1"} 1`,
|
|
`vehicle_realtime_kafka_lag{partition="2",topic="vehicle.raw.go.jt808.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 TestAsyncSecondaryRealtimeUpdaterDoesNotBlockPrimaryPath(t *testing.T) {
|
|
env := envelope.FrameEnvelope{Protocol: envelope.ProtocolGB32960, VIN: "VIN001"}
|
|
primary := &contextCheckingRealtimeUpdater{}
|
|
secondary := &blockingRealtimeUpdater{started: make(chan struct{}), release: make(chan struct{})}
|
|
updater := newAsyncSecondaryRealtimeUpdater(primary, secondary, 8, 1)
|
|
defer updater.Close()
|
|
|
|
start := time.Now()
|
|
if err := updater.Update(context.Background(), env); err != nil {
|
|
t.Fatalf("Update() error = %v", err)
|
|
}
|
|
if elapsed := time.Since(start); elapsed > 100*time.Millisecond {
|
|
t.Fatalf("async updater blocked primary path for %v", elapsed)
|
|
}
|
|
if primary.count != 1 {
|
|
t.Fatalf("primary updates = %d, want 1", primary.count)
|
|
}
|
|
select {
|
|
case <-secondary.started:
|
|
case <-time.After(time.Second):
|
|
t.Fatal("secondary update was not started asynchronously")
|
|
}
|
|
close(secondary.release)
|
|
}
|
|
|
|
func TestKafkaTopicsFromEnvDefaultsToGoRawTopics(t *testing.T) {
|
|
got := strings.Join(kafkaTopicsFromEnv(), ",")
|
|
want := "vehicle.raw.go.gb32960.v1,vehicle.raw.go.jt808.v1,vehicle.raw.go.yutong-mqtt.v1"
|
|
if got != want {
|
|
t.Fatalf("topics = %q, want %q", got, want)
|
|
}
|
|
}
|
|
|
|
func TestRealtimeReaderConfigStartsAtLatestOffset(t *testing.T) {
|
|
cfg := realtimeReaderConfig([]string{"127.0.0.1:9092"}, []string{"vehicle.raw.go.jt808.v1"})
|
|
|
|
if cfg.StartOffset != kafka.LastOffset {
|
|
t.Fatalf("StartOffset = %d, want kafka.LastOffset %d", cfg.StartOffset, kafka.LastOffset)
|
|
}
|
|
if got := strings.Join(cfg.GroupTopics, ","); got != "vehicle.raw.go.jt808.v1" {
|
|
t.Fatalf("GroupTopics = %q", got)
|
|
}
|
|
}
|
|
|
|
func TestRealtimeAPIDoesNotExposeDuplicateMileagePointRoute(t *testing.T) {
|
|
source, err := os.ReadFile("main.go")
|
|
if err != nil {
|
|
t.Fatalf("read main.go: %v", err)
|
|
}
|
|
if strings.Contains(string(source), "/api/history/mileage-points") {
|
|
t.Fatalf("realtime api should expose mileage through /api/history/locations only")
|
|
}
|
|
}
|
|
|
|
func TestRealtimeAPICachesBindingPlateResolver(t *testing.T) {
|
|
source, err := os.ReadFile("main.go")
|
|
if err != nil {
|
|
t.Fatalf("read main.go: %v", err)
|
|
}
|
|
for _, want := range []string{"NewCachedPlateResolver", "PLATE_CACHE_TTL_SECONDS"} {
|
|
if !strings.Contains(string(source), want) {
|
|
t.Fatalf("realtime api should configure cached plate resolver, missing %s", 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) {}
|
|
|
|
type blockingRealtimeUpdater struct {
|
|
started chan struct{}
|
|
release chan struct{}
|
|
once sync.Once
|
|
}
|
|
|
|
func (u *blockingRealtimeUpdater) Update(context.Context, envelope.FrameEnvelope) error {
|
|
u.once.Do(func() { close(u.started) })
|
|
<-u.release
|
|
return nil
|
|
}
|