Skip to content

Commit

Permalink
provide more helpful error for negative duration (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
wuYin authored Dec 18, 2020
1 parent 0373241 commit 3bed00b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
7 changes: 7 additions & 0 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,13 @@ func (c *Conn) Close() error {
}

func (c *Conn) cmd(t *Tube, ts *TubeSet, body []byte, op string, args ...interface{}) (req, error) {
// negative dur checking
for _, arg := range args {
if d, _ := arg.(dur); d < 0 {
return req{}, fmt.Errorf("duration must be non-negative, got %v", time.Duration(d))
}
}

r := req{c.c.Next(), op}
c.c.StartRequest(r.id)
defer c.c.EndRequest(r.id)
Expand Down
10 changes: 10 additions & 0 deletions conn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,16 @@ func TestNameBadChar(t *testing.T) {
}
}

func TestNegativeDuration(t *testing.T) {
c := NewConn(mock("", ""))
tube := NewTube(c, "foo")
for _, d := range []time.Duration{-100 * time.Millisecond, -2 * time.Second} {
if _, err := tube.Put([]byte("hello"), 0, d, d); err == nil {
t.Fatalf("put job with negative duration %v expected error, got nil", d)
}
}
}

func TestDeleteMissing(t *testing.T) {
c := NewConn(mock("delete 1\r\n", "NOT_FOUND\r\n"))

Expand Down

0 comments on commit 3bed00b

Please sign in to comment.