Skip to content

Commit

Permalink
adding tests for githandlers (argoproj#16678)
Browse files Browse the repository at this point in the history
Signed-off-by: zhaque44 <[email protected]>
  • Loading branch information
zhaque44 authored and clement-heetch committed Feb 12, 2024
1 parent d336b7a commit bdfbf14
Showing 1 changed file with 122 additions and 0 deletions.
122 changes: 122 additions & 0 deletions reposerver/metrics/githandlers_test.go
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)
})
}
}

0 comments on commit bdfbf14

Please sign in to comment.