Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit fe8b9a2

Browse files
committed
Squashed commit of the following:
commit 4d53a56 Author: xtaci <[email protected]> Date: Sat Dec 28 22:14:17 2019 +0800 add function PeekSize commit 9ca9c83 Author: xtaci <[email protected]> Date: Sat Dec 28 21:53:41 2019 +0800 add Peak
1 parent 32b9d9a commit fe8b9a2

2 files changed

Lines changed: 18 additions & 3 deletions

File tree

session_test.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ func TestPoll(t *testing.T) {
160160
}
161161
}()
162162

163-
buf := make([]byte, 128)
163+
buf := make([]byte, 65536)
164164
events := make([]*Stream, 128)
165165
for {
166166
n, err := session.PollWait(events)
@@ -171,15 +171,20 @@ func TestPoll(t *testing.T) {
171171
for i := 0; i < n; i++ {
172172
stream := events[i]
173173
for {
174-
n, err := stream.TryRead(buf)
174+
size := stream.PeekSize()
175+
nr, err := stream.TryRead(buf)
175176
if err == ErrWouldBlock {
176177
break
177178
}
178179

180+
if size != nr {
181+
t.Fatal("incorrect peak")
182+
}
183+
179184
if err != nil {
180185
t.Fatal(err)
181186
}
182-
received += n
187+
received += nr
183188
if received == len(tx)*N {
184189
session.Close()
185190
return

stream.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,16 @@ func (s *Stream) Read(b []byte) (n int, err error) {
7979
}
8080
}
8181

82+
// PeekSize returns the next size for Read
83+
func (s *Stream) PeekSize() int {
84+
s.bufferLock.Lock()
85+
defer s.bufferLock.Unlock()
86+
if len(s.buffers) > 0 {
87+
return len(s.buffers[0])
88+
}
89+
return 0
90+
}
91+
8292
// TryRead is the nonblocking version of Read
8393
func (s *Stream) TryRead(b []byte) (n int, err error) {
8494
if s.sess.config.Version == 2 {

0 commit comments

Comments
 (0)