378 lines
12 KiB
Go
378 lines
12 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"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{})}
|
|
registry := metrics.NewRegistry()
|
|
updater := newAsyncSecondaryRealtimeUpdater(primary, secondary, 8, 1, registry, nil)
|
|
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)
|
|
text := registry.Render()
|
|
for _, want := range []string{
|
|
`vehicle_realtime_async_queue_total{protocol="GB32960",status="queued",store="mysql"} 1`,
|
|
`vehicle_realtime_async_queue_depth{protocol="GB32960",store="mysql"}`,
|
|
} {
|
|
if !strings.Contains(text, want) {
|
|
t.Fatalf("async queue metric missing %s:\n%s", want, text)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestAsyncSecondaryQueueDropDoesNotFailPrimaryUpdate(t *testing.T) {
|
|
env := envelope.FrameEnvelope{Protocol: envelope.ProtocolJT808, VIN: "VIN001"}
|
|
primary := &contextCheckingRealtimeUpdater{}
|
|
registry := metrics.NewRegistry()
|
|
updater := &asyncSecondaryRealtimeUpdater{
|
|
primary: primary,
|
|
secondary: &contextCheckingRealtimeUpdater{},
|
|
queue: make(chan envelope.FrameEnvelope, 1),
|
|
registry: registry,
|
|
}
|
|
updater.queue <- envelope.FrameEnvelope{Protocol: envelope.ProtocolJT808, VIN: "VIN000"}
|
|
|
|
if err := updater.Update(context.Background(), env); err != nil {
|
|
t.Fatalf("Update() error = %v", err)
|
|
}
|
|
if primary.count != 1 {
|
|
t.Fatalf("primary updates = %d, want 1", primary.count)
|
|
}
|
|
text := registry.Render()
|
|
for _, want := range []string{
|
|
`vehicle_realtime_async_queue_total{protocol="JT808",status="dropped",store="mysql"} 1`,
|
|
`vehicle_realtime_async_queue_depth{protocol="JT808",store="mysql"} 1`,
|
|
} {
|
|
if !strings.Contains(text, want) {
|
|
t.Fatalf("async queue drop metric missing %s:\n%s", want, text)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestStoreMetricUpdaterRecordsStoreUpdateResults(t *testing.T) {
|
|
registry := metrics.NewRegistry()
|
|
env := envelope.FrameEnvelope{Protocol: envelope.ProtocolJT808, VIN: "VIN001"}
|
|
updater := storeMetricUpdater{
|
|
store: "redis",
|
|
delegate: &contextCheckingRealtimeUpdater{},
|
|
registry: registry,
|
|
}
|
|
|
|
if err := updater.Update(context.Background(), env); err != nil {
|
|
t.Fatalf("Update() error = %v", err)
|
|
}
|
|
text := registry.Render()
|
|
for _, want := range []string{
|
|
`vehicle_realtime_store_updates_total{protocol="JT808",status="ok",store="redis"} 1`,
|
|
`vehicle_realtime_store_update_duration_ms{protocol="JT808",status="ok",store="redis"}`,
|
|
} {
|
|
if !strings.Contains(text, want) {
|
|
t.Fatalf("store update metric missing %s:\n%s", want, text)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestRetryRealtimeUpdaterRetriesTransientMySQLDeadlock(t *testing.T) {
|
|
delegate := &retryOnceRealtimeUpdater{err: errors.New("Error 1213 (40001): Deadlock found when trying to get lock")}
|
|
updater := retryRealtimeUpdater{delegate: delegate, attempts: 3}
|
|
|
|
if err := updater.Update(context.Background(), envelope.FrameEnvelope{Protocol: envelope.ProtocolGB32960, VIN: "VIN001"}); err != nil {
|
|
t.Fatalf("Update() error = %v", err)
|
|
}
|
|
if delegate.count != 2 {
|
|
t.Fatalf("delegate updates = %d, want 2", delegate.count)
|
|
}
|
|
}
|
|
|
|
func TestAsyncSecondaryRealtimeUpdaterLogsSecondaryErrors(t *testing.T) {
|
|
env := envelope.FrameEnvelope{Protocol: envelope.ProtocolGB32960, VIN: "VIN001", EventID: "event-1"}
|
|
logger := &recordingRealtimeLogger{}
|
|
updater := newAsyncSecondaryRealtimeUpdater(nil, failingRealtimeUpdater{}, 8, 1, nil, logger)
|
|
defer updater.Close()
|
|
|
|
if err := updater.Update(context.Background(), env); err != nil {
|
|
t.Fatalf("Update() error = %v", err)
|
|
}
|
|
|
|
deadline := time.Now().Add(time.Second)
|
|
for time.Now().Before(deadline) {
|
|
if logger.containsError("async realtime secondary update failed") {
|
|
return
|
|
}
|
|
time.Sleep(10 * time.Millisecond)
|
|
}
|
|
t.Fatalf("secondary error was not logged: %#v", logger.errors)
|
|
}
|
|
|
|
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)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestRealtimeAPIExposesPostRawFrameQueryRoute(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/raw-frames/query"`) {
|
|
t.Fatalf("realtime api should expose POST raw frame query route")
|
|
}
|
|
}
|
|
|
|
func TestRealtimeAPIExposesOperationalRealtimeRoutes(t *testing.T) {
|
|
source, err := os.ReadFile("main.go")
|
|
if err != nil {
|
|
t.Fatalf("read main.go: %v", err)
|
|
}
|
|
for _, want := range []string{`"/api/realtime/online"`, `"/api/debug/pipeline"`} {
|
|
if !strings.Contains(string(source), want) {
|
|
t.Fatalf("realtime api should expose operational route %s", want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestRealtimeAPIDoesNotExposeMySQLKVRoute(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/realtime/kv"`) || strings.Contains(string(source), "NewKVQuery") {
|
|
t.Fatal("realtime api should not expose MySQL realtime kv route")
|
|
}
|
|
}
|
|
|
|
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 recordingRealtimeLogger struct {
|
|
mu sync.Mutex
|
|
errors []string
|
|
}
|
|
|
|
func (l *recordingRealtimeLogger) Info(string, ...any) {}
|
|
func (l *recordingRealtimeLogger) Warn(string, ...any) {}
|
|
func (l *recordingRealtimeLogger) Error(message string, _ ...any) {
|
|
l.mu.Lock()
|
|
defer l.mu.Unlock()
|
|
l.errors = append(l.errors, message)
|
|
}
|
|
|
|
func (l *recordingRealtimeLogger) containsError(message string) bool {
|
|
l.mu.Lock()
|
|
defer l.mu.Unlock()
|
|
for _, got := range l.errors {
|
|
if got == message {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
type failingRealtimeUpdater struct{}
|
|
|
|
func (failingRealtimeUpdater) Update(context.Context, envelope.FrameEnvelope) error {
|
|
return errTestRealtimeUpdate
|
|
}
|
|
|
|
var errTestRealtimeUpdate = errors.New("test realtime update failed")
|
|
|
|
type retryOnceRealtimeUpdater struct {
|
|
err error
|
|
count int
|
|
}
|
|
|
|
func (u *retryOnceRealtimeUpdater) Update(context.Context, envelope.FrameEnvelope) error {
|
|
u.count++
|
|
if u.count == 1 {
|
|
return u.err
|
|
}
|
|
return nil
|
|
}
|