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 <16801068+Rory-Z@users.noreply.github.com>
  • Loading branch information
Rory-Z committed Nov 7, 2023
1 parent 2efc074 commit d4e31fc
Show file tree
Hide file tree
Showing 9 changed files with 114 additions and 206 deletions.
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 d4e31fc

Please sign in to comment.