go.mercari.io/datastore@v1.8.2/testsuite/dsmiddleware/rpcretry/glitch_emulator.go (about) 1 package rpcretry 2 3 import ( 4 "fmt" 5 "strings" 6 7 "go.mercari.io/datastore" 8 ) 9 10 var _ datastore.Middleware = &glitchEmulator{} 11 12 type glitchEmulator struct { 13 raised map[string]map[string]int // raised["PutMultiWithoutTx"]["Data/1"] = 1 14 errCount int 15 } 16 17 func (gm *glitchEmulator) keysToString(keys []datastore.Key) string { 18 keyStrings := make([]string, 0, len(keys)) 19 for _, key := range keys { 20 keyStrings = append(keyStrings, key.String()) 21 } 22 23 return strings.Join(keyStrings, ", ") 24 } 25 26 func (gm *glitchEmulator) raiseError(opName string, keys []datastore.Key) error { 27 if gm.raised == nil { 28 gm.raised = make(map[string]map[string]int) 29 } 30 if _, ok := gm.raised[opName]; !ok { 31 gm.raised[opName] = make(map[string]int) 32 } 33 keysStr := gm.keysToString(keys) 34 cnt := gm.raised[opName][keysStr] 35 if cnt != gm.errCount { 36 gm.raised[opName][keysStr] = cnt + 1 37 return fmt.Errorf("error by *glitchEmulator: %s, keys=%s", opName, keysStr) 38 } 39 40 return nil 41 } 42 43 func (gm *glitchEmulator) AllocateIDs(info *datastore.MiddlewareInfo, keys []datastore.Key) ([]datastore.Key, error) { 44 45 if err := gm.raiseError("AllocateIDs", keys); err != nil { 46 return nil, err 47 } 48 49 return info.Next.AllocateIDs(info, keys) 50 } 51 52 func (gm *glitchEmulator) PutMultiWithoutTx(info *datastore.MiddlewareInfo, keys []datastore.Key, psList []datastore.PropertyList) ([]datastore.Key, error) { 53 54 if err := gm.raiseError("PutMultiWithoutTx", keys); err != nil { 55 return nil, err 56 } 57 58 return info.Next.PutMultiWithoutTx(info, keys, psList) 59 } 60 61 func (gm *glitchEmulator) PutMultiWithTx(info *datastore.MiddlewareInfo, keys []datastore.Key, psList []datastore.PropertyList) ([]datastore.PendingKey, error) { 62 63 if err := gm.raiseError("PutMultiWithTx", keys); err != nil { 64 return nil, err 65 } 66 67 return info.Next.PutMultiWithTx(info, keys, psList) 68 } 69 70 func (gm *glitchEmulator) GetMultiWithoutTx(info *datastore.MiddlewareInfo, keys []datastore.Key, psList []datastore.PropertyList) error { 71 72 if err := gm.raiseError("GetMultiWithoutTx", keys); err != nil { 73 return err 74 } 75 76 return info.Next.GetMultiWithoutTx(info, keys, psList) 77 } 78 79 func (gm *glitchEmulator) GetMultiWithTx(info *datastore.MiddlewareInfo, keys []datastore.Key, psList []datastore.PropertyList) error { 80 81 if err := gm.raiseError("GetMultiWithTx", keys); err != nil { 82 return err 83 } 84 85 return info.Next.GetMultiWithTx(info, keys, psList) 86 } 87 88 func (gm *glitchEmulator) DeleteMultiWithoutTx(info *datastore.MiddlewareInfo, keys []datastore.Key) error { 89 90 if err := gm.raiseError("DeleteMultiWithoutTx", keys); err != nil { 91 return err 92 } 93 94 return info.Next.DeleteMultiWithoutTx(info, keys) 95 } 96 97 func (gm *glitchEmulator) DeleteMultiWithTx(info *datastore.MiddlewareInfo, keys []datastore.Key) error { 98 99 if err := gm.raiseError("DeleteMultiWithTx", keys); err != nil { 100 return err 101 } 102 103 return info.Next.DeleteMultiWithTx(info, keys) 104 } 105 106 func (gm *glitchEmulator) PostCommit(info *datastore.MiddlewareInfo, tx datastore.Transaction, commit datastore.Commit) error { 107 return info.Next.PostCommit(info, tx, commit) 108 } 109 110 func (gm *glitchEmulator) PostRollback(info *datastore.MiddlewareInfo, tx datastore.Transaction) error { 111 return info.Next.PostRollback(info, tx) 112 } 113 114 func (gm *glitchEmulator) Run(info *datastore.MiddlewareInfo, q datastore.Query, qDump *datastore.QueryDump) datastore.Iterator { 115 return info.Next.Run(info, q, qDump) 116 } 117 118 func (gm *glitchEmulator) GetAll(info *datastore.MiddlewareInfo, q datastore.Query, qDump *datastore.QueryDump, psList *[]datastore.PropertyList) ([]datastore.Key, error) { 119 120 if err := gm.raiseError("GetAll", nil); err != nil { 121 return nil, err 122 } 123 124 return info.Next.GetAll(info, q, qDump, psList) 125 } 126 127 func (gm *glitchEmulator) Next(info *datastore.MiddlewareInfo, q datastore.Query, qDump *datastore.QueryDump, iter datastore.Iterator, ps *datastore.PropertyList) (datastore.Key, error) { 128 return info.Next.Next(info, q, qDump, iter, ps) 129 } 130 131 func (gm *glitchEmulator) Count(info *datastore.MiddlewareInfo, q datastore.Query, qDump *datastore.QueryDump) (int, error) { 132 133 if err := gm.raiseError("Count", nil); err != nil { 134 return 0, err 135 } 136 137 return info.Next.Count(info, q, qDump) 138 }