-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcounter_test.go
55 lines (49 loc) · 1.19 KB
/
counter_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package metric
import (
"fmt"
"sync"
"testing"
)
func TestCounterAdd(t *testing.T) {
counter := NewCounter()
counter.Inc()
if expected, got := 1.0, counter.Value(); expected != got {
t.Errorf("Expected %f, got %f.", expected, got)
}
counter.Add(42)
if expected, got := 43.0, counter.Value(); expected != got {
t.Errorf("Expected %f, got %f.", expected, got)
}
counter.Add(24.42)
if expected, got := 67.42, counter.Value(); expected != got {
t.Errorf("Expected %f, got %f.", expected, got)
}
if expected, got := "counter cannot decrease in value", decreaseCounter(counter).Error(); expected != got {
t.Errorf("Expected error %q, got %q.", expected, got)
}
}
func TestCounterAddConcurrently(t *testing.T) {
const concurrency = 1000
counter := NewCounter()
var wg sync.WaitGroup
wg.Add(concurrency)
for i := 0; i < concurrency; i++ {
go func() {
counter.Inc()
wg.Done()
}()
}
wg.Wait()
if expected, got := float64(concurrency), counter.Value(); expected != got {
t.Errorf("Expected %f, got %f.", expected, got)
}
}
func decreaseCounter(c Counter) (err error) {
defer func() {
if e := recover(); e != nil {
err = fmt.Errorf("%v", e)
}
}()
c.Add(-1)
return nil
}