|
18 | 18 | package store
|
19 | 19 |
|
20 | 20 | import (
|
| 21 | + "bytes" |
21 | 22 | "fmt"
|
22 | 23 | "os"
|
23 | 24 | "path/filepath"
|
24 | 25 | "reflect"
|
25 | 26 | "testing"
|
| 27 | + |
| 28 | + jsoniter "github.com/json-iterator/go" |
| 29 | + "github.com/valyala/bytebufferpool" |
26 | 30 | )
|
27 | 31 |
|
28 | 32 | type TestItem struct {
|
@@ -221,6 +225,72 @@ func TestQueueStoreListN(t *testing.T) {
|
221 | 225 | }
|
222 | 226 | }
|
223 | 227 |
|
| 228 | +func TestMultiplePutGetRaw(t *testing.T) { |
| 229 | + defer func() { |
| 230 | + if err := tearDownQueueStore(); err != nil { |
| 231 | + t.Fatalf("Failed to tear down store; %v", err) |
| 232 | + } |
| 233 | + }() |
| 234 | + store, err := setUpQueueStore(queueDir, 10) |
| 235 | + if err != nil { |
| 236 | + t.Fatalf("Failed to create a queue store; %v", err) |
| 237 | + } |
| 238 | + // TestItem{Name: "test-item", Property: "property"} |
| 239 | + var items []TestItem |
| 240 | + for i := 0; i < 10; i++ { |
| 241 | + items = append(items, TestItem{ |
| 242 | + Name: fmt.Sprintf("test-item-%d", i), |
| 243 | + Property: "property", |
| 244 | + }) |
| 245 | + } |
| 246 | + |
| 247 | + buf := bytebufferpool.Get() |
| 248 | + defer bytebufferpool.Put(buf) |
| 249 | + |
| 250 | + enc := jsoniter.ConfigCompatibleWithStandardLibrary.NewEncoder(buf) |
| 251 | + for i := range items { |
| 252 | + if err = enc.Encode(items[i]); err != nil { |
| 253 | + t.Fatal(err) |
| 254 | + } |
| 255 | + } |
| 256 | + |
| 257 | + if _, err := store.PutMultiple(items); err != nil { |
| 258 | + t.Fatalf("failed to put multiple; %v", err) |
| 259 | + } |
| 260 | + |
| 261 | + keys := store.List() |
| 262 | + if len(keys) != 1 { |
| 263 | + t.Fatalf("expected len(keys)=1, but found %d", len(keys)) |
| 264 | + } |
| 265 | + |
| 266 | + key := keys[0] |
| 267 | + if !key.Compress { |
| 268 | + t.Fatal("expected the item to be compressed") |
| 269 | + } |
| 270 | + if key.ItemCount != 10 { |
| 271 | + t.Fatalf("expected itemcount=10 but found %v", key.ItemCount) |
| 272 | + } |
| 273 | + |
| 274 | + raw, err := store.GetRaw(key) |
| 275 | + if err != nil { |
| 276 | + t.Fatalf("unable to get multiple items; %v", err) |
| 277 | + } |
| 278 | + |
| 279 | + if !bytes.Equal(buf.Bytes(), raw) { |
| 280 | + t.Fatalf("expected bytes: %d vs read bytes is wrong %d", len(buf.Bytes()), len(raw)) |
| 281 | + } |
| 282 | + |
| 283 | + if err := store.Del(key); err != nil { |
| 284 | + t.Fatalf("unable to Del; %v", err) |
| 285 | + } |
| 286 | + |
| 287 | + // Re-list |
| 288 | + keys = store.List() |
| 289 | + if len(keys) > 0 || err != nil { |
| 290 | + t.Fatalf("Expected List() to return empty list and no error, got %v err: %v", keys, err) |
| 291 | + } |
| 292 | +} |
| 293 | + |
224 | 294 | func TestMultiplePutGets(t *testing.T) {
|
225 | 295 | defer func() {
|
226 | 296 | if err := tearDownQueueStore(); err != nil {
|
|
0 commit comments