@@ -35,10 +35,6 @@ func TestMeshsyncLibraryWithK8sClusterCustomBrokerIntegration(t *testing.T) {
3535 }
3636}
3737
38- // TODO fix cyclop error
39- // integration-tests/k8s_cluster_meshsync_as_library_integration_test.go:47:1: calculated cyclomatic complexity for function runWithMeshsyncLibraryAndk8sClusterTestCase is 15, max is 10 (cyclop)
40- //
41- //nolint:cyclop
4238func runWithMeshsyncLibraryAndk8sClusterCustomBrokerTestCase (
4339 br broker.Handler ,
4440 tcIndex int ,
@@ -86,46 +82,15 @@ func runWithMeshsyncLibraryAndk8sClusterCustomBrokerTestCase(
8682 }
8783
8884 // 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 )
85+ runMeshsyncLibraryAsync (br , log , tc , errCh )
9986
10087 // intentionally big timeout to wait till the run execution ended
10188 timeout := time .Duration (time .Hour * 24 )
10289 if tc .waitMeshsyncTimeout > 0 {
10390 timeout = tc .waitMeshsyncTimeout
10491 }
10592
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- }
93+ handleLibraryCompletion (t , errCh , timeout , tc )
12994
13095 // Step 4: do final assertion, if any
13196 if tc .finalHandler != nil {
@@ -136,6 +101,7 @@ func runWithMeshsyncLibraryAndk8sClusterCustomBrokerTestCase(
136101 }
137102}
138103
104+ // introduced these below function to decrease cyclomatic complexity
139105func withMeshsyncLibraryAndk8sClusterCustomBrokerPrepareMeshsyncLoggerOptions (
140106 t * testing.T ,
141107 tcIndex int ,
@@ -165,3 +131,61 @@ func withMeshsyncLibraryAndk8sClusterCustomBrokerPrepareMeshsyncLoggerOptions(
165131
166132 return options , deferFunc
167133}
134+
135+ func runMeshsyncLibraryAsync (
136+ br broker.Handler ,
137+ log logger.Handler ,
138+ tc meshsyncLibraryWithK8SClusterCustomBrokerTestCaseStruct ,
139+ errCh chan <- error ,
140+ ) {
141+ go func () {
142+ runOptions := make ([]libmeshsync.OptionsSetter , 0 , len (tc .meshsyncRunOptions )+ 1 )
143+ runOptions = append (runOptions , tc .meshsyncRunOptions ... )
144+ runOptions = append (runOptions , libmeshsync .WithBrokerHandler (br ))
145+
146+ errCh <- libmeshsync .Run (log , runOptions ... )
147+ }()
148+ }
149+
150+ func handleLibraryCompletion (
151+ t * testing.T ,
152+ errCh <- chan error ,
153+ timeout time.Duration ,
154+ tc meshsyncLibraryWithK8SClusterCustomBrokerTestCaseStruct ,
155+ ) {
156+ select {
157+ case err := <- errCh :
158+ validateErrorOutcome (t , err , tc )
159+ case <- time .After (timeout ):
160+ self , findErr := os .FindProcess (os .Getpid ())
161+ if findErr != nil {
162+ t .Fatalf ("could not find self process: %v" , findErr )
163+ }
164+ if err := self .Signal (syscall .SIGTERM ); err != nil {
165+ t .Fatalf ("error terminating meshsync library: %v" , err )
166+ }
167+ t .Logf ("processing after timeout %d" , timeout )
168+ }
169+ }
170+
171+ func validateErrorOutcome (
172+ t * testing.T ,
173+ err error ,
174+ tc meshsyncLibraryWithK8SClusterCustomBrokerTestCaseStruct ,
175+ ) {
176+ if err != nil {
177+ if ! tc .expectError {
178+ t .Fatal ("must not end with error" , err )
179+ }
180+ assert .ErrorContains (t , err , tc .expectedErrorMessage , "must end with expected error" )
181+ return
182+ }
183+
184+ // err == nil
185+ if tc .expectError {
186+ if tc .expectedErrorMessage != "" {
187+ t .Fatalf ("must end with expected error message %s" , tc .expectedErrorMessage )
188+ }
189+ t .Fatalf ("must end with error" )
190+ }
191+ }
0 commit comments