mirror of
https://github.com/redis/go-redis.git
synced 2025-11-26 06:23:09 +03:00
* wip * wip, used and unusable states * polish state machine * correct handling OnPut * better errors for tests, hook should work now * fix linter * improve reauth state management. fix tests * Update internal/pool/conn.go Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update internal/pool/conn.go Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * better timeouts * empty endpoint handoff case * fix handoff state when queued for handoff * try to detect the deadlock * try to detect the deadlock x2 * delete should be called * improve tests * fix mark on uninitialized connection * Update internal/pool/conn_state_test.go Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update internal/pool/conn_state_test.go Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update internal/pool/pool.go Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update internal/pool/conn_state.go Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update internal/pool/conn.go Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * fix error from copilot * address copilot comment * fix(pool): pool performance (#3565) * perf(pool): replace hookManager RWMutex with atomic.Pointer and add predefined state slices - Replace hookManager RWMutex with atomic.Pointer for lock-free reads in hot paths - Add predefined state slices to avoid allocations (validFromInUse, validFromCreatedOrIdle, etc.) - Add Clone() method to PoolHookManager for atomic updates - Update AddPoolHook/RemovePoolHook to use copy-on-write pattern - Update all hookManager access points to use atomic Load() Performance improvements: - Eliminates RWMutex contention in Get/Put/Remove hot paths - Reduces allocations by reusing predefined state slices - Lock-free reads allow better CPU cache utilization * perf(pool): eliminate mutex overhead in state machine hot path The state machine was calling notifyWaiters() on EVERY Get/Put operation, which acquired a mutex even when no waiters were present (the common case). Fix: Use atomic waiterCount to check for waiters BEFORE acquiring mutex. This eliminates mutex contention in the hot path (Get/Put operations). Implementation: - Added atomic.Int32 waiterCount field to ConnStateMachine - Increment when adding waiter, decrement when removing - Check waiterCount atomically before acquiring mutex in notifyWaiters() Performance impact: - Before: mutex lock/unlock on every Get/Put (even with no waiters) - After: lock-free atomic check, only acquire mutex if waiters exist - Expected improvement: ~30-50% for Get/Put operations * perf(pool): use predefined state slices to eliminate allocations in hot path The pool was creating new slice literals on EVERY Get/Put operation: - popIdle(): []ConnState{StateCreated, StateIdle} - putConn(): []ConnState{StateInUse} - CompareAndSwapUsed(): []ConnState{StateIdle} and []ConnState{StateInUse} - MarkUnusableForHandoff(): []ConnState{StateInUse, StateIdle, StateCreated} These allocations were happening millions of times per second in the hot path. Fix: Use predefined global slices defined in conn_state.go: - validFromInUse - validFromCreatedOrIdle - validFromCreatedInUseOrIdle Performance impact: - Before: 4 slice allocations per Get/Put cycle - After: 0 allocations (use predefined slices) - Expected improvement: ~30-40% reduction in allocations and GC pressure * perf(pool): optimize TryTransition to reduce atomic operations Further optimize the hot path by: 1. Remove redundant GetState() call in the loop 2. Only check waiterCount after successful CAS (not before loop) 3. Inline the waiterCount check to avoid notifyWaiters() call overhead This reduces atomic operations from 4-5 per Get/Put to 2-3: - Before: GetState() + CAS + waiterCount.Load() + notifyWaiters mutex check - After: CAS + waiterCount.Load() (only if CAS succeeds) Performance impact: - Eliminates 1-2 atomic operations per Get/Put - Expected improvement: ~10-15% for Get/Put operations * perf(pool): add fast path for Get/Put to match master performance Introduced TryTransitionFast() for the hot path (Get/Put operations): - Single CAS operation (same as master's atomic bool) - No waiter notification overhead - No loop through valid states - No error allocation Hot path flow: 1. popIdle(): Try IDLE → IN_USE (fast), fallback to CREATED → IN_USE 2. putConn(): Try IN_USE → IDLE (fast) This matches master's performance while preserving state machine for: - Background operations (handoff/reauth use UNUSABLE state) - State validation (TryTransition still available) - Waiter notification (AwaitAndTransition for blocking) Performance comparison per Get/Put cycle: - Master: 2 atomic CAS operations - State machine (before): 5 atomic operations (2.5x slower) - State machine (after): 2 atomic CAS operations (same as master!) Expected improvement: Restore to baseline ~11,373 ops/sec * combine cas * fix linter * try faster approach * fast semaphore * better inlining for hot path * fix linter issues * use new semaphore in auth as well * linter should be happy now * add comments * Update internal/pool/conn_state.go Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * address comment * slight reordering * try to cache time if for non-critical calculation * fix wrong benchmark * add concurrent test * fix benchmark report * add additional expect to check output * comment and variable rename --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * initConn sets IDLE state - Handle unexpected conn state changes * fix precision of time cache and usedAt * allow e2e tests to run longer * Fix broken initialization of idle connections * optimize push notif * 100ms -> 50ms * use correct timer for last health check * verify pass auth on conn creation * fix assertion * fix unsafe test * fix benchmark test * improve remove conn * re doesn't support requirepass * wait more in e2e test * flaky test * add missed method in interface * fix test assertions * silence logs and faster hooks manager * address linter comment * fix flaky test * use read instad of control * use pool size for semsize * CAS instead of reading the state * preallocate errors and states * preallocate state slices * fix flaky test * fix fast semaphore that could have been starved * try to fix the semaphore * should properly notify the waiters - this way a waiter that timesout at the same time a releaser is releasing, won't throw token. the releaser will fail to notify and will pick another waiter. this hybrid approach should be faster than channels and maintains FIFO * waiter may double-release (if closed/times out) * priority of operations * use simple approach of fifo waiters * use simple channel based semaphores * address linter and tests * remove unused benchs * change log message * address pr comments * address pr comments * fix data race --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
624 lines
19 KiB
Go
624 lines
19 KiB
Go
package proto
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"math/rand"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
// TestPeekPushNotificationName tests the updated PeekPushNotificationName method
|
|
func TestPeekPushNotificationName(t *testing.T) {
|
|
t.Run("ValidPushNotifications", func(t *testing.T) {
|
|
testCases := []struct {
|
|
name string
|
|
notification string
|
|
expected string
|
|
}{
|
|
{"MOVING", "MOVING", "MOVING"},
|
|
{"MIGRATING", "MIGRATING", "MIGRATING"},
|
|
{"MIGRATED", "MIGRATED", "MIGRATED"},
|
|
{"FAILING_OVER", "FAILING_OVER", "FAILING_OVER"},
|
|
{"FAILED_OVER", "FAILED_OVER", "FAILED_OVER"},
|
|
{"message", "message", "message"},
|
|
{"pmessage", "pmessage", "pmessage"},
|
|
{"subscribe", "subscribe", "subscribe"},
|
|
{"unsubscribe", "unsubscribe", "unsubscribe"},
|
|
{"psubscribe", "psubscribe", "psubscribe"},
|
|
{"punsubscribe", "punsubscribe", "punsubscribe"},
|
|
{"smessage", "smessage", "smessage"},
|
|
{"ssubscribe", "ssubscribe", "ssubscribe"},
|
|
{"sunsubscribe", "sunsubscribe", "sunsubscribe"},
|
|
{"custom", "custom", "custom"},
|
|
{"short", "a", "a"},
|
|
{"empty", "", ""},
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
buf := createValidPushNotification(tc.notification, "data")
|
|
reader := NewReader(buf)
|
|
|
|
// Prime the buffer by peeking first
|
|
_, _ = reader.rd.Peek(1)
|
|
|
|
name, err := reader.PeekPushNotificationName()
|
|
if err != nil {
|
|
t.Errorf("PeekPushNotificationName should not error for valid notification: %v", err)
|
|
}
|
|
|
|
if name != tc.expected {
|
|
t.Errorf("Expected notification name '%s', got '%s'", tc.expected, name)
|
|
}
|
|
})
|
|
}
|
|
})
|
|
|
|
t.Run("NotificationWithMultipleArguments", func(t *testing.T) {
|
|
// Create push notification with multiple arguments
|
|
buf := createPushNotificationWithArgs("MOVING", "slot", "123", "from", "node1", "to", "node2")
|
|
reader := NewReader(buf)
|
|
|
|
// Prime the buffer
|
|
_, _ = reader.rd.Peek(1)
|
|
|
|
name, err := reader.PeekPushNotificationName()
|
|
if err != nil {
|
|
t.Errorf("PeekPushNotificationName should not error: %v", err)
|
|
}
|
|
|
|
if name != "MOVING" {
|
|
t.Errorf("Expected 'MOVING', got '%s'", name)
|
|
}
|
|
})
|
|
|
|
t.Run("SingleElementNotification", func(t *testing.T) {
|
|
// Create push notification with single element
|
|
buf := createSingleElementPushNotification("TEST")
|
|
reader := NewReader(buf)
|
|
|
|
// Prime the buffer
|
|
_, _ = reader.rd.Peek(1)
|
|
|
|
name, err := reader.PeekPushNotificationName()
|
|
if err != nil {
|
|
t.Errorf("PeekPushNotificationName should not error: %v", err)
|
|
}
|
|
|
|
if name != "TEST" {
|
|
t.Errorf("Expected 'TEST', got '%s'", name)
|
|
}
|
|
})
|
|
|
|
t.Run("ErrorDetection", func(t *testing.T) {
|
|
t.Run("NotPushNotification", func(t *testing.T) {
|
|
// Test with regular array instead of push notification
|
|
buf := &bytes.Buffer{}
|
|
fmt.Fprint(buf, "*2\r\n$6\r\nMOVING\r\n$4\r\ndata\r\n")
|
|
reader := NewReader(buf)
|
|
|
|
_, err := reader.PeekPushNotificationName()
|
|
if err == nil {
|
|
t.Error("PeekPushNotificationName should error for non-push notification")
|
|
}
|
|
|
|
// The error might be "no data available" or "can't parse push notification"
|
|
if !strings.Contains(err.Error(), "can't peek push notification name") {
|
|
t.Errorf("Error should mention push notification parsing, got: %v", err)
|
|
}
|
|
})
|
|
|
|
t.Run("InsufficientData", func(t *testing.T) {
|
|
// Test with buffer smaller than peek size - this might panic due to bounds checking
|
|
buf := &bytes.Buffer{}
|
|
fmt.Fprint(buf, ">")
|
|
reader := NewReader(buf)
|
|
|
|
func() {
|
|
defer func() {
|
|
if r := recover(); r != nil {
|
|
t.Logf("PeekPushNotificationName panicked as expected for insufficient data: %v", r)
|
|
}
|
|
}()
|
|
_, err := reader.PeekPushNotificationName()
|
|
if err == nil {
|
|
t.Error("PeekPushNotificationName should error for insufficient data")
|
|
}
|
|
}()
|
|
})
|
|
|
|
t.Run("EmptyBuffer", func(t *testing.T) {
|
|
buf := &bytes.Buffer{}
|
|
reader := NewReader(buf)
|
|
|
|
_, err := reader.PeekPushNotificationName()
|
|
if err == nil {
|
|
t.Error("PeekPushNotificationName should error for empty buffer")
|
|
}
|
|
})
|
|
|
|
t.Run("DifferentRESPTypes", func(t *testing.T) {
|
|
// Test with different RESP types that should be rejected
|
|
respTypes := []byte{'+', '-', ':', '$', '*', '%', '~', '|', '('}
|
|
|
|
for _, respType := range respTypes {
|
|
t.Run(fmt.Sprintf("Type_%c", respType), func(t *testing.T) {
|
|
buf := &bytes.Buffer{}
|
|
buf.WriteByte(respType)
|
|
fmt.Fprint(buf, "test data that fills the buffer completely")
|
|
reader := NewReader(buf)
|
|
|
|
_, err := reader.PeekPushNotificationName()
|
|
if err == nil {
|
|
t.Errorf("PeekPushNotificationName should error for RESP type '%c'", respType)
|
|
}
|
|
|
|
// The error might be "no data available" or "can't parse push notification"
|
|
if !strings.Contains(err.Error(), "can't peek push notification name") {
|
|
t.Errorf("Error should mention push notification parsing, got: %v", err)
|
|
}
|
|
})
|
|
}
|
|
})
|
|
})
|
|
|
|
t.Run("EdgeCases", func(t *testing.T) {
|
|
t.Run("ZeroLengthArray", func(t *testing.T) {
|
|
// Create push notification with zero elements: >0\r\n
|
|
buf := &bytes.Buffer{}
|
|
fmt.Fprint(buf, ">0\r\npadding_data_to_fill_buffer_completely")
|
|
reader := NewReader(buf)
|
|
|
|
_, err := reader.PeekPushNotificationName()
|
|
if err == nil {
|
|
t.Error("PeekPushNotificationName should error for zero-length array")
|
|
}
|
|
})
|
|
|
|
t.Run("EmptyNotificationName", func(t *testing.T) {
|
|
// Create push notification with empty name: >1\r\n$0\r\n\r\n
|
|
buf := createValidPushNotification("", "data")
|
|
reader := NewReader(buf)
|
|
|
|
// Prime the buffer
|
|
_, _ = reader.rd.Peek(1)
|
|
|
|
name, err := reader.PeekPushNotificationName()
|
|
if err != nil {
|
|
t.Errorf("PeekPushNotificationName should not error for empty name: %v", err)
|
|
}
|
|
|
|
if name != "" {
|
|
t.Errorf("Expected empty notification name, got '%s'", name)
|
|
}
|
|
})
|
|
|
|
t.Run("CorruptedData", func(t *testing.T) {
|
|
corruptedCases := []struct {
|
|
name string
|
|
data string
|
|
}{
|
|
{"CorruptedLength", ">abc\r\n$6\r\nMOVING\r\n"},
|
|
{"MissingCRLF", ">2$6\r\nMOVING\r\n$4\r\ndata\r\n"},
|
|
{"InvalidStringLength", ">2\r\n$abc\r\nMOVING\r\n$4\r\ndata\r\n"},
|
|
{"NegativeStringLength", ">2\r\n$-1\r\n$4\r\ndata\r\n"},
|
|
{"IncompleteString", ">1\r\n$6\r\nMOV"},
|
|
}
|
|
|
|
for _, tc := range corruptedCases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
buf := &bytes.Buffer{}
|
|
fmt.Fprint(buf, tc.data)
|
|
reader := NewReader(buf)
|
|
|
|
// Some corrupted data might not error but return unexpected results
|
|
// This is acceptable behavior for malformed input
|
|
name, err := reader.PeekPushNotificationName()
|
|
if err != nil {
|
|
t.Logf("PeekPushNotificationName errored for corrupted data %s: %v (DATA: %s)", tc.name, err, tc.data)
|
|
} else {
|
|
t.Logf("PeekPushNotificationName returned '%s' for corrupted data NAME: %s, DATA: %s", name, tc.name, tc.data)
|
|
}
|
|
})
|
|
}
|
|
})
|
|
})
|
|
|
|
t.Run("BoundaryConditions", func(t *testing.T) {
|
|
t.Run("ExactlyPeekSize", func(t *testing.T) {
|
|
// Create buffer that is exactly 36 bytes (the peek window size)
|
|
buf := &bytes.Buffer{}
|
|
// ">1\r\n$4\r\nTEST\r\n" = 14 bytes, need 22 more
|
|
fmt.Fprint(buf, ">1\r\n$4\r\nTEST\r\n1234567890123456789012")
|
|
if buf.Len() != 36 {
|
|
t.Errorf("Expected buffer length 36, got %d", buf.Len())
|
|
}
|
|
|
|
reader := NewReader(buf)
|
|
// Prime the buffer
|
|
_, _ = reader.rd.Peek(1)
|
|
|
|
name, err := reader.PeekPushNotificationName()
|
|
if err != nil {
|
|
t.Errorf("PeekPushNotificationName should work for exact peek size: %v", err)
|
|
}
|
|
|
|
if name != "TEST" {
|
|
t.Errorf("Expected 'TEST', got '%s'", name)
|
|
}
|
|
})
|
|
|
|
t.Run("LessThanPeekSize", func(t *testing.T) {
|
|
// Create buffer smaller than 36 bytes but with complete notification
|
|
buf := createValidPushNotification("TEST", "")
|
|
reader := NewReader(buf)
|
|
|
|
// Prime the buffer
|
|
_, _ = reader.rd.Peek(1)
|
|
|
|
name, err := reader.PeekPushNotificationName()
|
|
if err != nil {
|
|
t.Errorf("PeekPushNotificationName should work for complete notification: %v", err)
|
|
}
|
|
|
|
if name != "TEST" {
|
|
t.Errorf("Expected 'TEST', got '%s'", name)
|
|
}
|
|
})
|
|
|
|
t.Run("LongNotificationName", func(t *testing.T) {
|
|
// Test with notification name that might exceed peek window
|
|
longName := strings.Repeat("A", 20) // 20 character name (safe size)
|
|
buf := createValidPushNotification(longName, "data")
|
|
reader := NewReader(buf)
|
|
|
|
// Prime the buffer
|
|
_, _ = reader.rd.Peek(1)
|
|
|
|
name, err := reader.PeekPushNotificationName()
|
|
if err != nil {
|
|
t.Errorf("PeekPushNotificationName should work for long name: %v", err)
|
|
}
|
|
|
|
if name != longName {
|
|
t.Errorf("Expected '%s', got '%s'", longName, name)
|
|
}
|
|
})
|
|
})
|
|
}
|
|
|
|
// Helper functions to create test data
|
|
|
|
// createValidPushNotification creates a valid RESP3 push notification
|
|
func createValidPushNotification(notificationName, data string) *bytes.Buffer {
|
|
buf := &bytes.Buffer{}
|
|
|
|
simpleOrString := rand.Intn(2) == 0
|
|
defMsg := fmt.Sprintf("$%d\r\n%s\r\n", len(notificationName), notificationName)
|
|
|
|
if data == "" {
|
|
|
|
// Single element notification
|
|
fmt.Fprint(buf, ">1\r\n")
|
|
if simpleOrString {
|
|
fmt.Fprintf(buf, "+%s\r\n", notificationName)
|
|
} else {
|
|
fmt.Fprint(buf, defMsg)
|
|
}
|
|
} else {
|
|
// Two element notification
|
|
fmt.Fprint(buf, ">2\r\n")
|
|
if simpleOrString {
|
|
fmt.Fprintf(buf, "+%s\r\n", notificationName)
|
|
fmt.Fprintf(buf, "+%s\r\n", data)
|
|
} else {
|
|
fmt.Fprint(buf, defMsg)
|
|
fmt.Fprint(buf, defMsg)
|
|
}
|
|
}
|
|
|
|
return buf
|
|
}
|
|
|
|
// createReaderWithPrimedBuffer creates a reader and primes the buffer
|
|
func createReaderWithPrimedBuffer(buf *bytes.Buffer) *Reader {
|
|
reader := NewReader(buf)
|
|
// Prime the buffer by peeking first
|
|
_, _ = reader.rd.Peek(1)
|
|
return reader
|
|
}
|
|
|
|
// createPushNotificationWithArgs creates a push notification with multiple arguments
|
|
func createPushNotificationWithArgs(notificationName string, args ...string) *bytes.Buffer {
|
|
buf := &bytes.Buffer{}
|
|
|
|
totalElements := 1 + len(args)
|
|
fmt.Fprintf(buf, ">%d\r\n", totalElements)
|
|
|
|
// Write notification name
|
|
fmt.Fprintf(buf, "$%d\r\n%s\r\n", len(notificationName), notificationName)
|
|
|
|
// Write arguments
|
|
for _, arg := range args {
|
|
fmt.Fprintf(buf, "$%d\r\n%s\r\n", len(arg), arg)
|
|
}
|
|
|
|
return buf
|
|
}
|
|
|
|
// createSingleElementPushNotification creates a push notification with single element
|
|
func createSingleElementPushNotification(notificationName string) *bytes.Buffer {
|
|
buf := &bytes.Buffer{}
|
|
fmt.Fprint(buf, ">1\r\n")
|
|
fmt.Fprintf(buf, "$%d\r\n%s\r\n", len(notificationName), notificationName)
|
|
return buf
|
|
}
|
|
|
|
// BenchmarkPeekPushNotificationName benchmarks the method performance
|
|
func BenchmarkPeekPushNotificationName(b *testing.B) {
|
|
testCases := []struct {
|
|
name string
|
|
notification string
|
|
}{
|
|
{"Short", "TEST"},
|
|
{"Medium", "MOVING_NOTIFICATION"},
|
|
{"Long", "VERY_LONG_NOTIFICATION_NAME_FOR_TESTING"},
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
b.Run(tc.name, func(b *testing.B) {
|
|
buf := createValidPushNotification(tc.notification, "data")
|
|
data := buf.Bytes()
|
|
|
|
// Reuse both bytes.Reader and proto.Reader to avoid allocations
|
|
bytesReader := bytes.NewReader(data)
|
|
reader := NewReader(bytesReader)
|
|
|
|
b.ResetTimer()
|
|
b.ReportAllocs()
|
|
for i := 0; i < b.N; i++ {
|
|
// Reset the bytes.Reader to the beginning without allocating
|
|
bytesReader.Reset(data)
|
|
// Reset the proto.Reader to reuse the bufio buffer
|
|
reader.Reset(bytesReader)
|
|
_, err := reader.PeekPushNotificationName()
|
|
if err != nil {
|
|
b.Errorf("PeekPushNotificationName should not error: %v", err)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestPeekPushNotificationNameSpecialCases tests special cases and realistic scenarios
|
|
func TestPeekPushNotificationNameSpecialCases(t *testing.T) {
|
|
t.Run("RealisticNotifications", func(t *testing.T) {
|
|
// Test realistic Redis push notifications
|
|
realisticCases := []struct {
|
|
name string
|
|
notification []string
|
|
expected string
|
|
}{
|
|
{"MovingSlot", []string{"MOVING", "slot", "123", "from", "127.0.0.1:7000", "to", "127.0.0.1:7001"}, "MOVING"},
|
|
{"MigratingSlot", []string{"MIGRATING", "slot", "456", "from", "127.0.0.1:7001", "to", "127.0.0.1:7002"}, "MIGRATING"},
|
|
{"MigratedSlot", []string{"MIGRATED", "slot", "789", "from", "127.0.0.1:7002", "to", "127.0.0.1:7000"}, "MIGRATED"},
|
|
{"FailingOver", []string{"FAILING_OVER", "node", "127.0.0.1:7000"}, "FAILING_OVER"},
|
|
{"FailedOver", []string{"FAILED_OVER", "node", "127.0.0.1:7000"}, "FAILED_OVER"},
|
|
{"PubSubMessage", []string{"message", "mychannel", "hello world"}, "message"},
|
|
{"PubSubPMessage", []string{"pmessage", "pattern*", "mychannel", "hello world"}, "pmessage"},
|
|
{"Subscribe", []string{"subscribe", "mychannel", "1"}, "subscribe"},
|
|
{"Unsubscribe", []string{"unsubscribe", "mychannel", "0"}, "unsubscribe"},
|
|
}
|
|
|
|
for _, tc := range realisticCases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
buf := createPushNotificationWithArgs(tc.notification[0], tc.notification[1:]...)
|
|
reader := createReaderWithPrimedBuffer(buf)
|
|
|
|
name, err := reader.PeekPushNotificationName()
|
|
if err != nil {
|
|
t.Errorf("PeekPushNotificationName should not error for %s: %v", tc.name, err)
|
|
}
|
|
|
|
if name != tc.expected {
|
|
t.Errorf("Expected '%s', got '%s'", tc.expected, name)
|
|
}
|
|
})
|
|
}
|
|
})
|
|
|
|
t.Run("SpecialCharactersInName", func(t *testing.T) {
|
|
specialCases := []struct {
|
|
name string
|
|
notification string
|
|
}{
|
|
{"WithUnderscore", "test_notification"},
|
|
{"WithDash", "test-notification"},
|
|
{"WithNumbers", "test123"},
|
|
{"WithDots", "test.notification"},
|
|
{"WithColon", "test:notification"},
|
|
{"WithSlash", "test/notification"},
|
|
{"MixedCase", "TestNotification"},
|
|
{"AllCaps", "TESTNOTIFICATION"},
|
|
{"AllLower", "testnotification"},
|
|
{"Unicode", "tëst"},
|
|
}
|
|
|
|
for _, tc := range specialCases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
buf := createValidPushNotification(tc.notification, "data")
|
|
reader := createReaderWithPrimedBuffer(buf)
|
|
|
|
name, err := reader.PeekPushNotificationName()
|
|
if err != nil {
|
|
t.Errorf("PeekPushNotificationName should not error for '%s': %v", tc.notification, err)
|
|
}
|
|
|
|
if name != tc.notification {
|
|
t.Errorf("Expected '%s', got '%s'", tc.notification, name)
|
|
}
|
|
})
|
|
}
|
|
})
|
|
|
|
t.Run("IdempotentPeek", func(t *testing.T) {
|
|
// Test that multiple peeks return the same result
|
|
buf := createValidPushNotification("MOVING", "data")
|
|
reader := createReaderWithPrimedBuffer(buf)
|
|
|
|
// First peek
|
|
name1, err1 := reader.PeekPushNotificationName()
|
|
if err1 != nil {
|
|
t.Errorf("First PeekPushNotificationName should not error: %v", err1)
|
|
}
|
|
|
|
// Second peek should return the same result
|
|
name2, err2 := reader.PeekPushNotificationName()
|
|
if err2 != nil {
|
|
t.Errorf("Second PeekPushNotificationName should not error: %v", err2)
|
|
}
|
|
|
|
if name1 != name2 {
|
|
t.Errorf("Peek should be idempotent: first='%s', second='%s'", name1, name2)
|
|
}
|
|
|
|
if name1 != "MOVING" {
|
|
t.Errorf("Expected 'MOVING', got '%s'", name1)
|
|
}
|
|
})
|
|
}
|
|
|
|
// TestPeekPushNotificationNamePerformance tests performance characteristics
|
|
func TestPeekPushNotificationNamePerformance(t *testing.T) {
|
|
t.Run("RepeatedCalls", func(t *testing.T) {
|
|
// Test that repeated calls work correctly
|
|
buf := createValidPushNotification("TEST", "data")
|
|
reader := createReaderWithPrimedBuffer(buf)
|
|
|
|
// Call multiple times
|
|
for i := 0; i < 10; i++ {
|
|
name, err := reader.PeekPushNotificationName()
|
|
if err != nil {
|
|
t.Errorf("PeekPushNotificationName should not error on call %d: %v", i, err)
|
|
}
|
|
if name != "TEST" {
|
|
t.Errorf("Expected 'TEST' on call %d, got '%s'", i, name)
|
|
}
|
|
}
|
|
})
|
|
|
|
t.Run("LargeNotifications", func(t *testing.T) {
|
|
// Test with large notification data
|
|
largeData := strings.Repeat("x", 1000)
|
|
buf := createValidPushNotification("LARGE", largeData)
|
|
reader := createReaderWithPrimedBuffer(buf)
|
|
|
|
name, err := reader.PeekPushNotificationName()
|
|
if err != nil {
|
|
t.Errorf("PeekPushNotificationName should not error for large notification: %v", err)
|
|
}
|
|
|
|
if name != "LARGE" {
|
|
t.Errorf("Expected 'LARGE', got '%s'", name)
|
|
}
|
|
})
|
|
}
|
|
|
|
// TestPeekPushNotificationNameBehavior documents the method's behavior
|
|
func TestPeekPushNotificationNameBehavior(t *testing.T) {
|
|
t.Run("MethodBehavior", func(t *testing.T) {
|
|
// Test that the method works as intended:
|
|
// 1. Peek at the buffer without consuming it
|
|
// 2. Detect push notifications (RESP type '>')
|
|
// 3. Extract the notification name from the first element
|
|
// 4. Return the name for filtering decisions
|
|
|
|
buf := createValidPushNotification("MOVING", "slot_data")
|
|
reader := createReaderWithPrimedBuffer(buf)
|
|
|
|
// Peek should not consume the buffer
|
|
name, err := reader.PeekPushNotificationName()
|
|
if err != nil {
|
|
t.Errorf("PeekPushNotificationName should not error: %v", err)
|
|
}
|
|
|
|
if name != "MOVING" {
|
|
t.Errorf("Expected 'MOVING', got '%s'", name)
|
|
}
|
|
|
|
// Buffer should still be available for normal reading
|
|
replyType, err := reader.PeekReplyType()
|
|
if err != nil {
|
|
t.Errorf("PeekReplyType should work after PeekPushNotificationName: %v", err)
|
|
}
|
|
|
|
if replyType != RespPush {
|
|
t.Errorf("Expected RespPush, got %v", replyType)
|
|
}
|
|
})
|
|
|
|
t.Run("BufferNotConsumed", func(t *testing.T) {
|
|
// Verify that peeking doesn't consume the buffer
|
|
buf := createValidPushNotification("TEST", "data")
|
|
originalData := buf.Bytes()
|
|
reader := createReaderWithPrimedBuffer(buf)
|
|
|
|
// Peek the notification name
|
|
name, err := reader.PeekPushNotificationName()
|
|
if err != nil {
|
|
t.Errorf("PeekPushNotificationName should not error: %v", err)
|
|
}
|
|
|
|
if name != "TEST" {
|
|
t.Errorf("Expected 'TEST', got '%s'", name)
|
|
}
|
|
|
|
// Read the actual notification
|
|
reply, err := reader.ReadReply()
|
|
if err != nil {
|
|
t.Errorf("ReadReply should work after peek: %v", err)
|
|
}
|
|
|
|
// Verify we got the complete notification
|
|
if replySlice, ok := reply.([]interface{}); ok {
|
|
if len(replySlice) != 2 {
|
|
t.Errorf("Expected 2 elements, got %d", len(replySlice))
|
|
}
|
|
if replySlice[0] != "TEST" {
|
|
t.Errorf("Expected 'TEST', got %v", replySlice[0])
|
|
}
|
|
} else {
|
|
t.Errorf("Expected slice reply, got %T", reply)
|
|
}
|
|
|
|
// Verify buffer was properly consumed
|
|
if buf.Len() != 0 {
|
|
t.Errorf("Buffer should be empty after reading, but has %d bytes: %q", buf.Len(), buf.Bytes())
|
|
}
|
|
|
|
t.Logf("Original buffer size: %d bytes", len(originalData))
|
|
t.Logf("Successfully peeked and then read complete notification")
|
|
})
|
|
|
|
t.Run("ImplementationSuccess", func(t *testing.T) {
|
|
// Document that the implementation is now working correctly
|
|
t.Log("PeekPushNotificationName implementation status:")
|
|
t.Log("1. ✅ Correctly parses RESP3 push notifications")
|
|
t.Log("2. ✅ Extracts notification names properly")
|
|
t.Log("3. ✅ Handles buffer peeking without consumption")
|
|
t.Log("4. ✅ Works with various notification types")
|
|
t.Log("5. ✅ Supports empty notification names")
|
|
t.Log("")
|
|
t.Log("RESP3 format parsing:")
|
|
t.Log(">2\\r\\n$6\\r\\nMOVING\\r\\n$4\\r\\ndata\\r\\n")
|
|
t.Log("✅ Correctly identifies push notification marker (>)")
|
|
t.Log("✅ Skips array length (2)")
|
|
t.Log("✅ Parses string marker ($) and length (6)")
|
|
t.Log("✅ Extracts notification name (MOVING)")
|
|
t.Log("✅ Returns name without consuming buffer")
|
|
t.Log("")
|
|
t.Log("Note: Buffer must be primed with a peek operation first")
|
|
})
|
|
}
|