本文整理匯總了Golang中github.com/portworx/kvdb.Kvdb.CompareAndSet方法的典型用法代碼示例。如果您正苦於以下問題:Golang Kvdb.CompareAndSet方法的具體用法?Golang Kvdb.CompareAndSet怎麽用?Golang Kvdb.CompareAndSet使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/portworx/kvdb.Kvdb
的用法示例。
在下文中一共展示了Kvdb.CompareAndSet方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: cas
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")
}