Skip to content

Commit

Permalink
test: refactor test suite
Browse files Browse the repository at this point in the history
Signed-off-by: Rory Z <[email protected]>
  • Loading branch information
Rory-Z committed Nov 7, 2023
1 parent 2800fa1 commit 2c7ecb3
Show file tree
Hide file tree
Showing 10 changed files with 117 additions and 204 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,8 @@ jobs:
with:
go-version: '1.20.x'
- run: make test
- uses: codecov/codecov-action@v3
with:
files: ./cover.out
fail_ci_if_error: true # optional (default = false)
verbose: true # optional (default = false)
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ bin
__pycache__
env/

cover.*
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ build:
@cp $(PROJECT_DIR)/config/example/config.yaml $(LOCALBIN)/config.yaml

.PHONY: test
test: build
test:
go test -v -race --cover -covermode=atomic -coverpkg=./... -coverprofile=cover.out ./...
go tool cover -html=cover.out -o=cover.html

.PHONY: docker
docker:
Expand Down
14 changes: 12 additions & 2 deletions client/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ func NewCluster(metrics *config.Metrics, logger log.Logger) collector.Cluster {
}

func (c *cluster) GetLicense() (lic *collector.LicenseInfo, err error) {
c.nodeLock.Lock()
defer c.nodeLock.Unlock()
client := c.getNode()
if client == nil {
return
Expand All @@ -79,6 +81,8 @@ func (c *cluster) GetLicense() (lic *collector.LicenseInfo, err error) {
}

func (c *cluster) GetClusterStatus() (cluster collector.ClusterStatus, err error) {
c.nodeLock.Lock()
defer c.nodeLock.Unlock()
client := c.getNode()
if client == nil {
return
Expand All @@ -92,6 +96,8 @@ func (c *cluster) GetClusterStatus() (cluster collector.ClusterStatus, err error
}

func (c *cluster) GetBrokerMetrics() (brokers *collector.Broker, err error) {
c.nodeLock.Lock()
defer c.nodeLock.Unlock()
client := c.getNode()
if client == nil {
return
Expand All @@ -105,6 +111,8 @@ func (c *cluster) GetBrokerMetrics() (brokers *collector.Broker, err error) {
}

func (c *cluster) GetRuleEngineMetrics() (bridges []collector.DataBridge, res []collector.RuleEngine, err error) {
c.nodeLock.Lock()
defer c.nodeLock.Unlock()
client := c.getNode()
if client == nil {
return
Expand All @@ -123,6 +131,8 @@ func (c *cluster) GetRuleEngineMetrics() (bridges []collector.DataBridge, res []
}

func (c *cluster) GetAuthenticationMetrics() (dataSources []collector.DataSource, auths []collector.Authentication, err error) {
c.nodeLock.Lock()
defer c.nodeLock.Unlock()
client := c.getNode()
if client == nil {
return
Expand All @@ -136,6 +146,8 @@ func (c *cluster) GetAuthenticationMetrics() (dataSources []collector.DataSource
}

func (c *cluster) GetAuthorizationMetrics() (dataSources []collector.DataSource, auths []collector.Authorization, err error) {
c.nodeLock.Lock()
defer c.nodeLock.Unlock()
client := c.getNode()
if client == nil {
return
Expand All @@ -149,8 +161,6 @@ func (c *cluster) GetAuthorizationMetrics() (dataSources []collector.DataSource,
}

func (c *cluster) getNode() client {
c.nodeLock.RLock()
client := c.client
c.nodeLock.RUnlock()
return client
}
6 changes: 3 additions & 3 deletions collector/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,14 @@ func (n EMQXCollector) Describe(ch chan<- *prometheus.Desc) {
// Collect implements the prometheus.Collector interface.
func (n EMQXCollector) Collect(ch chan<- prometheus.Metric) {
wg := sync.WaitGroup{}
wg.Add(len(n.Collectors))
defer wg.Wait()
for name, c := range n.Collectors {
wg.Add(1)
go func(name string, c Collector) {
defer wg.Done()
execute(name, c, ch, n.logger)
wg.Done()
}(name, c)
}
wg.Wait()
}

func execute(name string, c Collector, ch chan<- prometheus.Metric, logger log.Logger) {
Expand Down
11 changes: 0 additions & 11 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,10 @@ require (

require (
github.com/Microsoft/go-winio v0.6.1 // indirect
github.com/Microsoft/hcsshim v0.11.1 // indirect
github.com/alecthomas/units v0.0.0-20211218093645-b94a6e3cc137 // indirect
github.com/andybalholm/brotli v1.0.5 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/containerd/cgroups v1.1.0 // indirect
github.com/containerd/containerd v1.7.8 // indirect
github.com/containerd/continuity v0.4.2 // indirect
github.com/containerd/log v0.1.0 // indirect
github.com/coreos/go-systemd/v22 v22.5.0 // indirect
github.com/distribution/reference v0.5.0 // indirect
github.com/docker/distribution v2.8.3+incompatible // indirect
Expand All @@ -38,7 +33,6 @@ require (
github.com/go-logr/logr v1.2.4 // indirect
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38 // indirect
Expand All @@ -47,7 +41,6 @@ require (
github.com/klauspost/compress v1.16.4 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0 // indirect
github.com/moby/sys/mountinfo v0.6.2 // indirect
github.com/moby/term v0.5.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
Expand All @@ -57,11 +50,9 @@ require (
github.com/opencontainers/image-spec v1.1.0-rc2.0.20221005185240-3a7f492d3f1b // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/prometheus/procfs v0.11.1 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/stretchr/testify v1.8.4 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/xhit/go-str2duration/v2 v2.1.0 // indirect
go.opencensus.io v0.24.0 // indirect
golang.org/x/crypto v0.14.0 // indirect
golang.org/x/mod v0.12.0 // indirect
golang.org/x/net v0.17.0 // indirect
Expand All @@ -72,8 +63,6 @@ require (
golang.org/x/time v0.3.0 // indirect
golang.org/x/tools v0.12.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230711160842-782d3b101e98 // indirect
google.golang.org/grpc v1.58.3 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gotest.tools/v3 v3.5.1 // indirect
Expand Down
Loading

0 comments on commit 2c7ecb3

Please sign in to comment.