diff --git a/conn.go b/conn.go index 5576992..f5d85c9 100644 --- a/conn.go +++ b/conn.go @@ -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) diff --git a/conn_test.go b/conn_test.go index 0cc1d8c..8197037 100644 --- a/conn_test.go +++ b/conn_test.go @@ -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"))