func createHeartBeatConnection( c *C, readTimeout, writeTimeout int, readTimeoutError time.Duration) (*Conn, *fakeReaderWriter) { fc1, fc2 := testutil.NewFakeConn(c) stop := make(chan struct{}) reader := NewReader(fc2) writer := NewWriter(fc2) go func() { f1, err := reader.Read() c.Assert(err, IsNil) c.Assert(f1.Command, Equals, "CONNECT") c.Assert(f1.Get("heart-beat"), Equals, "1,1") f2 := NewFrame("CONNECTED", "version", "1.2") f2.Add("heart-beat", fmt.Sprintf("%d,%d", readTimeout, writeTimeout)) writer.Write(f2) close(stop) }() conn, err := Connect(fc1, Options{HeartBeat: "1,1", ReadHeartBeatError: 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 := NewReader(fc2) writer := NewWriter(fc2) go func() { f1, err := reader.Read() c.Assert(err, IsNil) c.Assert(f1.Command, Equals, "CONNECT") f2 := NewFrame("CONNECTED", "version", version.String()) writer.Write(f2) close(stop) }() conn, err := Connect(fc1, Options{}) c.Assert(conn, NotNil) c.Assert(err, IsNil) <-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 := NewReader(fc2) writer := NewWriter(fc2) f1, err := reader.Read() c.Assert(err, IsNil) c.Assert(f1.Command, Equals, "CONNECT") c.Assert(f1.Get("login"), Equals, "guest") c.Assert(f1.Get("passcode"), Equals, "guest") c.Assert(f1.Get("host"), Equals, "/") c.Assert(f1.Get("x-max-length"), Equals, "50") connectedFrame := NewFrame("CONNECTED") connectedFrame.Add("session", "session-0voRHrG-VbBedx1Gwwb62Q") connectedFrame.Add("heart-beat", "0,0") connectedFrame.Add("server", "RabbitMQ/3.2.1") connectedFrame.Add("version", "1.0") writer.Write(connectedFrame) f2, err := reader.Read() c.Assert(err, IsNil) c.Assert(f2.Command, Equals, "DISCONNECT") receipt, _ := f2.Contains("receipt") c.Check(receipt, Equals, "1") writer.Write(NewFrame("RECEIPT", frame.ReceiptId, "1")) }() client, err := Connect(fc1, Options{ Login: "******", Passcode: "guest", Host: "/", NonStandard: NewHeader("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 := NewReader(fc2) writer := NewWriter(fc2) f1, err := reader.Read() c.Assert(err, IsNil) c.Assert(f1.Command, Equals, "CONNECT") f2 := NewFrame("ERROR", "message", "auth-failed") writer.Write(f2) }() conn, err := Connect(fc1, Options{}) c.Assert(conn, IsNil) c.Assert(err, ErrorMatches, "auth-failed") }
func (s *StompSuite) Test_successful_connect_and_disconnect(c *C) { testcases := []struct { Options Options NegotiatedVersion string ExpectedVersion Version ExpectedSession string ExpectedHost string ExpectedServer string }{ { Options: Options{}, ExpectedVersion: "1.0", ExpectedSession: "", ExpectedHost: "the-server", ExpectedServer: "some-server/1.1", }, { Options: Options{}, NegotiatedVersion: "1.1", ExpectedVersion: "1.1", ExpectedSession: "the-session", ExpectedHost: "the-server", }, { Options: Options{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 := NewReader(fc2) writer := NewWriter(fc2) f1, err := reader.Read() c.Assert(err, IsNil) c.Assert(f1.Command, Equals, "CONNECT") host, _ := f1.Contains("host") c.Check(host, Equals, tc.ExpectedHost) connectedFrame := NewFrame("CONNECTED") if tc.NegotiatedVersion != "" { connectedFrame.Add("version", tc.NegotiatedVersion) } if tc.ExpectedSession != "" { connectedFrame.Add("session", tc.ExpectedSession) } if tc.ExpectedServer != "" { connectedFrame.Add("server", tc.ExpectedServer) } writer.Write(connectedFrame) f2, err := reader.Read() c.Assert(err, IsNil) c.Assert(f2.Command, Equals, "DISCONNECT") receipt, _ := f2.Contains("receipt") c.Check(receipt, Equals, "1") writer.Write(NewFrame("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 } }