generated from cloudwego/.github
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(thrift): add tests for WriteDirect
- Loading branch information
Showing
3 changed files
with
127 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package netpoll | ||
|
||
// NetpollDirectWriter implements NocopyWriter for fastcodec. | ||
// | ||
// It's only used for testing purposes, | ||
// see the WriteDirect implementation of netpoll.Writer for details. | ||
type NetpollDirectWriter struct { | ||
data []byte | ||
|
||
// for WriteDirect | ||
wbuf [][]byte | ||
wend []int // remainCap | ||
} | ||
|
||
// Bytes returns the actual result of FastWriteNocopy | ||
func (p *NetpollDirectWriter) Bytes() []byte { | ||
ret := make([]byte, 0, len(p.data)) | ||
start := 0 | ||
for i := 0; i < len(p.wend); i++ { | ||
end := len(p.data) - p.wend[i] | ||
ret = append(ret, p.data[start:end]...) // bytes from p.data | ||
ret = append(ret, p.wbuf[i]...) // bytes from WriteDirect | ||
start = end | ||
} | ||
// copy left bytes | ||
ret = append(ret, p.data[start:start+len(p.data)-len(ret)]...) | ||
if len(ret) != len(p.data) { | ||
panic("size not match") | ||
} | ||
return ret | ||
} | ||
|
||
// Malloc creates a new buffer for FastWriteNocopy | ||
func (p *NetpollDirectWriter) Malloc(n int) []byte { | ||
p.wbuf = p.wbuf[:0] | ||
p.wend = p.wend[:0] | ||
p.data = make([]byte, n) | ||
return p.data | ||
} | ||
|
||
// WriteDirect implements NocopyWriter for fastcodec | ||
func (p *NetpollDirectWriter) WriteDirect(b []byte, remainCap int) error { | ||
if remainCap < len(b) { | ||
panic("buffer too small to fit the input") | ||
} | ||
p.wbuf = append(p.wbuf, b) | ||
p.wend = append(p.wend, remainCap) | ||
return nil | ||
} | ||
|
||
func (p *NetpollDirectWriter) WriteDirectN() int { | ||
return len(p.wbuf) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package netpoll | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestNetpollDirectWriter(t *testing.T) { | ||
w := &NetpollDirectWriter{} | ||
|
||
i := 0 | ||
b := w.Malloc(100) | ||
vv := [][]byte{ // 10 x 10 | ||
[]byte("1234567890"), | ||
[]byte("2234567890"), | ||
[]byte("3234567890"), | ||
[]byte("4234567890"), | ||
[]byte("5234567890"), | ||
[]byte("6234567890"), | ||
[]byte("7234567890"), | ||
[]byte("8234567890"), | ||
[]byte("9234567890"), | ||
[]byte("0234567890"), | ||
} | ||
|
||
for j, v := range vv { | ||
if j != 0 && j%2 == 0 { | ||
_ = w.WriteDirect(v, len(b)-i) | ||
} else { | ||
i += copy(b[i:], v) | ||
} | ||
} | ||
expect := bytes.Join(vv, []byte{}) | ||
require.Equal(t, expect, w.Bytes()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters