consistent_watchable_store_test.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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/lease"
  18. "github.com/coreos/etcd/storage/backend"
  19. )
  20. type indexVal uint64
  21. func (v *indexVal) ConsistentIndex() uint64 { return uint64(*v) }
  22. func TestConsistentWatchableStoreConsistentIndex(t *testing.T) {
  23. var idx indexVal
  24. b, tmpPath := backend.NewDefaultTmpBackend()
  25. s := newConsistentWatchableStore(b, &lease.FakeLessor{}, &idx)
  26. defer cleanup(s, b, tmpPath)
  27. tests := []uint64{1, 2, 3, 5, 10}
  28. for i, tt := range tests {
  29. idx = indexVal(tt)
  30. s.Put([]byte("foo"), []byte("bar"), lease.NoLease)
  31. id := s.TxnBegin()
  32. g := s.consistentIndex()
  33. s.TxnEnd(id)
  34. if g != tt {
  35. t.Errorf("#%d: index = %d, want %d", i, g, tt)
  36. }
  37. }
  38. }
  39. func TestConsistentWatchableStoreSkip(t *testing.T) {
  40. idx := indexVal(5)
  41. b, tmpPath := backend.NewDefaultTmpBackend()
  42. s := newConsistentWatchableStore(b, &lease.FakeLessor{}, &idx)
  43. defer cleanup(s, b, tmpPath)
  44. s.Put([]byte("foo"), []byte("bar"), lease.NoLease)
  45. // put is skipped
  46. rev := s.Put([]byte("foo"), []byte("bar"), lease.NoLease)
  47. if rev != 0 {
  48. t.Errorf("rev = %d, want 0", rev)
  49. }
  50. }