File tree 2 files changed +26
-14
lines changed
2 files changed +26
-14
lines changed Original file line number Diff line number Diff line change @@ -9,7 +9,6 @@ package metrics
9
9
import (
10
10
"fmt"
11
11
"sync"
12
- "sync/atomic"
13
12
"time"
14
13
15
14
"github.com/spf13/viper"
@@ -31,7 +30,8 @@ const (
31
30
32
31
var RootScope Scope
33
32
var once sync.Once
34
- var started uint32
33
+ var rootScopeMutex = & sync.Mutex {}
34
+ var running bool
35
35
36
36
// NewOpts create metrics options based config file
37
37
func NewOpts () Opts {
@@ -84,21 +84,33 @@ func Init(opts Opts) (err error) {
84
84
85
85
//Start starts metrics server
86
86
func Start () error {
87
- if atomic .CompareAndSwapUint32 (& started , 0 , 1 ) {
88
- return RootScope .Start ()
87
+ rootScopeMutex .Lock ()
88
+ defer rootScopeMutex .Unlock ()
89
+ if running {
90
+ return nil
89
91
}
90
- return nil
92
+ running = true
93
+ return RootScope .Start ()
91
94
}
92
95
93
96
//Shutdown closes underlying resources used by metrics server
94
97
func Shutdown () error {
95
- if atomic . CompareAndSwapUint32 ( & started , 1 , 0 ) {
96
- err := RootScope . Close ()
97
- RootScope = nil
98
- return err
98
+ rootScopeMutex . Lock ()
99
+ defer rootScopeMutex . Unlock ()
100
+ if ! running {
101
+ return nil
99
102
}
100
103
101
- return nil
104
+ err := RootScope .Close ()
105
+ RootScope = nil
106
+ running = false
107
+ return err
108
+ }
109
+
110
+ func isRunning () bool {
111
+ rootScopeMutex .Lock ()
112
+ defer rootScopeMutex .Unlock ()
113
+ return running
102
114
}
103
115
104
116
type StatsdReporterOpts struct {
Original file line number Diff line number Diff line change @@ -9,11 +9,11 @@ package metrics
9
9
import (
10
10
"fmt"
11
11
"strings"
12
- "sync/atomic"
13
12
"testing"
14
13
"time"
15
14
16
15
"github.com/hyperledger/fabric/core/config/configtest"
16
+ . "github.com/onsi/gomega"
17
17
"github.com/spf13/viper"
18
18
"github.com/stretchr/testify/assert"
19
19
)
@@ -155,6 +155,7 @@ func TestStartInvalidReporter(t *testing.T) {
155
155
156
156
func TestStartAndClose (t * testing.T ) {
157
157
t .Parallel ()
158
+ gt := NewGomegaWithT (t )
158
159
defer Shutdown ()
159
160
opts := Opts {
160
161
Enabled : true ,
@@ -166,10 +167,9 @@ func TestStartAndClose(t *testing.T) {
166
167
FlushBytes : 512 ,
167
168
}}
168
169
Init (opts )
169
- go Start ()
170
- time .Sleep (1 * time .Second )
171
170
assert .NotNil (t , RootScope )
172
- assert .Equal (t , uint32 (1 ), atomic .LoadUint32 (& started ))
171
+ go Start ()
172
+ gt .Eventually (isRunning ).Should (BeTrue ())
173
173
}
174
174
175
175
func TestNoOpScopeMetrics (t * testing.T ) {
You can’t perform that action at this time.
0 commit comments