-
Notifications
You must be signed in to change notification settings - Fork 2
/
async_test.go
112 lines (102 loc) · 2.08 KB
/
async_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
package json
import (
"encoding/json"
"fmt"
"io"
"sync"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
var (
_ io.Reader = &slowReader{}
errNotSet = fmt.Errorf("marshalErr not set yet")
)
type slowReader struct {
src []byte
index int
len int
mutex sync.Mutex
pause time.Duration
}
func newSlowReader(src []byte, pause time.Duration) *slowReader {
return &slowReader{
src: src,
len: 1,
pause: pause,
mutex: sync.Mutex{},
}
}
func (s *slowReader) Work() {
s.mutex.Lock()
i := s.len
s.mutex.Unlock()
for ; i <= len(s.src); i++ {
time.Sleep(s.pause)
s.mutex.Lock()
s.len = i
s.mutex.Unlock()
}
}
func (s *slowReader) Read(p []byte) (int, error) {
n := len(p)
s.mutex.Lock()
readerLen := s.len
s.mutex.Unlock()
if s.index == len(s.src) {
return 0, io.EOF
}
if s.index+n <= readerLen {
copy(p, s.src[s.index:s.index+n])
s.index += n
return n, nil
}
length := readerLen - s.index
copy(p, s.src[s.index:readerLen])
s.index = readerLen
return length, nil
}
func TestUnmarshal_SlowReader(t *testing.T) {
initBig()
data := newSlowReader(jsonBig, 100*time.Nanosecond)
res := new(map[string]interface{})
expected := new(map[string]interface{})
require.NoError(t, json.Unmarshal(jsonBig, expected))
go data.Work()
require.NoError(t, Unmarshal(data, res))
assert.Equal(t, expected, res)
}
func TestMarshalUnmarshalAsync(t *testing.T) {
want := genValue(100000)
r, w := io.Pipe()
mutex := sync.Mutex{}
err := errNotSet
go func() {
mutex.Lock()
err = Marshal(want, w)
mutex.Unlock()
}()
var got interface{}
require.NoError(t, Unmarshal(r, &got))
mutex.Lock()
require.NoError(t, err)
mutex.Unlock()
assert.Equal(t, want, got)
}
func TestMarshalAsync(t *testing.T) {
r, w := io.Pipe()
mutex := sync.Mutex{}
marshalErr := errNotSet
go func() {
mutex.Lock()
marshalErr = Marshal(allValue, w)
mutex.Unlock()
}()
data, err := io.ReadAll(r)
require.NoError(t, err)
mutex.Lock()
require.NoError(t, marshalErr)
mutex.Unlock()
require.Equal(t, string(data), allValueCompact)
}