From 52e4a01c453b3de42105389beb2374f256751064 Mon Sep 17 00:00:00 2001 From: wuYin Date: Sun, 11 Dec 2022 07:57:34 +0800 Subject: [PATCH] support reserve-job command (#50) --- conn.go | 9 +++++++++ conn_test.go | 15 +++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/conn.go b/conn.go index f5d85c9..7c3eb40 100644 --- a/conn.go +++ b/conn.go @@ -241,6 +241,15 @@ func (c *Conn) Peek(id uint64) (body []byte, err error) { return c.readResp(r, true, "FOUND %d", &id) } +// ReserveJob reserves the specified job by id from the server. +func (c *Conn) ReserveJob(id uint64) (body []byte, err error) { + r, err := c.cmd(nil, nil, nil, "reserve-job", id) + if err != nil { + return nil, err + } + return c.readResp(r, true, "RESERVED %d", &id) +} + // Stats retrieves global statistics from the server. func (c *Conn) Stats() (map[string]string, error) { r, err := c.cmd(nil, nil, nil, "stats") diff --git a/conn_test.go b/conn_test.go index 8197037..6cd5009 100644 --- a/conn_test.go +++ b/conn_test.go @@ -197,6 +197,21 @@ func TestPeekTwice(t *testing.T) { } } +func TestReserveJob(t *testing.T) { + c := NewConn(mock("reserve-job 1\r\n", "RESERVED 1 1\r\nx\r\n")) + + body, err := c.ReserveJob(1) + if err != nil { + t.Fatal(err) + } + if len(body) != 1 || body[0] != 'x' { + t.Fatalf("bad body, expected %#v, got %#v", "x", string(body)) + } + if err = c.Close(); err != nil { + t.Fatal(err) + } +} + func TestRelease(t *testing.T) { c := NewConn(mock("release 1 3 2\r\n", "RELEASED\r\n"))