add some more error case tests
@@ -2,6 +2,7 @@ package agentd
import (
"encoding/json"
+ "errors"
"testing"
"github.com/sensu/sensu-go/backend/messaging"
@@ -15,6 +16,8 @@ import (
type testTransport struct {
sendCh chan *transport.Message
closed bool
+ sendErr error
+ recvErr error
}
func (t testTransport) Closed() bool {
@@ -27,11 +30,17 @@ func (t testTransport) Close() error {
}
func (t testTransport) Send(msg *transport.Message) error {
+ if t.sendErr != nil {
+ return t.sendErr
+ }
t.sendCh <- msg
return nil
}
func (t testTransport) Receive() (*transport.Message, error) {
+ if t.recvErr != nil {
+ return nil, t.recvErr
+ }
return <-t.sendCh, nil
}
@@ -72,7 +81,6 @@ func TestBadHandshake(t *testing.T) {
bus.Start()
st := &mockstore.MockStore{}
- st.On("UpdateEntity", mock.AnythingOfType("*types.Entity")).Return(nil)
session, err := NewSession(conn, bus, st)
assert.NoError(t, err)
@@ -87,3 +95,69 @@ func TestBadHandshake(t *testing.T) {
})
assert.Error(t, session.Start())
}
+
+func TestNoHandshake(t *testing.T) {
+ conn := testTransport{
+ sendCh: make(chan *transport.Message, 10),
+ }
+
+ bus := &messaging.WizardBus{}
+ bus.Start()
+
+ st := &mockstore.MockStore{}
+
+ session, err := NewSession(conn, bus, st)
+ assert.NoError(t, err)
+ if err != nil {
+ assert.FailNow(t, "unable to create session")
+ }
+ assert.NotNil(t, session)
+
+ conn.Send(&transport.Message{
+ Type: types.EventType,
+ Payload: []byte("..."),
+ })
+ assert.Error(t, session.Start())
+}
+
+func TestSendError(t *testing.T) {
+ conn := testTransport{
+ sendCh: make(chan *transport.Message, 10),
+ }
+
+ bus := &messaging.WizardBus{}
+ bus.Start()
+
+ st := &mockstore.MockStore{}
+
+ session, err := NewSession(conn, bus, st)
+ assert.NoError(t, err)
+ if err != nil {
+ assert.FailNow(t, "unable to create session")
+ }
+ assert.NotNil(t, session)
+
+ conn.sendErr = errors.New("error")
+ assert.Error(t, session.Start())
+}
+
+func TestReceiveError(t *testing.T) {
+ conn := testTransport{
+ sendCh: make(chan *transport.Message, 10),
+ }
+
+ bus := &messaging.WizardBus{}
+ bus.Start()
+
+ st := &mockstore.MockStore{}
+
+ session, err := NewSession(conn, bus, st)
+ assert.NoError(t, err)
+ if err != nil {
+ assert.FailNow(t, "unable to create session")
+ }
+ assert.NotNil(t, session)
+
+ conn.recvErr = errors.New("error")
+ assert.Error(t, session.Start())
+}
