func createHeartBeatConnection( c *C, readTimeout, writeTimeout int, readTimeoutError time.Duration) (*Conn, *fakeReaderWriter) { fc1, fc2 := testutil.NewFakeConn(c) stop := make(chan struct{}) reader := frame.NewReader(fc2) writer := frame.NewWriter(fc2) go func() { f1, err := reader.Read() c.Assert(err, IsNil) c.Assert(f1.Command, Equals, "CONNECT") c.Assert(f1.Header.Get("heart-beat"), Equals, "1,1") f2 := frame.New("CONNECTED", "version", "1.2") f2.Header.Add("heart-beat", fmt.Sprintf("%d,%d", readTimeout, writeTimeout)) writer.Write(f2) close(stop) }() conn, err := Connect(fc1, ConnOpt.HeartBeat(time.Millisecond, time.Millisecond), ConnOpt.HeartBeatError(readTimeoutError)) c.Assert(conn, NotNil) c.Assert(err, IsNil) <-stop return conn, &fakeReaderWriter{ reader: reader, writer: writer, conn: fc2, } }
// Sets up a connection for testing func connectHelper(c *C, version Version) (*Conn, *fakeReaderWriter) { fc1, fc2 := testutil.NewFakeConn(c) stop := make(chan struct{}) reader := frame.NewReader(fc2) writer := frame.NewWriter(fc2) go func() { f1, err := reader.Read() c.Assert(err, IsNil) c.Assert(f1.Command, Equals, "CONNECT") f2 := frame.New("CONNECTED", "version", version.String()) writer.Write(f2) close(stop) }() conn, err := Connect(fc1) c.Assert(err, IsNil) c.Assert(conn, NotNil) <-stop return conn, &fakeReaderWriter{ reader: reader, writer: writer, conn: fc2, } }
func (s *StompSuite) Test_successful_connect_with_nonstandard_header(c *C) { resetId() fc1, fc2 := testutil.NewFakeConn(c) stop := make(chan struct{}) go func() { defer func() { fc2.Close() close(stop) }() reader := frame.NewReader(fc2) writer := frame.NewWriter(fc2) f1, err := reader.Read() c.Assert(err, IsNil) c.Assert(f1.Command, Equals, "CONNECT") c.Assert(f1.Header.Get("login"), Equals, "guest") c.Assert(f1.Header.Get("passcode"), Equals, "guest") c.Assert(f1.Header.Get("host"), Equals, "/") c.Assert(f1.Header.Get("x-max-length"), Equals, "50") connectedFrame := frame.New("CONNECTED") connectedFrame.Header.Add("session", "session-0voRHrG-VbBedx1Gwwb62Q") connectedFrame.Header.Add("heart-beat", "0,0") connectedFrame.Header.Add("server", "RabbitMQ/3.2.1") connectedFrame.Header.Add("version", "1.0") writer.Write(connectedFrame) f2, err := reader.Read() c.Assert(err, IsNil) c.Assert(f2.Command, Equals, "DISCONNECT") receipt, _ := f2.Header.Contains("receipt") c.Check(receipt, Equals, "1") writer.Write(frame.New("RECEIPT", frame.ReceiptId, "1")) }() client, err := Connect(fc1, ConnOpt.Login("guest", "guest"), ConnOpt.Host("/"), ConnOpt.Header("x-max-length", "50")) c.Assert(err, IsNil) c.Assert(client, NotNil) c.Assert(client.Version(), Equals, V10) c.Assert(client.Session(), Equals, "session-0voRHrG-VbBedx1Gwwb62Q") c.Assert(client.Server(), Equals, "RabbitMQ/3.2.1") err = client.Disconnect() c.Assert(err, IsNil) <-stop }
func (s *StompSuite) Test_unsuccessful_connect(c *C) { fc1, fc2 := testutil.NewFakeConn(c) stop := make(chan struct{}) go func() { defer func() { fc2.Close() close(stop) }() reader := frame.NewReader(fc2) writer := frame.NewWriter(fc2) f1, err := reader.Read() c.Assert(err, IsNil) c.Assert(f1.Command, Equals, "CONNECT") f2 := frame.New("ERROR", "message", "auth-failed") writer.Write(f2) }() conn, err := Connect(fc1) c.Assert(conn, IsNil) c.Assert(err, ErrorMatches, "auth-failed") }
func (s *StompSuite) Test_successful_connect_and_disconnect(c *C) { testcases := []struct { Options []func(*Conn) error NegotiatedVersion string ExpectedVersion Version ExpectedSession string ExpectedHost string ExpectedServer string }{ { Options: []func(*Conn) error{ConnOpt.Host("the-server")}, ExpectedVersion: "1.0", ExpectedSession: "", ExpectedHost: "the-server", ExpectedServer: "some-server/1.1", }, { Options: []func(*Conn) error{}, NegotiatedVersion: "1.1", ExpectedVersion: "1.1", ExpectedSession: "the-session", ExpectedHost: "the-server", }, { Options: []func(*Conn) error{ConnOpt.Host("xxx")}, NegotiatedVersion: "1.2", ExpectedVersion: "1.2", ExpectedSession: "the-session", ExpectedHost: "xxx", }, } for _, tc := range testcases { resetId() fc1, fc2 := testutil.NewFakeConn(c) stop := make(chan struct{}) go func() { defer func() { fc2.Close() close(stop) }() reader := frame.NewReader(fc2) writer := frame.NewWriter(fc2) f1, err := reader.Read() c.Assert(err, IsNil) c.Assert(f1.Command, Equals, "CONNECT") host, _ := f1.Header.Contains("host") c.Check(host, Equals, tc.ExpectedHost) connectedFrame := frame.New("CONNECTED") if tc.NegotiatedVersion != "" { connectedFrame.Header.Add("version", tc.NegotiatedVersion) } if tc.ExpectedSession != "" { connectedFrame.Header.Add("session", tc.ExpectedSession) } if tc.ExpectedServer != "" { connectedFrame.Header.Add("server", tc.ExpectedServer) } writer.Write(connectedFrame) f2, err := reader.Read() c.Assert(err, IsNil) c.Assert(f2.Command, Equals, "DISCONNECT") receipt, _ := f2.Header.Contains("receipt") c.Check(receipt, Equals, "1") writer.Write(frame.New("RECEIPT", frame.ReceiptId, "1")) }() client, err := Connect(fc1, tc.Options...) c.Assert(err, IsNil) c.Assert(client, NotNil) c.Assert(client.Version(), Equals, tc.ExpectedVersion) c.Assert(client.Session(), Equals, tc.ExpectedSession) c.Assert(client.Server(), Equals, tc.ExpectedServer) err = client.Disconnect() c.Assert(err, IsNil) <-stop } }