1+ package protocol
2+
3+ import (
4+ "net"
5+ "testing"
6+ "time"
7+
8+ "google.golang.org/grpc"
9+ "google.golang.org/grpc/test/bufconn"
10+ )
11+
12+ func TestWithSubscribeOption (t * testing.T ) {
13+ lis := bufconn .Listen (1024 )
14+ defer lis .Close ()
15+
16+ s := grpc .NewServer ()
17+ defer s .Stop ()
18+
19+ conn , err := grpc .Dial ("bufnet" , grpc .WithInsecure (), grpc .WithDialer (func (string , time.Duration ) (net.Conn , error ) {
20+ return lis .Dial ()
21+ }))
22+ if err != nil {
23+ t .Fatalf ("Failed to create connection: %v" , err )
24+ }
25+ defer conn .Close ()
26+
27+ // Test valid subscribe option
28+ subscribeOpt := & SubscribeOption {
29+ Source : "test-source" ,
30+ ClusterName : "test-cluster" ,
31+ DataType : "test-type" ,
32+ }
33+
34+ p , err := NewProtocol (conn , WithSubscribeOption (subscribeOpt ))
35+ if err != nil {
36+ t .Fatalf ("Failed to create protocol: %v" , err )
37+ }
38+
39+ if p .subscribeOption != subscribeOpt {
40+ t .Error ("Subscribe option was not set correctly" )
41+ }
42+
43+ if p .subscribeOption .Source != "test-source" {
44+ t .Errorf ("Expected source 'test-source', got '%s'" , p .subscribeOption .Source )
45+ }
46+
47+ // Test nil subscribe option
48+ _ , err = NewProtocol (conn , WithSubscribeOption (nil ))
49+ if err == nil {
50+ t .Error ("Expected error for nil subscribe option" )
51+ }
52+ }
53+
54+ func TestWithReconnectErrorOption (t * testing.T ) {
55+ lis := bufconn .Listen (1024 )
56+ defer lis .Close ()
57+
58+ s := grpc .NewServer ()
59+ defer s .Stop ()
60+
61+ conn , err := grpc .Dial ("bufnet" , grpc .WithInsecure (), grpc .WithDialer (func (string , time.Duration ) (net.Conn , error ) {
62+ return lis .Dial ()
63+ }))
64+ if err != nil {
65+ t .Fatalf ("Failed to create connection: %v" , err )
66+ }
67+ defer conn .Close ()
68+
69+ // Test valid reconnect error option
70+ errorChan := make (chan error , 1 )
71+ interval := 5 * time .Second
72+
73+ p , err := NewProtocol (conn , WithReconnectErrorOption (errorChan , interval ))
74+ if err != nil {
75+ t .Fatalf ("Failed to create protocol: %v" , err )
76+ }
77+
78+ if p .reconnectErrorChan != errorChan {
79+ t .Error ("Reconnect error channel was not set correctly" )
80+ }
81+
82+ if p .checkInterval != interval {
83+ t .Errorf ("Expected interval %v, got %v" , interval , p .checkInterval )
84+ }
85+
86+ // Test with zero interval (should default to 20 seconds)
87+ p , err = NewProtocol (conn , WithReconnectErrorOption (errorChan , 0 ))
88+ if err != nil {
89+ t .Fatalf ("Failed to create protocol: %v" , err )
90+ }
91+
92+ if p .checkInterval != 20 * time .Second {
93+ t .Errorf ("Expected default interval 20s, got %v" , p .checkInterval )
94+ }
95+
96+ // Test with negative interval (should default to 20 seconds)
97+ p , err = NewProtocol (conn , WithReconnectErrorOption (errorChan , - 1 * time .Second ))
98+ if err != nil {
99+ t .Fatalf ("Failed to create protocol: %v" , err )
100+ }
101+
102+ if p .checkInterval != 20 * time .Second {
103+ t .Errorf ("Expected default interval 20s, got %v" , p .checkInterval )
104+ }
105+
106+ // Test nil error channel
107+ _ , err = NewProtocol (conn , WithReconnectErrorOption (nil , interval ))
108+ if err == nil {
109+ t .Error ("Expected error for nil reconnect error channel" )
110+ }
111+ }
112+
113+ func TestWithHealthCheck (t * testing.T ) {
114+ lis := bufconn .Listen (1024 )
115+ defer lis .Close ()
116+
117+ s := grpc .NewServer ()
118+ defer s .Stop ()
119+
120+ conn , err := grpc .Dial ("bufnet" , grpc .WithInsecure (), grpc .WithDialer (func (string , time.Duration ) (net.Conn , error ) {
121+ return lis .Dial ()
122+ }))
123+ if err != nil {
124+ t .Fatalf ("Failed to create connection: %v" , err )
125+ }
126+ defer conn .Close ()
127+
128+ // Test WithHealthCheck (which is an alias for WithReconnectErrorOption)
129+ errorChan := make (chan error , 1 )
130+ interval := 3 * time .Second
131+
132+ p , err := NewProtocol (conn , WithHealthCheck (interval , errorChan ))
133+ if err != nil {
134+ t .Fatalf ("Failed to create protocol: %v" , err )
135+ }
136+
137+ if p .reconnectErrorChan != errorChan {
138+ t .Error ("Health check error channel was not set correctly" )
139+ }
140+
141+ if p .checkInterval != interval {
142+ t .Errorf ("Expected health check interval %v, got %v" , interval , p .checkInterval )
143+ }
144+
145+ // Test nil error channel for health check
146+ _ , err = NewProtocol (conn , WithHealthCheck (interval , nil ))
147+ if err == nil {
148+ t .Error ("Expected error for nil health check error channel" )
149+ }
150+ }
151+
152+ func TestMultipleOptions (t * testing.T ) {
153+ lis := bufconn .Listen (1024 )
154+ defer lis .Close ()
155+
156+ s := grpc .NewServer ()
157+ defer s .Stop ()
158+
159+ conn , err := grpc .Dial ("bufnet" , grpc .WithInsecure (), grpc .WithDialer (func (string , time.Duration ) (net.Conn , error ) {
160+ return lis .Dial ()
161+ }))
162+ if err != nil {
163+ t .Fatalf ("Failed to create connection: %v" , err )
164+ }
165+ defer conn .Close ()
166+
167+ // Test applying multiple options
168+ subscribeOpt := & SubscribeOption {
169+ Source : "test-source" ,
170+ DataType : "test-type" ,
171+ }
172+ errorChan := make (chan error , 1 )
173+ interval := 2 * time .Second
174+
175+ p , err := NewProtocol (conn ,
176+ WithSubscribeOption (subscribeOpt ),
177+ WithHealthCheck (interval , errorChan ),
178+ )
179+ if err != nil {
180+ t .Fatalf ("Failed to create protocol: %v" , err )
181+ }
182+
183+ // Verify both options were applied
184+ if p .subscribeOption != subscribeOpt {
185+ t .Error ("Subscribe option was not set correctly" )
186+ }
187+
188+ if p .reconnectErrorChan != errorChan {
189+ t .Error ("Health check error channel was not set correctly" )
190+ }
191+
192+ if p .checkInterval != interval {
193+ t .Errorf ("Expected health check interval %v, got %v" , interval , p .checkInterval )
194+ }
195+ }
0 commit comments