@@ -9,6 +9,18 @@ import (
9
9
"github.com/redis/go-redis/v9/internal/pool"
10
10
)
11
11
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
+
12
24
// testHandler is a simple implementation of PushNotificationHandler for testing
13
25
type testHandler struct {
14
26
handlerFunc func (ctx context.Context , notification []interface {}) bool
@@ -84,8 +96,10 @@ func TestPushNotificationProcessor(t *testing.T) {
84
96
// Test the push notification processor
85
97
processor := redis .NewPushNotificationProcessor ()
86
98
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" )
89
103
}
90
104
91
105
// Test registering handlers
@@ -106,10 +120,15 @@ func TestPushNotificationProcessor(t *testing.T) {
106
120
t .Fatalf ("Failed to register handler: %v" , err )
107
121
}
108
122
109
- // Simulate handling a notification
123
+ // Simulate handling a notification using GetHandler
110
124
ctx := context .Background ()
111
125
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 )
113
132
114
133
if ! handled {
115
134
t .Error ("Notification should have been handled" )
@@ -119,9 +138,10 @@ func TestPushNotificationProcessor(t *testing.T) {
119
138
t .Error ("Specific handler should have been called" )
120
139
}
121
140
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" )
125
145
}
126
146
}
127
147
@@ -140,7 +160,7 @@ func TestClientPushNotificationIntegration(t *testing.T) {
140
160
t .Error ("Push notification processor should be initialized" )
141
161
}
142
162
143
- if processor . GetRegistry ( ) == nil {
163
+ if getRegistryForTesting ( processor ) == nil {
144
164
t .Error ("Push notification processor should have a registry when enabled" )
145
165
}
146
166
@@ -157,7 +177,7 @@ func TestClientPushNotificationIntegration(t *testing.T) {
157
177
// Simulate notification handling
158
178
ctx := context .Background ()
159
179
notification := []interface {}{"CUSTOM_EVENT" , "test_data" }
160
- handled := processor . GetRegistry ( ).HandleNotification (ctx , notification )
180
+ handled := getRegistryForTesting ( processor ).HandleNotification (ctx , notification )
161
181
162
182
if ! handled {
163
183
t .Error ("Notification should have been handled" )
@@ -183,7 +203,7 @@ func TestClientWithoutPushNotifications(t *testing.T) {
183
203
t .Error ("Push notification processor should never be nil" )
184
204
}
185
205
// VoidPushNotificationProcessor should have nil registry
186
- if processor . GetRegistry ( ) != nil {
206
+ if getRegistryForTesting ( processor ) != nil {
187
207
t .Error ("VoidPushNotificationProcessor should have nil registry" )
188
208
}
189
209
@@ -211,8 +231,9 @@ func TestPushNotificationEnabledClient(t *testing.T) {
211
231
t .Error ("Push notification processor should be initialized when enabled" )
212
232
}
213
233
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 )
216
237
}
217
238
218
239
// Test registering a handler
@@ -226,7 +247,6 @@ func TestPushNotificationEnabledClient(t *testing.T) {
226
247
}
227
248
228
249
// Test that the handler works
229
- registry := processor .GetRegistry ()
230
250
ctx := context .Background ()
231
251
notification := []interface {}{"TEST_NOTIFICATION" , "data" }
232
252
handled := registry .HandleNotification (ctx , notification )
@@ -375,7 +395,7 @@ func TestPubSubWithGenericPushNotifications(t *testing.T) {
375
395
376
396
// Test that the processor can handle notifications
377
397
notification := []interface {}{"CUSTOM_PUBSUB_EVENT" , "arg1" , "arg2" }
378
- handled := processor . GetRegistry ( ).HandleNotification (context .Background (), notification )
398
+ handled := getRegistryForTesting ( processor ).HandleNotification (context .Background (), notification )
379
399
380
400
if ! handled {
381
401
t .Error ("Push notification should have been handled" )
@@ -559,7 +579,7 @@ func TestPushNotificationProcessorEdgeCases(t *testing.T) {
559
579
// Test processor with disabled state
560
580
processor := redis .NewPushNotificationProcessor ()
561
581
562
- if processor . GetRegistry ( ) == nil {
582
+ if getRegistryForTesting ( processor ) == nil {
563
583
t .Error ("Processor should have a registry" )
564
584
}
565
585
@@ -573,7 +593,7 @@ func TestPushNotificationProcessorEdgeCases(t *testing.T) {
573
593
// Even with handlers registered, disabled processor shouldn't process
574
594
ctx := context .Background ()
575
595
notification := []interface {}{"TEST_CMD" , "data" }
576
- handled := processor . GetRegistry ( ).HandleNotification (ctx , notification )
596
+ handled := getRegistryForTesting ( processor ).HandleNotification (ctx , notification )
577
597
578
598
if ! handled {
579
599
t .Error ("Registry should still handle notifications even when processor is disabled" )
@@ -584,7 +604,7 @@ func TestPushNotificationProcessorEdgeCases(t *testing.T) {
584
604
}
585
605
586
606
// Test that processor always has a registry
587
- if processor . GetRegistry ( ) == nil {
607
+ if getRegistryForTesting ( processor ) == nil {
588
608
t .Error ("Processor should always have a registry" )
589
609
}
590
610
}
@@ -619,7 +639,7 @@ func TestPushNotificationProcessorConvenienceMethods(t *testing.T) {
619
639
620
640
// Test specific handler
621
641
notification := []interface {}{"CONV_CMD" , "data" }
622
- handled := processor . GetRegistry ( ).HandleNotification (ctx , notification )
642
+ handled := getRegistryForTesting ( processor ).HandleNotification (ctx , notification )
623
643
624
644
if ! handled {
625
645
t .Error ("Notification should be handled" )
@@ -635,7 +655,7 @@ func TestPushNotificationProcessorConvenienceMethods(t *testing.T) {
635
655
636
656
// Test func handler
637
657
notification = []interface {}{"FUNC_CMD" , "data" }
638
- handled = processor . GetRegistry ( ).HandleNotification (ctx , notification )
658
+ handled = getRegistryForTesting ( processor ).HandleNotification (ctx , notification )
639
659
640
660
if ! handled {
641
661
t .Error ("Notification should be handled" )
@@ -676,7 +696,7 @@ func TestClientPushNotificationEdgeCases(t *testing.T) {
676
696
t .Error ("Processor should never be nil" )
677
697
}
678
698
// VoidPushNotificationProcessor should have nil registry
679
- if processor . GetRegistry ( ) != nil {
699
+ if getRegistryForTesting ( processor ) != nil {
680
700
t .Error ("VoidPushNotificationProcessor should have nil registry when disabled" )
681
701
}
682
702
}
@@ -838,10 +858,10 @@ func TestPushNotificationProcessorConcurrency(t *testing.T) {
838
858
839
859
// Handle notifications
840
860
notification := []interface {}{command , "data" }
841
- processor . GetRegistry ( ).HandleNotification (context .Background (), notification )
861
+ getRegistryForTesting ( processor ).HandleNotification (context .Background (), notification )
842
862
843
863
// Access processor state
844
- processor . GetRegistry ( )
864
+ getRegistryForTesting ( processor )
845
865
}
846
866
}(i )
847
867
}
@@ -852,7 +872,7 @@ func TestPushNotificationProcessorConcurrency(t *testing.T) {
852
872
}
853
873
854
874
// Verify processor is still functional
855
- registry := processor . GetRegistry ( )
875
+ registry := getRegistryForTesting ( processor )
856
876
if registry == nil {
857
877
t .Error ("Processor registry should not be nil after concurrent operations" )
858
878
}
@@ -887,7 +907,7 @@ func TestPushNotificationClientConcurrency(t *testing.T) {
887
907
// Access processor
888
908
processor := client .GetPushNotificationProcessor ()
889
909
if processor != nil {
890
- processor . GetRegistry ( )
910
+ getRegistryForTesting ( processor )
891
911
}
892
912
}
893
913
}(i )
@@ -921,7 +941,7 @@ func TestPushNotificationConnectionHealthCheck(t *testing.T) {
921
941
if processor == nil {
922
942
t .Fatal ("Push notification processor should not be nil" )
923
943
}
924
- if processor . GetRegistry ( ) == nil {
944
+ if getRegistryForTesting ( processor ) == nil {
925
945
t .Fatal ("Push notification registry should not be nil when enabled" )
926
946
}
927
947
0 commit comments