forked from apache/dubbo-go-hessian2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
string_test.go
264 lines (219 loc) · 7.03 KB
/
string_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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package hessian
import (
"fmt"
"strings"
"sync"
"testing"
)
import (
"github.com/stretchr/testify/assert"
)
func TestEncString(t *testing.T) {
var (
v string
err error
e *Encoder
d *Decoder
res interface{}
)
e = NewEncoder()
v = "hello"
e.Encode(v)
if len(e.Buffer()) == 0 {
t.Fail()
}
d = NewDecoder(e.Buffer())
res, err = d.Decode()
t.Logf("decode(%v) = %v, %v\n", v, res, err)
}
func TestEncShortRune(t *testing.T) {
var (
v string
err error
e *Encoder
d *Decoder
res interface{}
)
e = NewEncoder()
v = "我化尘埃飞扬,追寻赤裸逆翔"
err = e.Encode(v)
assert.Nil(t, err)
if len(e.Buffer()) == 0 {
t.Fail()
}
d = NewDecoder(e.Buffer())
res, err = d.Decode()
assert.Nil(t, err)
if err != nil {
t.Logf("err:%s", err.Error())
}
assert.Equal(t, v, res)
}
func TestEncRune(t *testing.T) {
var (
v string
err error
e *Encoder
d *Decoder
res interface{}
)
e = NewEncoder()
v = "我化尘埃飞扬,追寻赤裸逆翔,奔去七月刑场,时间烧灼滚烫,回忆撕毁臆想,路上行走匆忙,难能可贵世上,散播留香磁场,我欲乘风破浪,踏遍黄沙海洋,与其误会一场,也要不负勇往,我愿你是个谎,从未出现南墙,笑是神的伪装,笑是强忍的伤,我想你就站在,站在大漠边疆,我化尘埃飞扬,追寻赤裸逆翔," +
"奔去七月刑场,时间烧灼滚烫,回忆撕毁臆想,路上行走匆忙,难能可贵世上,散播留香磁场,我欲乘风破浪,踏遍黄沙海洋,与其误会一场,也要不负勇往,我愿你是个谎,从未出现南墙,笑是神的伪装,笑是强忍的伤,我想你就站在,站在大漠边疆."
v = v + v + v + v + v
v = v + v + v + v + v
v = v + v + v + v + v
v = v + v + v + v + v
v = v + v + v + v + v
t.Logf("TestEncRune vlen: %d", len(v))
e.Encode(v)
if len(e.Buffer()) == 0 {
t.Fail()
}
d = NewDecoder(e.Buffer())
res, err = d.Decode()
if err != nil {
t.Errorf("Decode() = %+v", err)
}
assertEqual([]byte(res.(string)), []byte(v), t)
}
func TestEncStringChunk(t *testing.T) {
enc := NewEncoder()
v := strings.Repeat("我", CHUNK_SIZE-1) + "🤣"
assert.Nil(t, enc.Encode(v))
dec := NewDecoder(enc.Buffer())
s, err := dec.Decode()
assert.Nil(t, err)
assert.Equal(t, v, s)
}
func TestString(t *testing.T) {
s0 := ""
s1 := "0"
s32 := "01234567890123456789012345678901"
s1024 := ""
for i := 0; i < 16; i++ {
s1024 += fmt.Sprintf("%02d 456789012345678901234567890123456789012345678901234567890123\n", i)
}
s65560 := ""
for i := 0; i < 1024; i++ {
s65560 += fmt.Sprintf("%03d 56789012345678901234567890123456789012345678901234567890123\n", i)
}
testDecodeFramework(t, "replyString_0", s0)
testDecodeFramework(t, "replyString_1", s1)
testDecodeFramework(t, "replyString_1023", s1024[:1023])
testDecodeFramework(t, "replyString_1024", s1024)
testDecodeFramework(t, "replyString_31", s32[:31])
testDecodeFramework(t, "replyString_32", s32)
testDecodeFramework(t, "replyString_65536", s65560[:65536])
testDecodeFramework(t, "replyString_null", nil)
}
func TestDecodeJsonString(t *testing.T) {
jsonString := `{"params":{"fromAccid":"23495382","msgType":100,"msgId":"148ef1b2-808d-48f2-b268-7a1018a27bdb","attach":"{\"accid\":\"23495382\",\"classRoomFlag\":50685,\"msgId\":\"599645021431398400\",\"msgType\":\"100\",\"nickname\":\"橙子������\"}","roomid":413256699},"url":"https://api.netease.im/nimserver/chatroom/sendMsg.action"}`
testDecodeFramework(t, "customReplyJsonString", jsonString)
}
func TestStringEncode(t *testing.T) {
s0 := ""
s1 := "0"
s32 := "01234567890123456789012345678901"
s1024 := ""
for i := 0; i < 16; i++ {
s1024 += fmt.Sprintf("%02d 456789012345678901234567890123456789012345678901234567890123\n", i)
}
s65560 := ""
for i := 0; i < 1024; i++ {
s65560 += fmt.Sprintf("%03d 56789012345678901234567890123456789012345678901234567890123\n", i)
}
testJavaDecode(t, "argString_0", s0)
testJavaDecode(t, "argString_1", s1)
testJavaDecode(t, "argString_1023", s1024[:1023])
testJavaDecode(t, "argString_1024", s1024)
testJavaDecode(t, "argString_31", s32[:31])
testJavaDecode(t, "argString_32", s32)
testJavaDecode(t, "argString_65536", s65560[:65536])
}
func TestStringPrtEncode(t *testing.T) {
str := "dubbo-go-hessian2"
testSimpleEncode(t, &str)
}
var decodePool = &sync.Pool{
New: func() interface{} {
return NewCheapDecoderWithSkip([]byte{})
},
}
func TestStringWithPool(t *testing.T) {
e := NewEncoder()
e.Encode(testString)
buf := e.buffer
for i := 0; i < 3; i++ {
d := decodePool.Get().(*Decoder)
d.Reset(buf)
v, err := d.Decode()
if err != nil {
t.Errorf("err:%s", err.Error())
}
if v != testString {
t.Errorf("excpect decode %v, actual %v", testString, v)
}
decodePool.Put(d)
}
}
func TestStringEmoji(t *testing.T) {
// see: test_hessian/src/main/java/test/TestString.java
s0 := "emoji🤣"
s0 += ",max" + string(rune(0x10FFFF))
testDecodeFramework(t, "customReplyStringEmoji", s0)
testJavaDecode(t, "customArgString_emoji", s0)
}
func TestStringEmoji2(t *testing.T) {
// see: test_hessian/src/main/java/test/TestString.java
// see https://github.com/apache/dubbo-go-hessian2/issues/252
s0 := "❄️🚫🚫🚫🚫 多次自我介绍、任务、动态和"
testDecodeFramework(t, "customReplyStringEmoji2", s0)
testJavaDecode(t, "customArgString_emoji2", s0)
}
func TestStringComplex(t *testing.T) {
// see: test_hessian/src/main/java/test/TestString.java
s0 := "킐\u0088中国你好!\u0088\u0088\u0088\u0088\u0088\u0088"
testDecodeFramework(t, "customReplyComplexString", s0)
testJavaDecode(t, "customArgComplexString", s0)
}
func BenchmarkDecodeStringAscii(b *testing.B) {
runBenchmarkDecodeString(b, "hello world, hello hessian")
}
func BenchmarkDecodeStringUnicode(b *testing.B) {
runBenchmarkDecodeString(b, "你好, 世界, 你好, hessian")
}
func BenchmarkDecodeStringEmoji(b *testing.B) {
runBenchmarkDecodeString(b, "❄️🚫🚫🚫🚫 多次自我介绍、任务、动态和")
}
func runBenchmarkDecodeString(b *testing.B, s string) {
s = strings.Repeat(s, 4096)
e := NewEncoder()
_ = e.Encode(s)
buf := e.buffer
d := NewDecoder(buf)
for i := 0; i < b.N; i++ {
d.Reset(buf)
_, err := d.Decode()
if err != nil {
b.Logf("err: %s", err)
b.FailNow()
}
}
}