@@ -86,47 +86,16 @@ func runWithMeshsyncLibraryAndk8sClusterCustomBrokerTestCase(
8686 }
8787
8888 // Step 3: run meshsync library
89- go func (errCh0 chan <- error ) {
90- runOptions := make ([]libmeshsync.OptionsSetter , 0 , len (tc .meshsyncRunOptions ))
91- runOptions = append (runOptions , tc .meshsyncRunOptions ... )
92- runOptions = append (runOptions , libmeshsync .WithBrokerHandler (br ))
93-
94- errCh0 <- libmeshsync .Run (
95- log ,
96- runOptions ... ,
97- )
98- }(errCh )
99-
89+ runMeshsyncLibraryAsync (br , log , tc , errCh )
90+
10091 // intentionally big timeout to wait till the run execution ended
10192 timeout := time .Duration (time .Hour * 24 )
10293 if tc .waitMeshsyncTimeout > 0 {
10394 timeout = tc .waitMeshsyncTimeout
10495 }
10596
106- select {
107- case err := <- errCh :
108- if err != nil {
109- if ! tc .expectError {
110- t .Fatal ("must not end with error" , err )
111- }
112- assert .ErrorContains (t , err , tc .expectedErrorMessage , "must end with expected error" )
113- } else if tc .expectError {
114- if tc .expectedErrorMessage != "" {
115- t .Fatalf ("must end with expected error message %s" , tc .expectedErrorMessage )
116- }
117- t .Fatalf ("must end with error" )
118- }
119- case <- time .After (timeout ):
120- self , err := os .FindProcess (os .Getpid ())
121- if err != nil {
122- t .Fatalf ("could not find self process: %v" , err )
123- }
124- if err := self .Signal (syscall .SIGTERM ); err != nil {
125- t .Fatalf ("error terminating meshsync library: %v" , err )
126- }
127- t .Logf ("processing after timeout %d" , timeout )
128- }
129-
97+ handleLibraryCompletion (t , errCh , timeout , tc )
98+
13099 // Step 4: do final assertion, if any
131100 if tc .finalHandler != nil {
132101 tc .finalHandler (t , resultData )
@@ -136,6 +105,7 @@ func runWithMeshsyncLibraryAndk8sClusterCustomBrokerTestCase(
136105 }
137106}
138107
108+ // introduced these below function to decrease cyclomatic complexity
139109func withMeshsyncLibraryAndk8sClusterCustomBrokerPrepareMeshsyncLoggerOptions (
140110 t * testing.T ,
141111 tcIndex int ,
@@ -165,3 +135,63 @@ func withMeshsyncLibraryAndk8sClusterCustomBrokerPrepareMeshsyncLoggerOptions(
165135
166136 return options , deferFunc
167137}
138+
139+ func runMeshsyncLibraryAsync (
140+ br broker.Handler ,
141+ log logger.Handler ,
142+ tc meshsyncLibraryWithK8SClusterCustomBrokerTestCaseStruct ,
143+ errCh chan <- error ,
144+ ) {
145+ go func () {
146+ runOptions := make ([]libmeshsync.OptionsSetter , 0 , len (tc .meshsyncRunOptions ))
147+ runOptions = append (runOptions , tc .meshsyncRunOptions ... )
148+ runOptions = append (runOptions , libmeshsync .WithBrokerHandler (br ))
149+
150+ errCh <- libmeshsync .Run (log , runOptions ... )
151+ }()
152+ }
153+
154+ func handleLibraryCompletion (
155+ t * testing.T ,
156+ errCh <- chan error ,
157+ timeout time.Duration ,
158+ tc meshsyncLibraryWithK8SClusterCustomBrokerTestCaseStruct ,
159+ ) {
160+ select {
161+ case err := <- errCh :
162+ validateErrorOutcome (t , err , tc )
163+ case <- time .After (timeout ):
164+ self , findErr := os .FindProcess (os .Getpid ())
165+ if findErr != nil {
166+ t .Fatalf ("could not find self process: %v" , findErr )
167+ }
168+ if err := self .Signal (syscall .SIGTERM ); err != nil {
169+ t .Fatalf ("error terminating meshsync library: %v" , err )
170+ }
171+ t .Logf ("processing after timeout %d" , timeout )
172+ }
173+ }
174+
175+ func validateErrorOutcome (
176+ t * testing.T ,
177+ err error ,
178+ tc meshsyncLibraryWithK8SClusterCustomBrokerTestCaseStruct ,
179+ ) {
180+ if err != nil {
181+ if ! tc .expectError {
182+ t .Fatal ("must not end with error" , err )
183+ }
184+ assert .ErrorContains (t , err , tc .expectedErrorMessage , "must end with expected error" )
185+ return
186+ }
187+
188+ // err == nil
189+ if tc .expectError {
190+ if tc .expectedErrorMessage != "" {
191+ t .Fatalf ("must end with expected error message %s" , tc .expectedErrorMessage )
192+ }
193+ t .Fatalf ("must end with error" )
194+ }
195+ }
196+
197+
0 commit comments