github.com/whtcorpsinc/MilevaDB-Prod@v0.0.0-20211104133533-f57f4be3b597/causetstore/ekv/fault_injection_test.go (about) 1 // Copyright 2020 WHTCORPS INC, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 package ekv 15 16 import ( 17 "context" 18 19 "github.com/whtcorpsinc/BerolinaSQL/terror" 20 . "github.com/whtcorpsinc/check" 21 "github.com/whtcorpsinc/errors" 22 ) 23 24 type testFaultInjectionSuite struct{} 25 26 var _ = Suite(testFaultInjectionSuite{}) 27 28 func (s testFaultInjectionSuite) TestFaultInjectionBasic(c *C) { 29 var cfg InjectionConfig 30 err1 := errors.New("foo") 31 cfg.SetGetError(err1) 32 cfg.SetCommitError(err1) 33 34 storage := NewInjectedStore(newMockStorage(), &cfg) 35 txn, err := storage.Begin() 36 c.Assert(err, IsNil) 37 _, err = storage.BeginWithStartTS(0) 38 c.Assert(err, IsNil) 39 ver := Version{Ver: 1} 40 snap, err := storage.GetSnapshot(ver) 41 c.Assert(err, IsNil) 42 b, err := txn.Get(context.TODO(), []byte{'a'}) 43 c.Assert(err.Error(), Equals, err1.Error()) 44 c.Assert(b, IsNil) 45 b, err = snap.Get(context.TODO(), []byte{'a'}) 46 c.Assert(err.Error(), Equals, err1.Error()) 47 c.Assert(b, IsNil) 48 49 bs, err := snap.BatchGet(context.Background(), nil) 50 c.Assert(err.Error(), Equals, err1.Error()) 51 c.Assert(bs, IsNil) 52 53 bs, err = txn.BatchGet(context.Background(), nil) 54 c.Assert(err.Error(), Equals, err1.Error()) 55 c.Assert(bs, IsNil) 56 57 err = txn.Commit(context.Background()) 58 c.Assert(err.Error(), Equals, err1.Error()) 59 60 cfg.SetGetError(nil) 61 cfg.SetCommitError(nil) 62 63 storage = NewInjectedStore(newMockStorage(), &cfg) 64 txn, err = storage.Begin() 65 c.Assert(err, IsNil) 66 snap, err = storage.GetSnapshot(ver) 67 c.Assert(err, IsNil) 68 69 b, err = txn.Get(context.TODO(), []byte{'a'}) 70 c.Assert(err, IsNil) 71 c.Assert(b, IsNil) 72 73 bs, err = txn.BatchGet(context.Background(), nil) 74 c.Assert(err, IsNil) 75 c.Assert(bs, IsNil) 76 77 b, err = snap.Get(context.TODO(), []byte{'a'}) 78 c.Assert(terror.ErrorEqual(ErrNotExist, err), IsTrue) 79 c.Assert(b, IsNil) 80 81 bs, err = snap.BatchGet(context.Background(), []Key{[]byte("a")}) 82 c.Assert(err, IsNil) 83 c.Assert(len(bs), Equals, 0) 84 85 err = txn.Commit(context.Background()) 86 c.Assert(err, NotNil) 87 c.Assert(terror.ErrorEqual(err, ErrTxnRetryable), IsTrue) 88 }