Skip to content

Commit 5a21d10

Browse files
committed
Reduce cyclomatic complexity by using seperate function
Signed-off-by: Raunak Madan <[email protected]>
1 parent e34c563 commit 5a21d10

File tree

2 files changed

+94
-54
lines changed

2 files changed

+94
-54
lines changed

integration-tests/meshsync_as_binary_with_k8s_cluster_integration_test.go

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,6 @@ func TestMeshsyncBinaryWithK8sClusterIntegration(t *testing.T) {
5555
}
5656
}
5757

58-
// need this as separate function to bring down cyclomatic complexity
59-
// this one itself is also already too complicated :)
60-
//
6158
// TODO fix cyclop error
6259
// integration-tests/k8s_cluster_integration_test.go:74:1: calculated cyclomatic complexity for function runWithMeshsyncBinaryAndk8sClusterMeshsyncBinaryTestCase is 11, max is 10 (cyclop)
6360
//
@@ -103,28 +100,14 @@ func runMeshsyncBinaryWithK8sClusterTestCase(
103100
if err := cmd.Start(); err != nil {
104101
t.Fatalf("error starting binary: %v", err)
105102
}
106-
errCh := make(chan error)
107-
go func(cmd0 *exec.Cmd, errCh0 chan<- error) {
108-
errCh0 <- cmd0.Wait()
109-
}(cmd, errCh)
110103

111104
// intentionally big timeout to wait till the cmd execution ended
112105
timeout := time.Duration(time.Hour * 24)
113106
if tc.waitMeshsyncTimeout > 0 {
114107
timeout = tc.waitMeshsyncTimeout
115108
}
116109

117-
select {
118-
case err := <-errCh:
119-
if err != nil {
120-
t.Fatalf("error running binary: %v", err)
121-
}
122-
case <-time.After(timeout):
123-
if err := cmd.Process.Signal(syscall.SIGTERM); err != nil {
124-
t.Fatalf("error terminating meshsync command: %v", err)
125-
}
126-
t.Logf("processing after timeout %d", timeout)
127-
}
110+
waitForMeshsync(t, cmd, timeout)
128111

129112
// Step 4: do final assertion, if any
130113
if tc.finalHandler != nil {
@@ -135,7 +118,7 @@ func runMeshsyncBinaryWithK8sClusterTestCase(
135118
}
136119
}
137120

138-
// introduced this function to decrease cyclomatic complexity
121+
// introduced these below function to decrease cyclomatic complexity
139122
func withMeshsyncBinaryPrepareMeshsyncCMD(
140123
t *testing.T,
141124
tcIndex int,
@@ -166,3 +149,30 @@ func withMeshsyncBinaryPrepareMeshsyncCMD(
166149

167150
return cmd, deferFunc
168151
}
152+
153+
func waitForMeshsync(
154+
t *testing.T,
155+
cmd *exec.Cmd,
156+
timeout time.Duration,
157+
) {
158+
errCh := make(chan error)
159+
160+
go func(cmd0 *exec.Cmd, errCh0 chan<- error) {
161+
errCh0 <- cmd0.Wait()
162+
}(cmd, errCh)
163+
164+
select {
165+
case err := <-errCh:
166+
if err != nil {
167+
t.Fatalf("error running binary: %v", err)
168+
}
169+
case <-time.After(timeout):
170+
if err := cmd.Process.Signal(syscall.SIGTERM); err != nil {
171+
t.Fatalf("error terminating meshsync command: %v", err)
172+
}
173+
t.Logf("processing after timeout %d", timeout)
174+
175+
}
176+
}
177+
178+

integration-tests/meshsync_as_library_with_k8s_cluster_custom_broker_integration_test.go

Lines changed: 65 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -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
139109
func 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

Comments
 (0)