Skip to content

Commit

Permalink
add NewTube function (#45)
Browse files Browse the repository at this point in the history
This allows creating *Tube values in a similar way a *TubeSet is created
  • Loading branch information
Tim Cooper authored May 26, 2020
1 parent b83b682 commit 1cc502e
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 6 deletions.
2 changes: 1 addition & 1 deletion conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
8 changes: 4 additions & 4 deletions conn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions tube.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 1cc502e

Please sign in to comment.