consistent_watchable_store_test.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // Copyright 2015 CoreOS, Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package storage
  15. import (
  16. "testing"
  17. "github.com/coreos/etcd/storage/backend"
  18. )
  19. type indexVal uint64
  20. func (v *indexVal) ConsistentIndex() uint64 { return uint64(*v) }
  21. func TestConsistentWatchableStoreConsistentIndex(t *testing.T) {
  22. var idx indexVal
  23. b, tmpPath := backend.NewDefaultTmpBackend()
  24. s := newConsistentWatchableStore(b, &idx)
  25. defer cleanup(s, b, tmpPath)
  26. tests := []uint64{1, 2, 3, 5, 10}
  27. for i, tt := range tests {
  28. idx = indexVal(tt)
  29. s.Put([]byte("foo"), []byte("bar"), NoLease)
  30. id := s.TxnBegin()
  31. g := s.consistentIndex()
  32. s.TxnEnd(id)
  33. if g != tt {
  34. t.Errorf("#%d: index = %d, want %d", i, g, tt)
  35. }
  36. }
  37. }
  38. func TestConsistentWatchableStoreSkip(t *testing.T) {
  39. idx := indexVal(5)
  40. b, tmpPath := backend.NewDefaultTmpBackend()
  41. s := newConsistentWatchableStore(b, &idx)
  42. defer cleanup(s, b, tmpPath)
  43. s.Put([]byte("foo"), []byte("bar"), NoLease)
  44. // put is skipped
  45. rev := s.Put([]byte("foo"), []byte("bar"), NoLease)
  46. if rev != 0 {
  47. t.Errorf("rev = %d, want 0", rev)
  48. }
  49. }