Skip to content
This repository was archived by the owner on May 6, 2020. It is now read-only.

Commit def8d36

Browse files
committed
tests: Use WriteFrame from proxy api package.
Use the WriteFrame from proxy api package. Remove the local WriteFrame function from the mock package which was introduced when the WriteFrame from the proxy package was not goroutine safe. Signed-off-by: Archana Shinde <[email protected]>
1 parent c33e7f6 commit def8d36

File tree

2 files changed

+1
-51
lines changed

2 files changed

+1
-51
lines changed

Diff for: mock/mock_proxy.go

-50
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,7 @@
1515
package mock
1616

1717
import (
18-
"encoding/binary"
1918
"encoding/json"
20-
"errors"
21-
"fmt"
22-
"io"
2319
"net"
2420
"os"
2521
"sync"
@@ -248,49 +244,3 @@ func (proxy *Proxy) Stop() {
248244
os.Remove(proxy.connectionPath)
249245
proxy.log("Stopped")
250246
}
251-
252-
const (
253-
flagInError = 1 << (4 + iota)
254-
)
255-
256-
const minHeaderLength = 12
257-
258-
// WriteFrame reimplemented here till the one in api package
259-
// is implemented as goroutine-safe
260-
func WriteFrame(w io.Writer, frame *api.Frame) error {
261-
header := &frame.Header
262-
263-
if len(frame.Payload) < header.PayloadLength {
264-
return fmt.Errorf("frame: bad payload length %d",
265-
header.PayloadLength)
266-
}
267-
268-
// Prepare the header.
269-
len := minHeaderLength + header.PayloadLength
270-
buf := make([]byte, len)
271-
binary.BigEndian.PutUint16(buf[0:2], uint16(header.Version))
272-
buf[2] = byte(header.HeaderLength / 4)
273-
flags := byte(0)
274-
if frame.Header.InError {
275-
flags |= flagInError
276-
}
277-
buf[6] = flags | byte(header.Type)&0xf
278-
buf[7] = byte(header.Opcode)
279-
binary.BigEndian.PutUint32(buf[8:8+4], uint32(header.PayloadLength))
280-
281-
// Write payload if needed
282-
if header.PayloadLength > 0 {
283-
copy(buf[minHeaderLength:], frame.Payload[0:header.PayloadLength])
284-
}
285-
286-
n, err := w.Write(buf)
287-
if err != nil {
288-
return err
289-
}
290-
291-
if n != len {
292-
return errors.New("frame: couldn't write frame")
293-
}
294-
295-
return nil
296-
}

Diff for: mock/protocol.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ func (proto *protocol) Serve(conn net.Conn, userData interface{}) error {
160160
resp := proto.handleCommand(ctx, frame)
161161

162162
// Send the response back to the client.
163-
if err = WriteFrame(conn, resp); err != nil {
163+
if err = api.WriteFrame(conn, resp); err != nil {
164164
// Something made us unable to write the response back
165165
// to the client (could be a disconnection, ...).
166166
return err

0 commit comments

Comments
 (0)