Ejemplo n.º 1
0
Archivo: kv.go Proyecto: portworx/kvdb
func update(kv kvdb.Kvdb, t *testing.T) {
	fmt.Println("update")

	key := "update/foo"
	kv.Delete(key)

	kvp, err := kv.Update(key, []byte("bar"), 0)
	assert.Error(t, err, "Update should error on non-existent key")

	defer func() {
		kv.Delete(key)
	}()

	kvp, err = kv.Create(key, []byte("bar"), 0)
	assert.NoError(t, err, "Unexpected error on create")

	kvp, err = kv.Update(key, []byte("bar"), 0)
	assert.NoError(t, err, "Unexpected error on update")

	assert.Equal(t, kvp.Action, kvdb.KVSet,
		"Expected action KVSet, actual %v", kvp.Action)
}
Ejemplo n.º 2
0
Archivo: kv.go Proyecto: portworx/kvdb
func collect(kv kvdb.Kvdb, t *testing.T) {
	fmt.Println("collect")

	root := "pwx/test/collect"
	firstLevel := root + "/first"
	secondLevel := root + "/second"

	kv.DeleteTree(root)
	kvp, _ := kv.Create(firstLevel, []byte("bar"), 0)
	fmt.Printf("KVP is %v", kvp)
	collector, _ := kvdb.NewUpdatesCollector(kv, secondLevel, kvp.CreatedIndex)
	time.Sleep(time.Second)
	var updates []*kvdb.KVPair
	updateFn := func(start, end int) uint64 {
		maxVersion := uint64(0)
		for i := start; i < end; i++ {
			newLeaf := strconv.Itoa(i)
			kvp1, _ := kv.Create(secondLevel+"/"+newLeaf, []byte(newLeaf), 0)
			kvp2, _ := kv.Update(firstLevel, []byte(newLeaf), 0)
			updates = append(updates, kvp1)
			maxVersion = kvp2.ModifiedIndex
		}
		return maxVersion
	}
	updateFn(0, 10)
	// Allow watch updates to come back.
	time.Sleep(time.Millisecond * 500)
	collector.Stop()

	updateFn(10, 20)
	lastKVIndex := kvp.CreatedIndex
	lastLeafIndex := -1
	cb := func(prefix string, opaque interface{}, kvp *kvdb.KVPair,
		err error) error {
		assert.True(t, err == nil, "Error is nil %v", err)
		assert.True(t, kvp.ModifiedIndex > lastKVIndex,
			"Modified index %v lower than last index %v",
			kvp.ModifiedIndex, lastKVIndex)
		lastKVIndex = kvp.ModifiedIndex
		strValue := string(kvp.Value)
		value := secondLevel + "/" + strValue
		assert.True(t, strings.Compare(kvp.Key, value) == 0,
			"Key: %v, Value: %v", kvp.Key, value)
		leafIndex, _ := strconv.Atoi(strValue)
		assert.True(t, leafIndex == lastLeafIndex+1,
			"Last leaf: %v, leaf: %v", kvp.Key,
			value)
		lastLeafIndex = leafIndex
		return nil
	}

	replayCb := make([]kvdb.ReplayCb, 1)
	replayCb[0].Prefix = secondLevel
	replayCb[0].WatchCB = cb

	_, err := collector.ReplayUpdates(replayCb)
	assert.True(t, err == nil, "Replay encountered error %v", err)
	assert.True(t, lastLeafIndex == 9, "Last leaf index %v, expected : 9",
		lastLeafIndex)

	// Test with no updates.
	thirdLevel := root + "/third"
	kvp, _ = kv.Create(thirdLevel, []byte("bar_update"), 0)
	collector, _ = kvdb.NewUpdatesCollector(kv, thirdLevel, kvp.ModifiedIndex)
	time.Sleep(2 * time.Second)
	_, err = collector.ReplayUpdates(replayCb)
	assert.True(t, err == nil, "Replay encountered error %v", err)
	assert.True(t, lastLeafIndex == 9, "Last leaf index %v, expected : 9",
		lastLeafIndex)

	// Test with kvdb returning error because update index was too old.
	fourthLevel := root + "/fourth"
	kv.Create(fourthLevel, []byte(strconv.Itoa(0)), 0)
	for i := 1; i < 2000; i++ {
		kv.Update(fourthLevel, []byte(strconv.Itoa(i)), 0)
	}
	collector, _ = kvdb.NewUpdatesCollector(kv, fourthLevel, kvp.ModifiedIndex)
	kv.Update(fourthLevel, []byte(strconv.Itoa(2000)), 0)
	time.Sleep(500 * time.Millisecond)
	cb = func(prefix string, opaque interface{}, kvp *kvdb.KVPair,
		err error) error {
		fmt.Printf("Error is %v", err)
		assert.True(t, err != nil, "Error is nil %v", err)
		return nil
	}
	replayCb[0].WatchCB = cb
	collector.ReplayUpdates(replayCb)
}