-
Notifications
You must be signed in to change notification settings - Fork 0
/
sync-async-adapter_test.go
167 lines (147 loc) · 4.21 KB
/
sync-async-adapter_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
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package gonice
import (
"fmt"
"sync"
"testing"
"time"
)
const TestTimeout = time.Millisecond * 50
const ZeroProcessTime = 0
type TestFunc func(adapter *SyncAsyncAdapter, wg *sync.WaitGroup, thread int, t *testing.T)
type PathType int
const (
Happy PathType = 1
Fail PathType = 2
Timeout PathType = 3
)
func BenchmarkSyncAsync_Do(b *testing.B) {
service := newTestAsyncService(ZeroProcessTime)
adapter := NewSyncAsyncAdapter(service)
defer adapter.Destroy()
request := "request"
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_, err := adapter.Do(request, time.Second)
if err != nil {
b.Fatalf("%s %v", request, err)
}
}
}
func TestSyncAsync_MultiZero(t *testing.T) {
threads := 500
count := 100
initTest(ZeroProcessTime, threads, t, func(adapter *SyncAsyncAdapter, wg *sync.WaitGroup, thread int, t *testing.T) {
test(thread, count, adapter, wg, t, time.Nanosecond, Fail)
})
}
func TestSyncAsync_SingleZero(t *testing.T) {
threads := 1
count := 500000
initTest(ZeroProcessTime, threads, t, func(adapter *SyncAsyncAdapter, wg *sync.WaitGroup, thread int, t *testing.T) {
test(thread, count, adapter, wg, t, time.Nanosecond, Fail)
})
}
func TestSyncAsync_MultiThreadTimeout(t *testing.T) {
threads := 5
count := 20
initTest(time.Second, threads, t, func(adapter *SyncAsyncAdapter, wg *sync.WaitGroup, thread int, t *testing.T) {
test(thread, count, adapter, wg, t, time.Millisecond, Timeout)
})
}
func TestSyncAsync_SingleThreadTimeout(t *testing.T) {
threads := 1
count := 100
initTest(time.Second, threads, t, func(adapter *SyncAsyncAdapter, wg *sync.WaitGroup, thread int, t *testing.T) {
test(thread, count, adapter, wg, t, time.Millisecond, Timeout)
})
}
func TestSyncAsync_MultiThreadHappy(t *testing.T) {
threads := 500
count := 100
initTest(ZeroProcessTime, threads, t, func(adapter *SyncAsyncAdapter, wg *sync.WaitGroup, thread int, t *testing.T) {
test(thread, count, adapter, wg, t, TestTimeout, Happy)
})
}
func TestSyncAsync_SingleThreadHappy(t *testing.T) {
threads := 1
count := 500000
initTest(ZeroProcessTime, threads, t, func(adapter *SyncAsyncAdapter, wg *sync.WaitGroup, thread int, t *testing.T) {
test(thread, count, adapter, wg, t, TestTimeout, Happy)
})
}
func initTest(processTime time.Duration, threads int, t *testing.T, testFunc TestFunc) {
service := newTestAsyncService(processTime)
adapter := NewSyncAsyncAdapter(service)
defer adapter.Destroy()
wg := NewWaitGroup(threads)
for th := 0; th < threads; th++ {
go testFunc(adapter, wg, th, t)
}
wg.Wait()
}
func test(thread int, count int, adapter *SyncAsyncAdapter, wg *sync.WaitGroup, t *testing.T, timeout time.Duration, path PathType) {
defer func() {
recover()
wg.Done()
}()
var prevErr error
for i := 0; i < count; i++ {
request := fmt.Sprintf("request: %d %d", thread, i)
response, err := adapter.Do(request, timeout)
if path == Happy {
if err != nil {
t.Fatalf("%s %v", request, err)
}
if request != response {
t.Fatalf("Not the same in={%s} out={%s} %v", request, response, prevErr)
}
} else if path == Fail {
if err == nil {
if request != response {
t.Fatalf("Not the same in={%s} out={%s} %v", request, response, prevErr)
}
}
} else if path == Timeout {
if err != TimeoutError {
t.Fatal("Must be timeout error")
}
}
prevErr = err
}
}
func newTestAsyncService(processTime time.Duration) *TestAsyncService {
s := &TestAsyncService{
in: make(chan interface{}),
out: make(chan interface{}),
processTime: processTime}
for i := 0; i < 10; i++ {
go s.listen(i)
}
return s
}
type TestAsyncService struct {
in chan interface{}
out chan interface{}
processTime time.Duration
}
func (s *TestAsyncService) listen(th int) {
for value := range s.in {
if s.processTime != 0 {
time.Sleep(s.processTime)
}
s.out <- value
}
}
func (s *TestAsyncService) ExtractIdFromRequest(request interface{}) string {
return request.(string)
}
func (s *TestAsyncService) ExtractIdFromResponse(response interface{}) string {
return response.(string)
}
func (s *TestAsyncService) SendRequest(request interface{}) {
s.in <- request
}
func (s *TestAsyncService) GetResponseCh() chan interface{} {
return s.out
}