-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconcurrent_test.go
More file actions
107 lines (96 loc) · 2.12 KB
/
Copy pathconcurrent_test.go
File metadata and controls
107 lines (96 loc) · 2.12 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package xerror
import (
"sync"
"testing"
"github.com/gomooth/xerror/xcode"
)
func TestConcurrent_Register(t *testing.T) {
var wg sync.WaitGroup
for i := 0; i < 100; i++ {
wg.Add(1)
go func(i int) {
defer wg.Done()
xcode.Register(xcode.NewWithHTTPStatus(20000+i, 400, "业务错误"))
}(i)
}
wg.Wait()
for i := 0; i < 100; i++ {
code := xcode.NewFromRegistry(20000+i, "test")
if code.HttpStatus() != 400 {
t.Fatalf("code %d: expected httpStatus 400, got %d", 20000+i, code.HttpStatus())
}
}
xcode.Reset()
}
func TestConcurrent_Override(t *testing.T) {
var wg sync.WaitGroup
for i := 0; i < 100; i++ {
wg.Add(1)
go func(i int) {
defer wg.Done()
xcode.Override(500, 400+i%10, "覆写500")
}(i)
}
wg.Wait()
code := xcode.NewFromRegistry(500, "test")
if code.HttpStatus() < 400 || code.HttpStatus() > 409 {
t.Fatalf("unexpected httpStatus %d", code.HttpStatus())
}
xcode.Reset()
}
func TestConcurrent_Reset(t *testing.T) {
var wg sync.WaitGroup
for i := 0; i < 50; i++ {
wg.Add(2)
go func(i int) {
defer wg.Done()
xcode.Register(xcode.NewWithHTTPStatus(30000+i, 400, "注册"))
}(i)
go func() {
defer wg.Done()
xcode.Reset()
}()
}
wg.Wait()
xcode.Reset()
}
func TestConcurrent_Wrap(t *testing.T) {
err := New("base error")
var wg sync.WaitGroup
results := make([]XError, 100)
for i := 0; i < 100; i++ {
wg.Add(1)
go func(i int) {
defer wg.Done()
results[i] = Wrap(err, "wrapped")
}(i)
}
wg.Wait()
for i, r := range results {
if r.Message() != "wrapped" {
t.Fatalf("result[%d]: expected 'wrapped', got %q", i, r.Message())
}
}
}
func TestConcurrent_WithFields(t *testing.T) {
err := New("base error")
var wg sync.WaitGroup
results := make([]XError, 100)
for i := 0; i < 100; i++ {
wg.Add(1)
go func(i int) {
defer wg.Done()
results[i] = err.WithFields(F("key", i))
}(i)
}
wg.Wait()
for i, r := range results {
fields := r.GetFields()
if len(fields) != 1 {
t.Fatalf("result[%d]: expected 1 field, got %d", i, len(fields))
}
if fields[0].Key != "key" {
t.Fatalf("result[%d]: expected key 'key', got %q", i, fields[0].Key)
}
}
}