Skip to content

Commit b9fd706

Browse files
authored
improve Put error response (#15)
1 parent d74672d commit b9fd706

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

dsqueue.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,9 @@ func (q *DSQueue) Put(item []byte) (err error) {
101101
}
102102
defer func() {
103103
if r := recover(); r != nil {
104-
err = fmt.Errorf("failed to enqueue item: %s", r)
104+
err = fmt.Errorf("%s queue closed", q.name)
105105
}
106106
}()
107-
108107
q.enqueue <- item
109108
return
110109
}
@@ -130,6 +129,9 @@ type getResponse struct {
130129
// should not be used concurrently as items will be returned by one or the
131130
// other indeterminately.
132131
func (q *DSQueue) GetN(n int) ([][]byte, error) {
132+
if n == 0 {
133+
return nil, nil
134+
}
133135
rsp := make(chan getResponse)
134136
q.getn <- getRequest{
135137
n: n,
@@ -347,9 +349,6 @@ func (q *DSQueue) worker(ctx context.Context, bufferSize, dedupCacheSize int, id
347349
idle = true
348350
batchTimer.Reset(idleWriteTime)
349351

350-
case <-ctx.Done():
351-
return
352-
353352
case rsp := <-q.clear:
354353
var rmMemCount int
355354
if item != nil {

dsqueue_test.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,11 @@ func TestBasicOperation(t *testing.T) {
9494
if err == nil {
9595
t.Fatal("expected error calling Enqueue after Close")
9696
}
97+
98+
_, err = queue.GetN(5)
99+
if err == nil {
100+
t.Fatal("expected error calling GetN after Close")
101+
}
97102
}
98103

99104
func TestGetN(t *testing.T) {
@@ -141,7 +146,7 @@ func TestMangledData(t *testing.T) {
141146
// put bad data in the queue
142147
qds := namespace.Wrap(ds, datastore.NewKey("/dsq-"+dsqName))
143148
item := "borked"
144-
queueKey := datastore.NewKey(fmt.Sprintf("%s", item))
149+
queueKey := datastore.NewKey(item)
145150
err := qds.Put(context.Background(), queueKey, []byte(item))
146151
if err != nil {
147152
t.Fatal(err)

0 commit comments

Comments
 (0)