2021-04-18 15:41:13 -04:00
|
|
|
// Copyright (c) 2015-2021 MinIO, Inc.
|
|
|
|
//
|
|
|
|
// This file is part of MinIO Object Storage stack
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2019-02-25 21:01:13 -05:00
|
|
|
|
2023-04-04 13:52:24 -04:00
|
|
|
package store
|
2019-02-25 21:01:13 -05:00
|
|
|
|
|
|
|
import (
|
2024-11-06 19:32:39 -05:00
|
|
|
"bytes"
|
2024-09-06 19:06:30 -04:00
|
|
|
"fmt"
|
2019-02-25 21:01:13 -05:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
2024-11-06 19:32:39 -05:00
|
|
|
|
|
|
|
jsoniter "github.com/json-iterator/go"
|
|
|
|
"github.com/valyala/bytebufferpool"
|
2019-02-25 21:01:13 -05:00
|
|
|
)
|
|
|
|
|
2023-04-04 13:52:24 -04:00
|
|
|
type TestItem struct {
|
|
|
|
Name string `json:"Name"`
|
|
|
|
Property string `json:"property"`
|
|
|
|
}
|
2019-02-25 21:01:13 -05:00
|
|
|
|
2023-04-04 13:52:24 -04:00
|
|
|
var (
|
|
|
|
// TestDir
|
|
|
|
queueDir = filepath.Join(os.TempDir(), "minio_test")
|
|
|
|
// Sample test item.
|
|
|
|
testItem = TestItem{Name: "test-item", Property: "property"}
|
|
|
|
// Ext for test item
|
|
|
|
testItemExt = ".test"
|
|
|
|
)
|
2019-02-25 21:01:13 -05:00
|
|
|
|
2023-04-04 13:52:24 -04:00
|
|
|
// Initialize the queue store.
|
|
|
|
func setUpQueueStore(directory string, limit uint64) (Store[TestItem], error) {
|
|
|
|
queueStore := NewQueueStore[TestItem](queueDir, limit, testItemExt)
|
|
|
|
if oErr := queueStore.Open(); oErr != nil {
|
2019-02-25 21:01:13 -05:00
|
|
|
return nil, oErr
|
|
|
|
}
|
2023-04-04 13:52:24 -04:00
|
|
|
return queueStore, nil
|
2019-02-25 21:01:13 -05:00
|
|
|
}
|
|
|
|
|
2023-04-04 13:52:24 -04:00
|
|
|
// Tear down queue store.
|
|
|
|
func tearDownQueueStore() error {
|
2019-06-27 05:48:33 -04:00
|
|
|
return os.RemoveAll(queueDir)
|
2019-02-25 21:01:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// TestQueueStorePut - tests for store.Put
|
|
|
|
func TestQueueStorePut(t *testing.T) {
|
|
|
|
defer func() {
|
2023-04-04 13:52:24 -04:00
|
|
|
if err := tearDownQueueStore(); err != nil {
|
2019-02-25 21:01:13 -05:00
|
|
|
t.Fatal("Failed to tear down store ", err)
|
|
|
|
}
|
|
|
|
}()
|
2023-04-04 13:52:24 -04:00
|
|
|
store, err := setUpQueueStore(queueDir, 100)
|
2019-02-25 21:01:13 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Failed to create a queue store ", err)
|
|
|
|
}
|
2023-04-04 13:52:24 -04:00
|
|
|
// Put 100 items.
|
2019-02-25 21:01:13 -05:00
|
|
|
for i := 0; i < 100; i++ {
|
2024-09-06 19:06:30 -04:00
|
|
|
if _, err := store.Put(testItem); err != nil {
|
2019-02-25 21:01:13 -05:00
|
|
|
t.Fatal("Failed to put to queue store ", err)
|
|
|
|
}
|
|
|
|
}
|
2023-04-04 13:52:24 -04:00
|
|
|
// Count the items.
|
2024-09-06 19:06:30 -04:00
|
|
|
keys := store.List()
|
|
|
|
if len(keys) != 100 {
|
|
|
|
t.Fatalf("List() Expected: 100, got %d", len(keys))
|
2019-02-25 21:01:13 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestQueueStoreGet - tests for store.Get
|
|
|
|
func TestQueueStoreGet(t *testing.T) {
|
|
|
|
defer func() {
|
2023-04-04 13:52:24 -04:00
|
|
|
if err := tearDownQueueStore(); err != nil {
|
2019-02-25 21:01:13 -05:00
|
|
|
t.Fatal("Failed to tear down store ", err)
|
|
|
|
}
|
|
|
|
}()
|
2023-04-04 13:52:24 -04:00
|
|
|
store, err := setUpQueueStore(queueDir, 10)
|
2019-02-25 21:01:13 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Failed to create a queue store ", err)
|
|
|
|
}
|
2023-04-04 13:52:24 -04:00
|
|
|
// Put 10 items
|
2019-02-25 21:01:13 -05:00
|
|
|
for i := 0; i < 10; i++ {
|
2024-09-06 19:06:30 -04:00
|
|
|
if _, err := store.Put(testItem); err != nil {
|
2019-02-25 21:01:13 -05:00
|
|
|
t.Fatal("Failed to put to queue store ", err)
|
|
|
|
}
|
|
|
|
}
|
2024-09-06 19:06:30 -04:00
|
|
|
itemKeys := store.List()
|
2023-04-04 13:52:24 -04:00
|
|
|
// Get 10 items.
|
|
|
|
if len(itemKeys) == 10 {
|
|
|
|
for _, key := range itemKeys {
|
2024-09-06 19:06:30 -04:00
|
|
|
item, eErr := store.Get(key)
|
2019-02-25 21:01:13 -05:00
|
|
|
if eErr != nil {
|
2023-04-04 13:52:24 -04:00
|
|
|
t.Fatal("Failed to Get the item from the queue store ", eErr)
|
2019-02-25 21:01:13 -05:00
|
|
|
}
|
2023-04-04 13:52:24 -04:00
|
|
|
if !reflect.DeepEqual(testItem, item) {
|
|
|
|
t.Fatalf("Failed to read the item: error: expected = %v, got = %v", testItem, item)
|
2019-02-25 21:01:13 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2023-04-04 13:52:24 -04:00
|
|
|
t.Fatalf("List() Expected: 10, got %d", len(itemKeys))
|
2019-02-25 21:01:13 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestQueueStoreDel - tests for store.Del
|
|
|
|
func TestQueueStoreDel(t *testing.T) {
|
|
|
|
defer func() {
|
2023-04-04 13:52:24 -04:00
|
|
|
if err := tearDownQueueStore(); err != nil {
|
2019-02-25 21:01:13 -05:00
|
|
|
t.Fatal("Failed to tear down store ", err)
|
|
|
|
}
|
|
|
|
}()
|
2023-04-04 13:52:24 -04:00
|
|
|
store, err := setUpQueueStore(queueDir, 20)
|
2019-02-25 21:01:13 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Failed to create a queue store ", err)
|
|
|
|
}
|
2023-04-04 13:52:24 -04:00
|
|
|
// Put 20 items.
|
2019-02-25 21:01:13 -05:00
|
|
|
for i := 0; i < 20; i++ {
|
2024-09-06 19:06:30 -04:00
|
|
|
if _, err := store.Put(testItem); err != nil {
|
2019-02-25 21:01:13 -05:00
|
|
|
t.Fatal("Failed to put to queue store ", err)
|
|
|
|
}
|
|
|
|
}
|
2024-09-06 19:06:30 -04:00
|
|
|
itemKeys := store.List()
|
2023-04-04 13:52:24 -04:00
|
|
|
// Remove all the items.
|
|
|
|
if len(itemKeys) == 20 {
|
|
|
|
for _, key := range itemKeys {
|
2024-09-06 19:06:30 -04:00
|
|
|
err := store.Del(key)
|
2019-04-10 08:46:01 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("queue store Del failed with ", err)
|
|
|
|
}
|
2019-02-25 21:01:13 -05:00
|
|
|
}
|
|
|
|
} else {
|
2023-04-04 13:52:24 -04:00
|
|
|
t.Fatalf("List() Expected: 20, got %d", len(itemKeys))
|
2019-02-25 21:01:13 -05:00
|
|
|
}
|
|
|
|
|
2024-09-06 19:06:30 -04:00
|
|
|
keys := store.List()
|
|
|
|
if len(keys) != 0 {
|
|
|
|
t.Fatalf("List() Expected: 0, got %d", len(keys))
|
2019-02-25 21:01:13 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-04 13:52:24 -04:00
|
|
|
// TestQueueStoreLimit - tests the item limit for the store.
|
2019-02-25 21:01:13 -05:00
|
|
|
func TestQueueStoreLimit(t *testing.T) {
|
|
|
|
defer func() {
|
2023-04-04 13:52:24 -04:00
|
|
|
if err := tearDownQueueStore(); err != nil {
|
2019-02-25 21:01:13 -05:00
|
|
|
t.Fatal("Failed to tear down store ", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
// The max limit is set to 5.
|
2023-04-04 13:52:24 -04:00
|
|
|
store, err := setUpQueueStore(queueDir, 5)
|
2019-02-25 21:01:13 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Failed to create a queue store ", err)
|
|
|
|
}
|
|
|
|
for i := 0; i < 5; i++ {
|
2024-09-06 19:06:30 -04:00
|
|
|
if _, err := store.Put(testItem); err != nil {
|
2019-02-25 21:01:13 -05:00
|
|
|
t.Fatal("Failed to put to queue store ", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Should not allow 6th Put.
|
2024-09-06 19:06:30 -04:00
|
|
|
if _, err := store.Put(testItem); err == nil {
|
2019-04-10 08:46:01 -04:00
|
|
|
t.Fatalf("Expected to fail with %s, but passes", errLimitExceeded)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestQueueStoreLimit - tests for store.LimitN.
|
|
|
|
func TestQueueStoreListN(t *testing.T) {
|
|
|
|
defer func() {
|
2023-04-04 13:52:24 -04:00
|
|
|
if err := tearDownQueueStore(); err != nil {
|
2019-04-10 08:46:01 -04:00
|
|
|
t.Fatal("Failed to tear down store ", err)
|
|
|
|
}
|
|
|
|
}()
|
2023-04-04 13:52:24 -04:00
|
|
|
store, err := setUpQueueStore(queueDir, 10)
|
2019-04-10 08:46:01 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Failed to create a queue store ", err)
|
|
|
|
}
|
|
|
|
for i := 0; i < 10; i++ {
|
2024-09-06 19:06:30 -04:00
|
|
|
if _, err := store.Put(testItem); err != nil {
|
2019-04-10 08:46:01 -04:00
|
|
|
t.Fatal("Failed to put to queue store ", err)
|
|
|
|
}
|
|
|
|
}
|
2023-04-04 13:52:24 -04:00
|
|
|
// Should return all the item keys in the store.
|
2024-09-06 19:06:30 -04:00
|
|
|
keys := store.List()
|
2019-10-11 20:46:03 -04:00
|
|
|
|
2024-09-06 19:06:30 -04:00
|
|
|
if len(keys) != 10 {
|
|
|
|
t.Fatalf("List() Expected: 10, got %d", len(keys))
|
2019-10-11 20:46:03 -04:00
|
|
|
}
|
|
|
|
|
2022-11-08 19:36:47 -05:00
|
|
|
// re-open
|
2023-04-04 13:52:24 -04:00
|
|
|
store, err = setUpQueueStore(queueDir, 10)
|
2022-11-08 19:36:47 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Failed to create a queue store ", err)
|
|
|
|
}
|
2024-09-06 19:06:30 -04:00
|
|
|
keys = store.List()
|
2019-10-11 20:46:03 -04:00
|
|
|
|
2024-09-06 19:06:30 -04:00
|
|
|
if len(keys) != 10 {
|
|
|
|
t.Fatalf("List() Expected: 10, got %d", len(keys))
|
2022-11-08 19:36:47 -05:00
|
|
|
}
|
2024-09-06 19:06:30 -04:00
|
|
|
if len(keys) != store.Len() {
|
|
|
|
t.Fatalf("List() Expected: 10, got %d", len(keys))
|
2022-11-08 19:36:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Delete all
|
2024-09-06 19:06:30 -04:00
|
|
|
for _, key := range keys {
|
2022-11-08 19:36:47 -05:00
|
|
|
err := store.Del(key)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Re-list
|
2024-09-06 19:06:30 -04:00
|
|
|
keys = store.List()
|
|
|
|
if len(keys) > 0 || err != nil {
|
|
|
|
t.Fatalf("Expected List() to return empty list and no error, got %v err: %v", keys, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-06 19:32:39 -05:00
|
|
|
func TestMultiplePutGetRaw(t *testing.T) {
|
|
|
|
defer func() {
|
|
|
|
if err := tearDownQueueStore(); err != nil {
|
|
|
|
t.Fatalf("Failed to tear down store; %v", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
store, err := setUpQueueStore(queueDir, 10)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create a queue store; %v", err)
|
|
|
|
}
|
|
|
|
// TestItem{Name: "test-item", Property: "property"}
|
|
|
|
var items []TestItem
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
items = append(items, TestItem{
|
|
|
|
Name: fmt.Sprintf("test-item-%d", i),
|
|
|
|
Property: "property",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := bytebufferpool.Get()
|
|
|
|
defer bytebufferpool.Put(buf)
|
|
|
|
|
|
|
|
enc := jsoniter.ConfigCompatibleWithStandardLibrary.NewEncoder(buf)
|
|
|
|
for i := range items {
|
|
|
|
if err = enc.Encode(items[i]); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := store.PutMultiple(items); err != nil {
|
|
|
|
t.Fatalf("failed to put multiple; %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
keys := store.List()
|
|
|
|
if len(keys) != 1 {
|
|
|
|
t.Fatalf("expected len(keys)=1, but found %d", len(keys))
|
|
|
|
}
|
|
|
|
|
|
|
|
key := keys[0]
|
|
|
|
if !key.Compress {
|
|
|
|
t.Fatal("expected the item to be compressed")
|
|
|
|
}
|
|
|
|
if key.ItemCount != 10 {
|
|
|
|
t.Fatalf("expected itemcount=10 but found %v", key.ItemCount)
|
|
|
|
}
|
|
|
|
|
|
|
|
raw, err := store.GetRaw(key)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get multiple items; %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !bytes.Equal(buf.Bytes(), raw) {
|
|
|
|
t.Fatalf("expected bytes: %d vs read bytes is wrong %d", len(buf.Bytes()), len(raw))
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := store.Del(key); err != nil {
|
|
|
|
t.Fatalf("unable to Del; %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Re-list
|
|
|
|
keys = store.List()
|
|
|
|
if len(keys) > 0 || err != nil {
|
|
|
|
t.Fatalf("Expected List() to return empty list and no error, got %v err: %v", keys, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-06 19:06:30 -04:00
|
|
|
func TestMultiplePutGets(t *testing.T) {
|
|
|
|
defer func() {
|
|
|
|
if err := tearDownQueueStore(); err != nil {
|
|
|
|
t.Fatalf("Failed to tear down store; %v", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
store, err := setUpQueueStore(queueDir, 10)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create a queue store; %v", err)
|
|
|
|
}
|
|
|
|
// TestItem{Name: "test-item", Property: "property"}
|
|
|
|
var items []TestItem
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
items = append(items, TestItem{
|
|
|
|
Name: fmt.Sprintf("test-item-%d", i),
|
|
|
|
Property: "property",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := store.PutMultiple(items); err != nil {
|
|
|
|
t.Fatalf("failed to put multiple; %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
keys := store.List()
|
|
|
|
if len(keys) != 1 {
|
|
|
|
t.Fatalf("expected len(keys)=1, but found %d", len(keys))
|
|
|
|
}
|
|
|
|
|
|
|
|
key := keys[0]
|
|
|
|
if !key.Compress {
|
|
|
|
t.Fatal("expected the item to be compressed")
|
|
|
|
}
|
|
|
|
if key.ItemCount != 10 {
|
|
|
|
t.Fatalf("expected itemcount=10 but found %v", key.ItemCount)
|
|
|
|
}
|
|
|
|
|
|
|
|
resultItems, err := store.GetMultiple(key)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get multiple items; %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(resultItems, items) {
|
|
|
|
t.Fatalf("expected item list: %v; but got %v", items, resultItems)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := store.Del(key); err != nil {
|
|
|
|
t.Fatalf("unable to Del; %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Re-list
|
|
|
|
keys = store.List()
|
|
|
|
if len(keys) > 0 || err != nil {
|
|
|
|
t.Fatalf("Expected List() to return empty list and no error, got %v err: %v", keys, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMixedPutGets(t *testing.T) {
|
|
|
|
defer func() {
|
|
|
|
if err := tearDownQueueStore(); err != nil {
|
|
|
|
t.Fatalf("Failed to tear down store; %v", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
store, err := setUpQueueStore(queueDir, 10)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create a queue store; %v", err)
|
|
|
|
}
|
|
|
|
// TestItem{Name: "test-item", Property: "property"}
|
|
|
|
var items []TestItem
|
|
|
|
for i := 0; i < 5; i++ {
|
|
|
|
items = append(items, TestItem{
|
|
|
|
Name: fmt.Sprintf("test-item-%d", i),
|
|
|
|
Property: "property",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if _, err := store.PutMultiple(items); err != nil {
|
|
|
|
t.Fatalf("failed to put multiple; %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 5; i < 10; i++ {
|
|
|
|
item := TestItem{
|
|
|
|
Name: fmt.Sprintf("test-item-%d", i),
|
|
|
|
Property: "property",
|
|
|
|
}
|
|
|
|
if _, err := store.Put(item); err != nil {
|
|
|
|
t.Fatalf("unable to store.Put(); %v", err)
|
|
|
|
}
|
|
|
|
items = append(items, item)
|
|
|
|
}
|
|
|
|
|
|
|
|
keys := store.List()
|
|
|
|
if len(keys) != 6 {
|
|
|
|
// 1 multiple + 5 single PUTs
|
|
|
|
t.Fatalf("expected len(keys)=6, but found %d", len(keys))
|
|
|
|
}
|
|
|
|
|
|
|
|
var resultItems []TestItem
|
|
|
|
for _, key := range keys {
|
|
|
|
if key.ItemCount > 1 {
|
|
|
|
items, err := store.GetMultiple(key)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get multiple items; %v", err)
|
|
|
|
}
|
|
|
|
resultItems = append(resultItems, items...)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
item, err := store.Get(key)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get item; %v", err)
|
|
|
|
}
|
|
|
|
resultItems = append(resultItems, item)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(resultItems, items) {
|
|
|
|
t.Fatalf("expected item list: %v; but got %v", items, resultItems)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete all
|
|
|
|
for _, key := range keys {
|
|
|
|
if err := store.Del(key); err != nil {
|
|
|
|
t.Fatalf("unable to Del; %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Re-list
|
|
|
|
keys = store.List()
|
|
|
|
if len(keys) > 0 || err != nil {
|
|
|
|
t.Fatalf("Expected List() to return empty list and no error, got %v err: %v", keys, err)
|
2019-02-25 21:01:13 -05:00
|
|
|
}
|
|
|
|
}
|