From 7d582e1b5ba28da7bb5375a90807e5bbb60c1627 Mon Sep 17 00:00:00 2001 From: Tobias Schottdorf Date: Wed, 14 Jan 2015 07:32:01 +0100 Subject: [PATCH] add test for ConditionFailedError this adds some bits that were missing in #247 --- proto/errors.proto | 2 -- storage/range_test.go | 35 ++++++++++++++++++++++++++++++++++ storage/response_cache_test.go | 1 + 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/proto/errors.proto b/proto/errors.proto index c12ef903b6a7..01b385523068 100644 --- a/proto/errors.proto +++ b/proto/errors.proto @@ -128,8 +128,6 @@ message ConditionFailedError { } // Error is a union type containing all available errors. -// NOTE: new error types must be added here, and potentially in -// the two locations (*ResponseHeader).{,Set}GoError(). message Error { option (gogoproto.onlyone) = true; optional GenericError generic = 1; diff --git a/storage/range_test.go b/storage/range_test.go index 33a59117b11a..5a8423660481 100644 --- a/storage/range_test.go +++ b/storage/range_test.go @@ -1600,3 +1600,38 @@ func TestInternalMerge(t *testing.T) { t.Errorf("Get did not return expected value: %s != %s", string(a), e) } } + +// TestConditionFailedError tests that a ConditionFailedError correctly +// bubbles up from MVCC to Range. +func TestConditionFailedError(t *testing.T) { + s, r, _, _ := createTestRange(t) + key := []byte("k") + value := []byte("quack") + pArgs, pReply := putArgs(key, value, 1, s.StoreID()) + if err := r.executeCmd(proto.Put, pArgs, pReply); err != nil { + t.Fatal(err) + } + args := &proto.ConditionalPutRequest{ + RequestHeader: proto.RequestHeader{ + Key: key, + Timestamp: proto.MinTimestamp, + RaftID: 1, + Replica: proto.Replica{StoreID: s.StoreID()}, + }, + Value: proto.Value{ + Bytes: value, + }, + ExpValue: &proto.Value{ + Bytes: []byte("moo"), + }, + } + reply := &proto.ConditionalPutResponse{} + err := r.executeCmd(proto.ConditionalPut, args, reply) + if cErr, ok := err.(*proto.ConditionFailedError); err == nil || !ok { + t.Fatalf("expected ConditionFailedError, got %T with content %+v", + err, err) + } else if v := cErr.ActualValue; v == nil || !bytes.Equal(v.Bytes, value) { + t.Errorf("ConditionFailedError with bytes %q expected, but got %+v", + value, v) + } +} diff --git a/storage/response_cache_test.go b/storage/response_cache_test.go index 2b94e2572ab5..d5c2344c9958 100644 --- a/storage/response_cache_test.go +++ b/storage/response_cache_test.go @@ -240,6 +240,7 @@ func TestResponseCacheShouldCache(t *testing.T) { {&proto.RangeNotFoundError{}, true}, {&proto.RangeKeyMismatchError{}, true}, {&proto.TransactionStatusError{}, true}, + {&proto.ConditionFailedError{}, true}, {&proto.WriteIntentError{}, false}, {&proto.WriteTooOldError{}, false}, }