func deleteTree(kv kvdb.Kvdb, t *testing.T) { fmt.Println("deleteTree") prefix := "tree" keys := map[string]string{ prefix + "/1cbc9a98-072a-4793-8608-01ab43db96c8": "bar", prefix + "/foo": "baz", } for key, val := range keys { _, err := kv.Put(key, []byte(val), 0) assert.NoError(t, err, "Unexpected error on Put") } for key := range keys { _, err := kv.Get(key) assert.NoError(t, err, "Unexpected error on Get") } err := kv.DeleteTree(prefix) assert.NoError(t, err, "Unexpected error on DeleteTree") for key := range keys { _, err := kv.Get(key) assert.Error(t, err, "Get should fail on all keys after DeleteTree") } }
func watchWithIndex(kv kvdb.Kvdb, t *testing.T) { fmt.Println("\nwatchWithIndex") tree := "indexTree" subtree := tree + "/subtree" key := subtree + "/watchWithIndex" key1 := subtree + "/watchWithIndex21" kv.DeleteTree(tree) kvp, err := kv.Create(key, []byte("bar"), 0) assert.NoError(t, err, "Unexpected error in create: %v", err) time.Sleep(time.Millisecond * 100) waitIndex := kvp.ModifiedIndex + 2 watchData := watchData{ t: t, key: key, localIndex: waitIndex, } err = kv.WatchTree(tree, waitIndex, &watchData, watchWithIndexCb) if err != nil { fmt.Printf("Cannot test watchTree for watchWithIndex: %v", err) return } // Should not get updates for these kv.Put(key, []byte("bar1"), 0) kv.Put(key, []byte("bar1"), 0) // Should get updates for these kv.Put(key, []byte("bar2"), 0) kv.Create(key1, []byte("bar"), 0) kv.Delete(key) }
func getInterface(kv kvdb.Kvdb, t *testing.T) { fmt.Println("getInterface") expected := struct { N int S string }{ N: 10, S: "Ten", } actual := expected actual.N = 0 actual.S = "zero" key := "DEADBEEF" _, err := kv.Delete(key) _, err = kv.Put(key, &expected, 0) assert.NoError(t, err, "Failed in Put") _, err = kv.GetVal(key, &actual) assert.NoError(t, err, "Failed in Get") assert.Equal(t, expected, actual, "Expected %#v but got %#v", expected, actual) }
func watchUpdate(kv kvdb.Kvdb, data *watchData) error { var err error var kvp *kvdb.KVPair data.reader, data.writer = 0, 0 atomic.AddInt32(&data.writer, 1) // whichKey = 1 : key // whichKey = 0 : otherKey atomic.SwapInt32(&data.whichKey, 1) data.action = kvdb.KVCreate fmt.Printf("-") kvp, err = kv.Create(data.key, []byte("bar"), 0) for i := 0; i < data.iterations && err == nil; i++ { fmt.Printf("-") for data.writer != data.reader { time.Sleep(time.Millisecond * 100) } atomic.AddInt32(&data.writer, 1) data.action = kvdb.KVSet kvp, err = kv.Put(data.key, []byte("bar"), 0) data.updateIndex = kvp.KVDBIndex assert.NoError(data.t, err, "Unexpected error in Put") } fmt.Printf("-") for data.writer != data.reader { time.Sleep(time.Millisecond * 100) } atomic.AddInt32(&data.writer, 1) // Delete key data.action = kvdb.KVDelete kv.Delete(data.key) fmt.Printf("-") for data.writer != data.reader { time.Sleep(time.Millisecond * 100) } atomic.AddInt32(&data.writer, 1) atomic.SwapInt32(&data.whichKey, 0) data.action = kvdb.KVDelete // Delete otherKey kv.Delete(data.otherKey) fmt.Printf("-") for data.writer != data.reader { time.Sleep(time.Millisecond * 100) } atomic.AddInt32(&data.writer, 1) atomic.SwapInt32(&data.whichKey, 1) data.action = kvdb.KVCreate _, err = kv.Create(data.key, []byte(data.stop), 0) return err }
func enumerate(kv kvdb.Kvdb, t *testing.T) { fmt.Println("enumerate") prefix := "enumerate" keys := map[string]string{ prefix + "/1cbc9a98-072a-4793-8608-01ab43db96c8": "bar", prefix + "/foo": "baz", } kv.DeleteTree(prefix) defer func() { kv.DeleteTree(prefix) }() errPairs, err := kv.Enumerate(prefix) assert.Equal(t, 0, len(errPairs), "Expected 0 pairs") folderKey := prefix + "/folder/" _, err = kv.Put(folderKey, []byte(""), 0) assert.NoError(t, err, "Unexpected error on Put") kvPairs, err := kv.Enumerate(folderKey) assert.Equal(t, nil, err, "Unexpected error on Enumerate") kv.DeleteTree(prefix) for key, val := range keys { _, err := kv.Put(key, []byte(val), 0) assert.NoError(t, err, "Unexpected error on Put") } kvPairs, err = kv.Enumerate(prefix) assert.NoError(t, err, "Unexpected error on Enumerate") assert.Equal(t, len(kvPairs), len(keys), "Expecting %d keys under %s got: %d", len(keys), prefix, len(kvPairs)) for i := range kvPairs { v, ok := keys[kvPairs[i].Key] assert.True(t, ok, "unexpected kvpair (%s)->(%s)", kvPairs[i].Key, kvPairs[i].Value) assert.Equal(t, v, string(kvPairs[i].Value), "Invalid kvpair (%s)->(%s) expect value %s", kvPairs[i].Key, kvPairs[i].Value, v) } }
func cas(kv kvdb.Kvdb, t *testing.T) { fmt.Println("\ncas") key := "foo/docker" val := "great" defer func() { kv.Delete(key) }() kvPair, err := kv.Put(key, []byte(val), 0) assert.NoError(t, err, "Unxpected error in Put") kvPair, err = kv.Get(key) assert.NoError(t, err, "Failed in Get") _, err = kv.CompareAndSet(kvPair, kvdb.KVFlags(0), []byte("badval")) assert.Error(t, err, "CompareAndSet should fail on an incorrect previous value") //assert.EqualError(t, err, kvdb.ErrValueMismatch.Error(), "CompareAndSet should return value mismatch error") copyKVPair := *kvPair copyKVPair.ModifiedIndex++ _, err = kv.CompareAndSet(©KVPair, kvdb.KVModifiedIndex, nil) assert.Error(t, err, "CompareAndSet should fail on an incorrect modified index") //kvPair.ModifiedIndex-- copyKVPair.ModifiedIndex-- kvPair, err = kv.CompareAndSet(©KVPair, kvdb.KVModifiedIndex, nil) assert.NoError(t, err, "CompareAndSet should succeed on an correct modified index") kvPairNew, err := kv.CompareAndSet(kvPair, kvdb.KVFlags(0), []byte(val)) if err != nil { // consul does not handle this kind of compare and set assert.EqualError(t, err, kvdb.ErrNotSupported.Error(), "Invalid error returned : %v", err) } else { assert.NoError(t, err, "CompareAndSet should succeed on an correct value") } if kvPairNew != nil { kvPair = kvPairNew } kvPair, err = kv.CompareAndSet(kvPair, kvdb.KVModifiedIndex, []byte(val)) assert.NoError(t, err, "CompareAndSet should succeed on an correct value and modified index") }
func deleteKey(kv kvdb.Kvdb, t *testing.T) { fmt.Println("deleteKey") key := "delete_key" _, err := kv.Delete(key) _, err = kv.Put(key, []byte("delete_me"), 0) assert.NoError(t, err, "Unexpected error on Put") _, err = kv.Get(key) assert.NoError(t, err, "Unexpected error on Get") _, err = kv.Delete(key) assert.NoError(t, err, "Unexpected error on Delete") _, err = kv.Get(key) assert.Error(t, err, "Get should fail on deleted key") _, err = kv.Delete(key) assert.Error(t, err, "Delete should fail on non existent key") }
func get(kv kvdb.Kvdb, t *testing.T) { fmt.Println("get") kvPair, err := kv.Get("DEADCAFE") assert.Error(t, err, "Expecting error value for non-existent value") key := "foo/docker" val := "great" defer func() { kv.Delete(key) }() kvPair, err = kv.Put(key, []byte(val), 0) assert.NoError(t, err, "Unexpected error in Put") kvPair, err = kv.Get(key) assert.NoError(t, err, "Failed in Get") assert.Equal(t, key, kvPair.Key, "Key mismatch in Get") assert.Equal(t, string(kvPair.Value), val, "value mismatch in Get") }
func randomUpdate(kv kvdb.Kvdb, w *watchData) { for w.watchStopped == false { kv.Put("randomKey", []byte("bar"), 0) time.Sleep(time.Millisecond * 80) } }
func snapshot(kv kvdb.Kvdb, t *testing.T) { fmt.Println("snapshot") prefix := "snapshot/" kv.DeleteTree(prefix) defer func() { kv.DeleteTree(prefix) }() inputData := make(map[string]string) inputDataVersion := make(map[string]uint64) key := "key" value := "bar" count := 100 doneUpdate := make(chan bool, 2) updateFn := func(count int, v string) { for i := 0; i < count; i++ { suffix := strconv.Itoa(i) inputKey := prefix + key + suffix inputValue := v kv, err := kv.Put(inputKey, []byte(inputValue), 0) assert.NoError(t, err, "Unexpected error on Put") inputData[inputKey] = inputValue inputDataVersion[inputKey] = kv.ModifiedIndex } doneUpdate <- true } updateFn(count, value) <-doneUpdate newValue := "bar2" go updateFn(50, newValue) snap, snapVersion, err := kv.Snapshot(prefix) assert.NoError(t, err, "Unexpected error on Snapshot") <-doneUpdate kvPairs, err := snap.Enumerate(prefix) assert.NoError(t, err, "Unexpected error on Enumerate") assert.Equal(t, len(kvPairs), count, "Expecting %d keys under %s got: %d, kv: %v", count, prefix, len(kvPairs), kvPairs) for i := range kvPairs { currValue, ok1 := inputData[kvPairs[i].Key] mapVersion, ok2 := inputDataVersion[kvPairs[i].Key] assert.True(t, ok1 && ok2, "unexpected kvpair (%s)->(%s)", kvPairs[i].Key, kvPairs[i].Value) expectedValue := value if mapVersion <= snapVersion { expectedValue = currValue } assert.Equal(t, expectedValue, string(kvPairs[i].Value), "Invalid kvpair %v (%s)->(%s) expect value %s"+ " snap version: %v kvVersion: %v", i, kvPairs[i].Key, kvPairs[i].Value, expectedValue, snapVersion, mapVersion) assert.True(t, mapVersion > snapVersion || mapVersion == kvPairs[i].ModifiedIndex, "Invalid kvpair %v (%s)->(%s) expect version %v"+ " snap version: %v kvVersion: %v", i, kvPairs[i].Key, kvPairs[i].Value, mapVersion, snapVersion, kvPairs[i].KVDBIndex) } }