@@ -9,6 +9,18 @@ import (
99 "github.com/redis/go-redis/v9/internal/pool"
1010)
1111
12+ // Helper function to access registry for testing
13+ func getRegistryForTesting (processor redis.PushNotificationProcessorInterface ) * redis.PushNotificationRegistry {
14+ switch p := processor .(type ) {
15+ case * redis.PushNotificationProcessor :
16+ return p .GetRegistryForTesting ()
17+ case * redis.VoidPushNotificationProcessor :
18+ return p .GetRegistryForTesting ()
19+ default :
20+ return nil
21+ }
22+ }
23+
1224// testHandler is a simple implementation of PushNotificationHandler for testing
1325type testHandler struct {
1426 handlerFunc func (ctx context.Context , notification []interface {}) bool
@@ -84,8 +96,10 @@ func TestPushNotificationProcessor(t *testing.T) {
8496 // Test the push notification processor
8597 processor := redis .NewPushNotificationProcessor ()
8698
87- if processor .GetRegistry () == nil {
88- t .Error ("Processor should have a registry" )
99+ // Test that we can get a handler (should be nil since none registered yet)
100+ handler := processor .GetHandler ("TEST" )
101+ if handler != nil {
102+ t .Error ("Should not have handler for TEST initially" )
89103 }
90104
91105 // Test registering handlers
@@ -106,10 +120,15 @@ func TestPushNotificationProcessor(t *testing.T) {
106120 t .Fatalf ("Failed to register handler: %v" , err )
107121 }
108122
109- // Simulate handling a notification
123+ // Simulate handling a notification using GetHandler
110124 ctx := context .Background ()
111125 notification := []interface {}{"CUSTOM_NOTIFICATION" , "data" }
112- handled := processor .GetRegistry ().HandleNotification (ctx , notification )
126+ customHandler := processor .GetHandler ("CUSTOM_NOTIFICATION" )
127+ if customHandler == nil {
128+ t .Error ("Should have handler for CUSTOM_NOTIFICATION after registration" )
129+ return
130+ }
131+ handled := customHandler .HandlePushNotification (ctx , notification )
113132
114133 if ! handled {
115134 t .Error ("Notification should have been handled" )
@@ -119,9 +138,10 @@ func TestPushNotificationProcessor(t *testing.T) {
119138 t .Error ("Specific handler should have been called" )
120139 }
121140
122- // Test that processor always has a registry (no enable/disable anymore)
123- if processor .GetRegistry () == nil {
124- t .Error ("Processor should always have a registry" )
141+ // Test that processor can retrieve handlers (no enable/disable anymore)
142+ retrievedHandler := processor .GetHandler ("CUSTOM_NOTIFICATION" )
143+ if retrievedHandler == nil {
144+ t .Error ("Should be able to retrieve registered handler" )
125145 }
126146}
127147
@@ -140,7 +160,7 @@ func TestClientPushNotificationIntegration(t *testing.T) {
140160 t .Error ("Push notification processor should be initialized" )
141161 }
142162
143- if processor . GetRegistry ( ) == nil {
163+ if getRegistryForTesting ( processor ) == nil {
144164 t .Error ("Push notification processor should have a registry when enabled" )
145165 }
146166
@@ -157,7 +177,7 @@ func TestClientPushNotificationIntegration(t *testing.T) {
157177 // Simulate notification handling
158178 ctx := context .Background ()
159179 notification := []interface {}{"CUSTOM_EVENT" , "test_data" }
160- handled := processor . GetRegistry ( ).HandleNotification (ctx , notification )
180+ handled := getRegistryForTesting ( processor ).HandleNotification (ctx , notification )
161181
162182 if ! handled {
163183 t .Error ("Notification should have been handled" )
@@ -183,7 +203,7 @@ func TestClientWithoutPushNotifications(t *testing.T) {
183203 t .Error ("Push notification processor should never be nil" )
184204 }
185205 // VoidPushNotificationProcessor should have nil registry
186- if processor . GetRegistry ( ) != nil {
206+ if getRegistryForTesting ( processor ) != nil {
187207 t .Error ("VoidPushNotificationProcessor should have nil registry" )
188208 }
189209
@@ -211,8 +231,9 @@ func TestPushNotificationEnabledClient(t *testing.T) {
211231 t .Error ("Push notification processor should be initialized when enabled" )
212232 }
213233
214- if processor .GetRegistry () == nil {
215- t .Error ("Push notification processor should have a registry when enabled" )
234+ registry := getRegistryForTesting (processor )
235+ if registry == nil {
236+ t .Errorf ("Push notification processor should have a registry when enabled. Processor type: %T" , processor )
216237 }
217238
218239 // Test registering a handler
@@ -226,7 +247,6 @@ func TestPushNotificationEnabledClient(t *testing.T) {
226247 }
227248
228249 // Test that the handler works
229- registry := processor .GetRegistry ()
230250 ctx := context .Background ()
231251 notification := []interface {}{"TEST_NOTIFICATION" , "data" }
232252 handled := registry .HandleNotification (ctx , notification )
@@ -375,7 +395,7 @@ func TestPubSubWithGenericPushNotifications(t *testing.T) {
375395
376396 // Test that the processor can handle notifications
377397 notification := []interface {}{"CUSTOM_PUBSUB_EVENT" , "arg1" , "arg2" }
378- handled := processor . GetRegistry ( ).HandleNotification (context .Background (), notification )
398+ handled := getRegistryForTesting ( processor ).HandleNotification (context .Background (), notification )
379399
380400 if ! handled {
381401 t .Error ("Push notification should have been handled" )
@@ -559,7 +579,7 @@ func TestPushNotificationProcessorEdgeCases(t *testing.T) {
559579 // Test processor with disabled state
560580 processor := redis .NewPushNotificationProcessor ()
561581
562- if processor . GetRegistry ( ) == nil {
582+ if getRegistryForTesting ( processor ) == nil {
563583 t .Error ("Processor should have a registry" )
564584 }
565585
@@ -573,7 +593,7 @@ func TestPushNotificationProcessorEdgeCases(t *testing.T) {
573593 // Even with handlers registered, disabled processor shouldn't process
574594 ctx := context .Background ()
575595 notification := []interface {}{"TEST_CMD" , "data" }
576- handled := processor . GetRegistry ( ).HandleNotification (ctx , notification )
596+ handled := getRegistryForTesting ( processor ).HandleNotification (ctx , notification )
577597
578598 if ! handled {
579599 t .Error ("Registry should still handle notifications even when processor is disabled" )
@@ -584,7 +604,7 @@ func TestPushNotificationProcessorEdgeCases(t *testing.T) {
584604 }
585605
586606 // Test that processor always has a registry
587- if processor . GetRegistry ( ) == nil {
607+ if getRegistryForTesting ( processor ) == nil {
588608 t .Error ("Processor should always have a registry" )
589609 }
590610}
@@ -619,7 +639,7 @@ func TestPushNotificationProcessorConvenienceMethods(t *testing.T) {
619639
620640 // Test specific handler
621641 notification := []interface {}{"CONV_CMD" , "data" }
622- handled := processor . GetRegistry ( ).HandleNotification (ctx , notification )
642+ handled := getRegistryForTesting ( processor ).HandleNotification (ctx , notification )
623643
624644 if ! handled {
625645 t .Error ("Notification should be handled" )
@@ -635,7 +655,7 @@ func TestPushNotificationProcessorConvenienceMethods(t *testing.T) {
635655
636656 // Test func handler
637657 notification = []interface {}{"FUNC_CMD" , "data" }
638- handled = processor . GetRegistry ( ).HandleNotification (ctx , notification )
658+ handled = getRegistryForTesting ( processor ).HandleNotification (ctx , notification )
639659
640660 if ! handled {
641661 t .Error ("Notification should be handled" )
@@ -676,7 +696,7 @@ func TestClientPushNotificationEdgeCases(t *testing.T) {
676696 t .Error ("Processor should never be nil" )
677697 }
678698 // VoidPushNotificationProcessor should have nil registry
679- if processor . GetRegistry ( ) != nil {
699+ if getRegistryForTesting ( processor ) != nil {
680700 t .Error ("VoidPushNotificationProcessor should have nil registry when disabled" )
681701 }
682702}
@@ -838,10 +858,10 @@ func TestPushNotificationProcessorConcurrency(t *testing.T) {
838858
839859 // Handle notifications
840860 notification := []interface {}{command , "data" }
841- processor . GetRegistry ( ).HandleNotification (context .Background (), notification )
861+ getRegistryForTesting ( processor ).HandleNotification (context .Background (), notification )
842862
843863 // Access processor state
844- processor . GetRegistry ( )
864+ getRegistryForTesting ( processor )
845865 }
846866 }(i )
847867 }
@@ -852,7 +872,7 @@ func TestPushNotificationProcessorConcurrency(t *testing.T) {
852872 }
853873
854874 // Verify processor is still functional
855- registry := processor . GetRegistry ( )
875+ registry := getRegistryForTesting ( processor )
856876 if registry == nil {
857877 t .Error ("Processor registry should not be nil after concurrent operations" )
858878 }
@@ -887,7 +907,7 @@ func TestPushNotificationClientConcurrency(t *testing.T) {
887907 // Access processor
888908 processor := client .GetPushNotificationProcessor ()
889909 if processor != nil {
890- processor . GetRegistry ( )
910+ getRegistryForTesting ( processor )
891911 }
892912 }
893913 }(i )
@@ -921,7 +941,7 @@ func TestPushNotificationConnectionHealthCheck(t *testing.T) {
921941 if processor == nil {
922942 t .Fatal ("Push notification processor should not be nil" )
923943 }
924- if processor . GetRegistry ( ) == nil {
944+ if getRegistryForTesting ( processor ) == nil {
925945 t .Fatal ("Push notification registry should not be nil when enabled" )
926946 }
927947
0 commit comments