perf(go): decouple realtime mysql projection
This commit is contained in:
@@ -5,7 +5,9 @@ import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/segmentio/kafka-go"
|
||||
|
||||
@@ -96,6 +98,31 @@ func TestCompositeRealtimeUpdaterUpdatesBothStores(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
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"
|
||||
@@ -164,3 +191,15 @@ 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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user