diff --git a/example_test.go b/example_test.go index 4f095d9..9799fad 100644 --- a/example_test.go +++ b/example_test.go @@ -9,26 +9,32 @@ import ( "github.com/go-stomp/stomp/v3/frame" ) -func ExampleConn_Send(c *stomp.Conn) error { +func handle(_ error) {} + +func ExampleConn_Send() { + c, err := stomp.Dial("tcp", "localhost:61613") + if err != nil { + handle(err) + } + // send with receipt and an optional header - err := c.Send( + err = c.Send( "/queue/test-1", // destination "text/plain", // content-type []byte("Message number 1"), // body stomp.SendOpt.Receipt, stomp.SendOpt.Header("expires", "2049-12-31 23:59:59")) if err != nil { - return err + handle(err) } // send with no receipt and no optional headers err = c.Send("/queue/test-2", "application/xml", []byte("hello")) if err != nil { - return err + handle(err) } - return nil } // Creates a new Header. @@ -49,27 +55,6 @@ func ExampleNewHeader() { doSomethingWith(h) } -// Creates a STOMP frame. -func ExampleNewFrame() { - /* - Creates a STOMP frame that looks like the following: - - CONNECT - login:scott - passcode:tiger - host:stompserver - accept-version:1.1,1.2 - - ^@ - */ - f := frame.New("CONNECT", - "login", "scott", - "passcode", "tiger", - "host", "stompserver", - "accept-version", "1.1,1.2") - doSomethingWith(f) -} - func doSomethingWith(f ...interface{}) { } @@ -78,22 +63,22 @@ func doAnotherThingWith(f interface{}, g interface{}) { } -func ExampleConn_Subscribe_1() error { +func ExampleConn_Subscribe() { conn, err := stomp.Dial("tcp", "localhost:61613") if err != nil { - return err + handle(err) } sub, err := conn.Subscribe("/queue/test-2", stomp.AckClient) if err != nil { - return err + handle(err) } // receive 5 messages and then quit for i := 0; i < 5; i++ { msg := <-sub.C if msg.Err != nil { - return msg.Err + handle(msg.Err) } doSomethingWith(msg) @@ -101,55 +86,58 @@ func ExampleConn_Subscribe_1() error { // acknowledge the message err = conn.Ack(msg) if err != nil { - return err + handle(err) } } err = sub.Unsubscribe() if err != nil { - return err + handle(err) } - return conn.Disconnect() + conn.Disconnect() } // Example of creating subscriptions with various options. -func ExampleConn_Subscribe_2(c *stomp.Conn) error { +func ExampleConn_Subscribe_with_options() { + c, err := stomp.Dial("tcp", "localhost:61613") + if err != nil { + handle(err) + } + // Subscribe to queue with automatic acknowledgement sub1, err := c.Subscribe("/queue/test-1", stomp.AckAuto) if err != nil { - return err + handle(err) } // Subscribe to queue with client acknowledgement and a custom header value sub2, err := c.Subscribe("/queue/test-2", stomp.AckClient, stomp.SubscribeOpt.Header("x-custom-header", "some-value")) if err != nil { - return err + handle(err) } doSomethingWith(sub1, sub2) - - return nil } -func ExampleTransaction() error { +func ExampleTransaction() { conn, err := stomp.Dial("tcp", "localhost:61613") if err != nil { - return err + handle(err) } defer conn.Disconnect() sub, err := conn.Subscribe("/queue/test-2", stomp.AckClient) if err != nil { - return err + handle(err) } // receive 5 messages and then quit for i := 0; i < 5; i++ { msg := <-sub.C if msg.Err != nil { - return msg.Err + handle(msg.Err) } tx := conn.Begin() @@ -162,46 +150,44 @@ func ExampleTransaction() error { // acknowledge the message err = tx.Ack(msg) if err != nil { - return err + handle(err) } err = tx.Commit() if err != nil { - return err + handle(err) } } err = sub.Unsubscribe() if err != nil { - return err + handle(err) } - return nil } // Example of connecting to a STOMP server using an existing network connection. -func ExampleConnect() error { +func ExampleConnect() { netConn, err := net.DialTimeout("tcp", "stomp.server.com:61613", 10*time.Second) if err != nil { - return err + handle(err) } stompConn, err := stomp.Connect(netConn) if err != nil { - return err + handle(err) } defer stompConn.Disconnect() doSomethingWith(stompConn) - return nil } // Connect to a STOMP server using default options. -func ExampleDial_1() error { +func ExampleDial() { conn, err := stomp.Dial("tcp", "192.168.1.1:61613") if err != nil { - return err + handle(err) } err = conn.Send( @@ -209,17 +195,17 @@ func ExampleDial_1() error { "text/plain", // content-type []byte("Test message #1")) // body if err != nil { - return err + handle(err) } - return conn.Disconnect() + conn.Disconnect() } // Connect to a STOMP server that requires authentication. In addition, // we are only prepared to use STOMP protocol version 1.1 or 1.2, and // the virtual host is named "dragon". In this example the STOMP // server also accepts a non-standard header called 'nonce'. -func ExampleDial_2() error { +func ExampleDial_with_options() { conn, err := stomp.Dial("tcp", "192.168.1.1:61613", stomp.ConnOpt.Login("scott", "leopard"), stomp.ConnOpt.AcceptVersion(stomp.V11), @@ -227,7 +213,7 @@ func ExampleDial_2() error { stomp.ConnOpt.Host("dragon"), stomp.ConnOpt.Header("nonce", "B256B26D320A")) if err != nil { - return err + handle(err) } err = conn.Send( @@ -235,8 +221,8 @@ func ExampleDial_2() error { "text/plain", // content-type []byte("Test message #1")) // body if err != nil { - return err + handle(err) } - return conn.Disconnect() + conn.Disconnect() }