Ejemplo n.º 1
0
Archivo: kv.go Proyecto: portworx/kvdb
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)
	}
}