Skip to content

Commit

Permalink
Fixed problem with processing of client ACK packet
Browse files Browse the repository at this point in the history
  * Problem in missed ```fallthrough``` in switch between _ACK and _BINARY_ACK
  * Added tests for checking _ACK and _BINARY_ACK client packet processing
  • Loading branch information
CyberLight committed May 10, 2016
1 parent f54ebfd commit a425169
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 0 deletions.
1 change: 1 addition & 0 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ func (h *socketHandler) onPacket(decoder *decoder, packet *packet) ([]interface{
case _ERROR:
message = "error"
case _ACK:
fallthrough
case _BINARY_ACK:
return nil, h.onAck(packet.Id, decoder, packet)
default:
Expand Down
105 changes: 105 additions & 0 deletions handler_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package socketio

import (
"testing"

. "github.com/smartystreets/goconvey/convey"
"net/http"
"io"
"github.com/googollee/go-engine.io"
)

type FakeBroadcastAdaptor struct {}

func (f *FakeBroadcastAdaptor) Join(room string, socket Socket) error {
return nil
}

func (f *FakeBroadcastAdaptor) Leave(room string, socket Socket) error {
return nil
}

func (f *FakeBroadcastAdaptor) Send(ignore Socket, room, event string, args ...interface{}) error {
return nil
}

type FakeReadCloser struct {}

func (fr *FakeReadCloser) Read(p []byte) (n int, err error) {
p = append(p, byte(128))
return 1, nil
}

func (fr *FakeReadCloser) Close() error {
return nil
}

type FakeWriteCloser struct {}

func (fr *FakeWriteCloser) Write(p []byte) (n int, err error) {
return len(p), nil
}

func (fr *FakeWriteCloser) Close() error {
return nil
}

type FakeSockConnection struct {}

func (f *FakeSockConnection) Id() string {
return "test1"
}

func (f *FakeSockConnection) Request() *http.Request {
return &http.Request{}
}

func (f *FakeSockConnection) Close() error {
return nil
}

func (f *FakeSockConnection) NextReader() (engineio.MessageType, io.ReadCloser, error) {
return engineio.MessageText, &FakeReadCloser{}, nil
}

func (f *FakeSockConnection) NextWriter(messageType engineio.MessageType) (io.WriteCloser, error) {
return &FakeWriteCloser{}, nil
}


func TestHandler(t *testing.T) {
//BugFix missed
//Method: handler.onPacket
//Reason: missed fallthrough after case _ACK:
//
// case _ACK:
// fallthrough <---- fixed problem
//
Convey("Call ACK handler by ACK id received from client", t, func() {
saver := &FrameSaver{}
var handlerCalled bool
baseHandlerInstance := newBaseHandler("some:event", &FakeBroadcastAdaptor{})
socketInstance := newSocket(&FakeSockConnection{}, baseHandlerInstance)
c, _ := newCaller(func () {handlerCalled = true})

socketInstance.acks[0] = c
socketInstance.onPacket(newDecoder(saver), &packet{Type:_ACK, Id:0, Data:"[]", NSP:"/"})

So(len(socketInstance.acks), ShouldEqual, 0)
So(handlerCalled, ShouldBeTrue)
})

Convey("Call BINARY ACK handler by BINARY ACK id received from client", t, func() {
saver := &FrameSaver{}
var handlerCalled bool
baseHandlerInstance := newBaseHandler("some:event", &FakeBroadcastAdaptor{})
socketInstance := newSocket(&FakeSockConnection{}, baseHandlerInstance)
c, _ := newCaller(func () {handlerCalled = true})

socketInstance.acks[0] = c
socketInstance.onPacket(newDecoder(saver), &packet{Type:_BINARY_ACK, Id:0, Data:"[]", NSP:"/"})

So(len(socketInstance.acks), ShouldEqual, 0)
So(handlerCalled, ShouldBeTrue)
})
}

0 comments on commit a425169

Please sign in to comment.