kv_test.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package storage
  2. import (
  3. "fmt"
  4. "os"
  5. "reflect"
  6. "testing"
  7. )
  8. type kv struct {
  9. k, v []byte
  10. }
  11. // TestWorkflow simulates the whole workflow that storage is used in normal
  12. // etcd running, including key changes, compaction and restart.
  13. func TestWorkflow(t *testing.T) {
  14. s := newStore("test")
  15. defer os.Remove("test")
  16. var lastrev int64
  17. var wkvs []kv
  18. for i := 0; i < 10; i++ {
  19. // regular compaction
  20. s.Compact(lastrev)
  21. // put 100 keys into the store in each round
  22. for k := 0; k < 100; k++ {
  23. key := fmt.Sprintf("bar_%03d_%03d", i, k)
  24. val := fmt.Sprintf("foo_%03d_%03d", i, k)
  25. s.Put([]byte(key), []byte(val))
  26. wkvs = append(wkvs, kv{k: []byte(key), v: []byte(val)})
  27. }
  28. // delete second-half keys in this round
  29. key := fmt.Sprintf("bar_%03d_050", i)
  30. end := fmt.Sprintf("bar_%03d_100", i)
  31. if n, _ := s.DeleteRange([]byte(key), []byte(end)); n != 50 {
  32. t.Errorf("#%d: delete number = %d, want 50", i, n)
  33. }
  34. wkvs = wkvs[:len(wkvs)-50]
  35. // check existing keys
  36. kvs, rev, err := s.Range([]byte("bar"), []byte("bas"), 0, 0)
  37. if err != nil {
  38. t.Errorf("#%d: range error (%v)", err)
  39. }
  40. for j, kv := range kvs {
  41. if !reflect.DeepEqual(kv.Key, wkvs[j].k) {
  42. t.Errorf("#%d: keys[%d] = %s, want %s", i, j, kv.Key, wkvs[j].k)
  43. }
  44. if !reflect.DeepEqual(kv.Value, wkvs[j].v) {
  45. t.Errorf("#%d: vals[%d] = %s, want %s", i, j, kv.Value, wkvs[j].v)
  46. }
  47. }
  48. lastrev = rev
  49. // the store is restarted and restored from the disk file
  50. s.Close()
  51. s = newStore("test")
  52. s.Restore()
  53. }
  54. }