From 1cc502ecaf3c7a6fc64633435799137aeb14035b Mon Sep 17 00:00:00 2001 From: Tim Cooper Date: Tue, 26 May 2020 01:08:43 -0500 Subject: [PATCH] add NewTube function (#45) This allows creating *Tube values in a similar way a *TubeSet is created --- conn.go | 2 +- conn_test.go | 8 ++++---- example_test.go | 2 +- tube.go | 5 +++++ 4 files changed, 11 insertions(+), 6 deletions(-) diff --git a/conn.go b/conn.go index 6eec75d..5576992 100644 --- a/conn.go +++ b/conn.go @@ -40,7 +40,7 @@ var ( func NewConn(conn io.ReadWriteCloser) *Conn { c := new(Conn) c.c = textproto.NewConn(conn) - c.Tube = Tube{c, "default"} + c.Tube = *NewTube(c, "default") c.TubeSet = *NewTubeSet(c, "default") c.used = "default" c.watched = map[string]bool{"default": true} diff --git a/conn_test.go b/conn_test.go index 7b045f4..0cc1d8c 100644 --- a/conn_test.go +++ b/conn_test.go @@ -8,7 +8,7 @@ import ( func TestNameTooLong(t *testing.T) { c := NewConn(mock("", "")) - tube := Tube{c, string(make([]byte, 201))} + tube := NewTube(c, string(make([]byte, 201))) _, err := tube.Put([]byte("foo"), 0, 0, 0) if e, ok := err.(NameError); !ok || e.Err != ErrTooLong { t.Fatal(err) @@ -21,7 +21,7 @@ func TestNameTooLong(t *testing.T) { func TestNameEmpty(t *testing.T) { c := NewConn(mock("", "")) - tube := Tube{c, ""} + tube := NewTube(c, "") _, err := tube.Put([]byte("foo"), 0, 0, 0) if e, ok := err.(NameError); !ok || e.Err != ErrEmpty { t.Fatal(err) @@ -34,7 +34,7 @@ func TestNameEmpty(t *testing.T) { func TestNameBadChar(t *testing.T) { c := NewConn(mock("", "")) - tube := Tube{c, "*"} + tube := NewTube(c, "*") _, err := tube.Put([]byte("foo"), 0, 0, 0) if e, ok := err.(NameError); !ok || e.Err != ErrBadChar { t.Fatal(err) @@ -61,7 +61,7 @@ func TestUse(t *testing.T) { "use foo\r\nput 0 0 0 5\r\nhello\r\n", "USING foo\r\nINSERTED 1\r\n", )) - tube := Tube{c, "foo"} + tube := NewTube(c, "foo") id, err := tube.Put([]byte("hello"), 0, 0, 0) if err != nil { t.Fatal(err) diff --git a/example_test.go b/example_test.go index a8b6124..a47ba16 100644 --- a/example_test.go +++ b/example_test.go @@ -36,7 +36,7 @@ func Example_put() { } func Example_putOtherTube() { - tube := &beanstalk.Tube{Conn: conn, Name: "mytube"} + tube := beanstalk.NewTube(conn, "mytube") id, err := tube.Put([]byte("myjob"), 1, 0, time.Minute) if err != nil { panic(err) diff --git a/tube.go b/tube.go index 0ed43a3..fe7baf7 100644 --- a/tube.go +++ b/tube.go @@ -11,6 +11,11 @@ type Tube struct { Name string } +// NewTube returns a new Tube representing the given name. +func NewTube(c *Conn, name string) *Tube { + return &Tube{c, name} +} + // Put puts a job into tube t with priority pri and TTR ttr, and returns // the id of the newly-created job. If delay is nonzero, the server will // wait the given amount of time after returning to the client and before