-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
nsqd / nsqlookupd protocol IOLoop updates
- nsqd: update IOLoop so FatalClientErr's are returned. current implementation shadows err, returned err was result of client.Reader.ReadSlice. - nsqlookupd: update IOLoop err shadowing in similar manner, rename SendResponse err to sendErr to be closer to nsqd implementation. in this case FatalClientErr's were effectively ignored since they would fail the type assertion. - add nequal to nsqlookupd_test - http.go / nsqd.go: resolve some err shadowing which had no impact.
- Loading branch information
Showing
8 changed files
with
171 additions
and
10 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,45 @@ | ||
package test | ||
|
||
import ( | ||
"net" | ||
"time" | ||
) | ||
|
||
type FakeNetConn struct { | ||
ReadFunc func([]byte) (int, error) | ||
WriteFunc func([]byte) (int, error) | ||
CloseFunc func() error | ||
LocalAddrFunc func() net.Addr | ||
RemoteAddrFunc func() net.Addr | ||
SetDeadlineFunc func(time.Time) error | ||
SetReadDeadlineFunc func(time.Time) error | ||
SetWriteDeadlineFunc func(time.Time) error | ||
} | ||
|
||
func (f FakeNetConn) Read(b []byte) (int, error) { return f.ReadFunc(b) } | ||
func (f FakeNetConn) Write(b []byte) (int, error) { return f.WriteFunc(b) } | ||
func (f FakeNetConn) Close() error { return f.CloseFunc() } | ||
func (f FakeNetConn) LocalAddr() net.Addr { return f.LocalAddrFunc() } | ||
func (f FakeNetConn) RemoteAddr() net.Addr { return f.RemoteAddrFunc() } | ||
func (f FakeNetConn) SetDeadline(t time.Time) error { return f.SetDeadlineFunc(t) } | ||
func (f FakeNetConn) SetReadDeadline(t time.Time) error { return f.SetReadDeadlineFunc(t) } | ||
func (f FakeNetConn) SetWriteDeadline(t time.Time) error { return f.SetWriteDeadlineFunc(t) } | ||
|
||
type fakeNetAddr struct{} | ||
|
||
func (fakeNetAddr) Network() string { return "" } | ||
func (fakeNetAddr) String() string { return "" } | ||
|
||
func NewFakeNetConn() FakeNetConn { | ||
netAddr := fakeNetAddr{} | ||
return FakeNetConn{ | ||
ReadFunc: func(b []byte) (int, error) { return 0, nil }, | ||
WriteFunc: func(b []byte) (int, error) { return len(b), nil }, | ||
CloseFunc: func() error { return nil }, | ||
LocalAddrFunc: func() net.Addr { return netAddr }, | ||
RemoteAddrFunc: func() net.Addr { return netAddr }, | ||
SetDeadlineFunc: func(time.Time) error { return nil }, | ||
SetWriteDeadlineFunc: func(time.Time) error { return nil }, | ||
SetReadDeadlineFunc: func(time.Time) error { return nil }, | ||
} | ||
} |
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
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
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
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
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
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,62 @@ | ||
package nsqlookupd | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
"time" | ||
|
||
"github.com/nsqio/nsq/internal/protocol" | ||
"github.com/nsqio/nsq/internal/test" | ||
) | ||
|
||
func TestIOLoopReturnsClientErrWhenSendFails(t *testing.T) { | ||
fakeConn := test.NewFakeNetConn() | ||
fakeConn.WriteFunc = func(b []byte) (int, error) { | ||
return 0, errors.New("write error") | ||
} | ||
|
||
testIOLoopReturnsClientErr(t, fakeConn) | ||
} | ||
|
||
func TestIOLoopReturnsClientErrWhenSendSucceeds(t *testing.T) { | ||
fakeConn := test.NewFakeNetConn() | ||
fakeConn.WriteFunc = func(b []byte) (int, error) { | ||
return len(b), nil | ||
} | ||
|
||
testIOLoopReturnsClientErr(t, fakeConn) | ||
} | ||
|
||
func testIOLoopReturnsClientErr(t *testing.T, fakeConn test.FakeNetConn) { | ||
fakeConn.ReadFunc = func(b []byte) (int, error) { | ||
return copy(b, []byte("INVALID_COMMAND\n")), nil | ||
} | ||
|
||
opts := NewOptions() | ||
opts.Logger = newTestLogger(t) | ||
opts.Verbose = true | ||
|
||
prot := &LookupProtocolV1{ctx: &Context{nsqlookupd: New(opts)}} | ||
|
||
errChan := make(chan error) | ||
test := func() { | ||
errChan <- prot.IOLoop(fakeConn) | ||
defer prot.ctx.nsqlookupd.Exit() | ||
} | ||
go test() | ||
|
||
var err error | ||
var timeout bool | ||
|
||
select { | ||
case err = <-errChan: | ||
case <-time.After(2 * time.Second): | ||
timeout = true | ||
} | ||
|
||
equal(t, timeout, false) | ||
|
||
nequal(t, err, nil) | ||
equal(t, err.Error(), "E_INVALID invalid command INVALID_COMMAND") | ||
nequal(t, err.(*protocol.FatalClientErr), nil) | ||
} |
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