Skip to content

Commit 1a70ea0

Browse files
committed
Refactor pubsub test writes with tagged types
1 parent 18e1aaa commit 1a70ea0

2 files changed

Lines changed: 19 additions & 11 deletions

File tree

proxy/pubsub_test.go

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,17 @@ import (
1111
"github.com/tidwall/redcon"
1212
)
1313

14+
// Tagged types to distinguish RESP wire types in mock writes.
15+
type respInt struct{ V int } // WriteInt
16+
type respInt64 struct{ V int64 } // WriteInt64
17+
type respArray struct{ N int } // WriteArray
18+
1419
// mockDetachedConn implements redcon.DetachedConn for unit testing.
1520
type mockDetachedConn struct {
1621
mu sync.Mutex
1722
commands []redcon.Command // queued commands to return from ReadCommand
1823
cmdIdx int
19-
writes []any // recorded writes: string for WriteString/WriteError/WriteBulkString, int for WriteInt/WriteArray, nil for WriteNull
24+
writes []any // recorded writes: string for WriteString/WriteError/WriteBulkString, respInt/respInt64/respArray for typed ints, nil for WriteNull
2025
closed bool
2126
readErr error // error to return from ReadCommand when commands exhausted
2227
}
@@ -74,20 +79,20 @@ func (m *mockDetachedConn) WriteBulkString(bulk string) {
7479
func (m *mockDetachedConn) WriteInt(num int) {
7580
m.mu.Lock()
7681
defer m.mu.Unlock()
77-
m.writes = append(m.writes, num)
82+
m.writes = append(m.writes, respInt{num})
7883
}
7984
func (m *mockDetachedConn) WriteInt64(num int64) {
8085
m.mu.Lock()
8186
defer m.mu.Unlock()
82-
m.writes = append(m.writes, int(num))
87+
m.writes = append(m.writes, respInt64{num})
8388
}
8489
func (m *mockDetachedConn) WriteUint64(_ uint64) {
8590
// Not used in pubsub tests.
8691
}
8792
func (m *mockDetachedConn) WriteArray(count int) {
8893
m.mu.Lock()
8994
defer m.mu.Unlock()
90-
m.writes = append(m.writes, count)
95+
m.writes = append(m.writes, respArray{count})
9196
}
9297
func (m *mockDetachedConn) WriteNull() {
9398
m.mu.Lock()
@@ -201,7 +206,7 @@ func TestPubSub_WriteUnsubAll_PerChannelReplies(t *testing.T) {
201206
// Count array headers (each reply starts with WriteArray(3))
202207
arrCount := 0
203208
for _, w := range writes {
204-
if n, ok := w.(int); ok && n == 3 {
209+
if a, ok := w.(respArray); ok && a.N == 3 {
205210
arrCount++
206211
}
207212
}
@@ -250,7 +255,7 @@ func TestPubSub_WriteUnsubAll_Patterns(t *testing.T) {
250255
// Should have 1 reply (one pattern)
251256
arrCount := 0
252257
for _, w := range writes {
253-
if n, ok := w.(int); ok && n == 3 {
258+
if a, ok := w.(respArray); ok && a.N == 3 {
254259
arrCount++
255260
}
256261
}
@@ -341,7 +346,7 @@ func TestPubSub_HandlePubSubPing(t *testing.T) {
341346

342347
writes := dconn.getWrites()
343348
// ["pong", ""]
344-
assert.Contains(t, writes, 2) // WriteArray(2)
349+
assert.Contains(t, writes, respArray{2}) // WriteArray(2)
345350
assert.Contains(t, writes, "BULKSTR:pong")
346351
assert.Contains(t, writes, "BULKSTR:") // empty string
347352
}
@@ -372,7 +377,7 @@ func TestPubSub_HandleUnsubNoSession(t *testing.T) {
372377
}
373378
}
374379
assert.True(t, hasNull)
375-
assert.Contains(t, writes, 0) // WriteInt64(0)
380+
assert.Contains(t, writes, respInt64{0}) // WriteInt64(0)
376381
}
377382

378383
func TestPubSub_SubscribeInTxnRejected(t *testing.T) {

proxy/sentry.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,18 +85,21 @@ func (r *SentryReporter) CaptureDivergence(div Divergence) {
8585
}
8686
r.hub.WithScope(func(scope *sentry.Scope) {
8787
scope.SetTag("command", div.Command)
88-
scope.SetTag("key", div.Key)
8988
scope.SetTag("kind", div.Kind.String())
89+
// Omit raw key from Sentry tags to avoid leaking sensitive data;
90+
// only send a truncated form as an extra for debugging.
91+
scope.SetExtra("key", truncateValue(div.Key))
9092
scope.SetExtra("primary", truncateValue(div.Primary))
9193
scope.SetExtra("secondary", truncateValue(div.Secondary))
9294
scope.SetFingerprint([]string{"divergence", div.Kind.String(), div.Command})
9395
scope.SetLevel(sentry.LevelWarning)
94-
r.hub.CaptureMessage(fmt.Sprintf("data divergence: %s %s (%s)", div.Kind, div.Command, div.Key))
96+
r.hub.CaptureMessage(fmt.Sprintf("data divergence: %s %s", div.Kind, div.Command))
9597
})
9698
}
9799

98100
// ShouldReport checks if this fingerprint has been reported recently (cooldown-based).
99-
// Periodically evicts expired entries to prevent unbounded map growth.
101+
// Evicts expired entries when the map reaches maxReportEntries to bound memory usage.
102+
// Returns false (drops the report) if the map is still at capacity after eviction.
100103
func (r *SentryReporter) ShouldReport(fingerprint string) bool {
101104
r.mu.Lock()
102105
defer r.mu.Unlock()

0 commit comments

Comments
 (0)