Commit eb7ea591 authored by Zahoor Mohamed's avatar Zahoor Mohamed Committed by GitHub

return empty response when nothing is pinned (#500)

* return empty response when nothing is pinned
parent 6efcbfdf
...@@ -47,6 +47,13 @@ func TestPinChunkHandler(t *testing.T) { ...@@ -47,6 +47,13 @@ func TestPinChunkHandler(t *testing.T) {
}) })
}) })
// list pins without anything pinned
t.Run("list-pins-zero-pins", func(t *testing.T) {
jsonhttptest.ResponseDirect(t, debugTestServer.Client, http.MethodGet, "/chunks-pin", nil, http.StatusOK, debugapi.ListPinnedChunksResponse{
Chunks: []debugapi.PinnedChunk{},
})
})
// pin a chunk which is not existing // pin a chunk which is not existing
t.Run("pin-absent-chunk", func(t *testing.T) { t.Run("pin-absent-chunk", func(t *testing.T) {
jsonhttptest.ResponseDirect(t, debugTestServer.Client, http.MethodPost, "/chunks-pin/123456", nil, http.StatusNotFound, jsonhttp.StatusResponse{ jsonhttptest.ResponseDirect(t, debugTestServer.Client, http.MethodPost, "/chunks-pin/123456", nil, http.StatusNotFound, jsonhttp.StatusResponse{
......
...@@ -30,6 +30,16 @@ func (db *DB) PinnedChunks(ctx context.Context, cursor swarm.Address) (pinnedChu ...@@ -30,6 +30,16 @@ func (db *DB) PinnedChunks(ctx context.Context, cursor swarm.Address) (pinnedChu
prefix = nil prefix = nil
} }
c, err := db.pinIndex.Count()
if err != nil {
return nil, fmt.Errorf("list pinned chunks: %w", err)
}
// send empty response if there is nothing pinned
if c == 0 {
return pinnedChunks, nil
}
it, err := db.pinIndex.First(prefix) it, err := db.pinIndex.First(prefix)
if err != nil { if err != nil {
return nil, fmt.Errorf("get first pin: %w", err) return nil, fmt.Errorf("get first pin: %w", err)
......
...@@ -263,6 +263,9 @@ func (m *MockStorer) SubscribePush(ctx context.Context) (c <-chan swarm.Chunk, s ...@@ -263,6 +263,9 @@ func (m *MockStorer) SubscribePush(ctx context.Context) (c <-chan swarm.Chunk, s
func (m *MockStorer) PinnedChunks(ctx context.Context, cursor swarm.Address) (pinnedChunks []*storage.Pinner, err error) { func (m *MockStorer) PinnedChunks(ctx context.Context, cursor swarm.Address) (pinnedChunks []*storage.Pinner, err error) {
m.pinSetMu.Lock() m.pinSetMu.Lock()
defer m.pinSetMu.Unlock() defer m.pinSetMu.Unlock()
if len(m.pinnedAddress) == 0 {
return pinnedChunks, nil
}
for i, addr := range m.pinnedAddress { for i, addr := range m.pinnedAddress {
pi := &storage.Pinner{ pi := &storage.Pinner{
Address: swarm.NewAddress(addr.Bytes()), Address: swarm.NewAddress(addr.Bytes()),
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment