From e2b61c9056a1c71888e57852ec6431ce44cd82cf Mon Sep 17 00:00:00 2001 From: Ken Hibino Date: Tue, 9 Nov 2021 15:53:48 -0800 Subject: [PATCH] Return error if Unique TTL is less than 1s --- client.go | 7 ++++++- client_test.go | 5 +++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/client.go b/client.go index ef82da7..61db5bf 100644 --- a/client.go +++ b/client.go @@ -143,6 +143,7 @@ func (t deadlineOption) Value() interface{} { return time.Time(t) } // Task enqueued with this option is guaranteed to be unique within the given ttl. // Once the task gets processed successfully or once the TTL has expired, another task with the same uniqueness may be enqueued. // ErrDuplicateTask error is returned when enqueueing a duplicate task. +// TTL duration must be greater than or equal to 1 second. // // Uniqueness of a task is based on the following properties: // - Task Type @@ -246,7 +247,11 @@ func composeOptions(opts ...Option) (option, error) { case deadlineOption: res.deadline = time.Time(opt) case uniqueOption: - res.uniqueTTL = time.Duration(opt) + ttl := time.Duration(opt) + if ttl < 1*time.Second { + return option{}, errors.New("Unique TTL cannot be less than 1s") + } + res.uniqueTTL = ttl case processAtOption: res.processAt = time.Time(opt) case processInOption: diff --git a/client_test.go b/client_test.go index e59c5e1..867de91 100644 --- a/client_test.go +++ b/client_test.go @@ -734,6 +734,11 @@ func TestClientEnqueueError(t *testing.T) { task: NewTask("foo", nil), opts: []Option{TaskID(" ")}, }, + { + desc: "With unique option less than 1s", + task: NewTask("foo", nil), + opts: []Option{Unique(300 * time.Millisecond)}, + }, } for _, tc := range tests {