forked from argoproj/argo-cd
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding tests for githandlers (argoproj#16678)
Signed-off-by: zhaque44 <[email protected]>
- Loading branch information
1 parent
d336b7a
commit bdfbf14
Showing
1 changed file
with
122 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
package metrics | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"golang.org/x/sync/semaphore" | ||
) | ||
|
||
func TestMain(m *testing.M) { | ||
os.Exit(m.Run()) | ||
} | ||
|
||
func TestEdgeCasesAndErrorHandling(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
setup func() | ||
teardown func() | ||
testFunc func(t *testing.T) | ||
}{ | ||
{ | ||
name: "lsRemoteParallelismLimitSemaphore is nil", | ||
testFunc: func(t *testing.T) { | ||
lsRemoteParallelismLimitSemaphore = nil | ||
assert.NotPanics(t, func() { | ||
NewGitClientEventHandlers(&MetricsServer{}) | ||
}) | ||
}, | ||
}, | ||
{ | ||
name: "lsRemoteParallelismLimitSemaphore is not nil", | ||
setup: func() { | ||
lsRemoteParallelismLimitSemaphore = semaphore.NewWeighted(1) | ||
}, | ||
teardown: func() { | ||
lsRemoteParallelismLimitSemaphore = nil | ||
}, | ||
testFunc: func(t *testing.T) { | ||
assert.NotPanics(t, func() { | ||
NewGitClientEventHandlers(&MetricsServer{}) | ||
}) | ||
}, | ||
}, | ||
{ | ||
name: "lsRemoteParallelismLimitSemaphore is not nil and Acquire returns error", | ||
setup: func() { | ||
lsRemoteParallelismLimitSemaphore = semaphore.NewWeighted(1) | ||
}, | ||
teardown: func() { | ||
lsRemoteParallelismLimitSemaphore = nil | ||
}, | ||
testFunc: func(t *testing.T) { | ||
assert.NotPanics(t, func() { | ||
NewGitClientEventHandlers(&MetricsServer{}) | ||
}) | ||
}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if tt.setup != nil { | ||
tt.setup() | ||
} | ||
if tt.teardown != nil { | ||
defer tt.teardown() | ||
} | ||
tt.testFunc(t) | ||
}) | ||
} | ||
} | ||
|
||
func TestSemaphoreFunctionality(t *testing.T) { | ||
os.Setenv("ARGOCD_GIT_LSREMOTE_PARALLELISM_LIMIT", "1") | ||
|
||
tests := []struct { | ||
name string | ||
setup func() | ||
teardown func() | ||
testFunc func(t *testing.T) | ||
}{ | ||
{ | ||
name: "lsRemoteParallelismLimitSemaphore is not nil", | ||
setup: func() { | ||
lsRemoteParallelismLimitSemaphore = semaphore.NewWeighted(1) | ||
}, | ||
teardown: func() { | ||
lsRemoteParallelismLimitSemaphore = nil | ||
}, | ||
testFunc: func(t *testing.T) { | ||
assert.NotPanics(t, func() { | ||
NewGitClientEventHandlers(&MetricsServer{}) | ||
}) | ||
}, | ||
}, | ||
{ | ||
name: "lsRemoteParallelismLimitSemaphore is not nil and Acquire returns error", | ||
setup: func() { | ||
lsRemoteParallelismLimitSemaphore = semaphore.NewWeighted(1) | ||
}, | ||
teardown: func() { | ||
lsRemoteParallelismLimitSemaphore = nil | ||
}, | ||
testFunc: func(t *testing.T) { | ||
assert.NotPanics(t, func() { | ||
NewGitClientEventHandlers(&MetricsServer{}) | ||
}) | ||
}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if tt.setup != nil { | ||
tt.setup() | ||
} | ||
if tt.teardown != nil { | ||
defer tt.teardown() | ||
} | ||
tt.testFunc(t) | ||
}) | ||
} | ||
} |