gitee.com/woood2/luca@v1.0.4/internal/cache/local_test.go (about) 1 package cache 2 3 import ( 4 "testing" 5 "time" 6 ) 7 8 func TestLocalMiss(t *testing.T) { 9 c := Local() 10 var tests = []struct { 11 in string // input 12 }{ 13 {"县"}, 14 {"市"}, 15 {"省"}, 16 } 17 for _, tt := range tests { 18 var v interface{} 19 if e := c.Get(tt.in, &v); e != c.Miss() { 20 t.Errorf("expected e=cache.Miss; actual e=%v", e) 21 } 22 } 23 } 24 25 func TestLocalExpire(t *testing.T) { 26 c := Local() 27 var tests = []struct { 28 in string // input 29 }{ 30 {"孺子牛"}, 31 {"拓荒牛"}, 32 {"老黄牛"}, 33 } 34 for _, tt := range tests { 35 p := &person{ 36 Name: tt.in, 37 } 38 c.Set(tt.in, p, 1*time.Second) 39 } 40 for _, tt := range tests { 41 var v interface{} 42 if e := c.Get(tt.in, &v); e != nil { 43 t.Errorf("expected e=nil; actual e=%v", e) 44 } 45 } 46 time.Sleep(2 * time.Second) 47 for _, tt := range tests { 48 var v interface{} 49 if e := c.Get(tt.in, &v); e != c.Miss() { 50 t.Errorf("expected e=cache.Miss; actual e=%v", e) 51 } 52 } 53 } 54 55 func TestLocalNormal(t *testing.T) { 56 c := Local() 57 var tests = []struct { 58 in string // input 59 expected string // expected result 60 }{ 61 {"wmd", "wmd"}, 62 {"吴孟达", "吴孟达"}, 63 {"周星驰", "周星驰"}, 64 } 65 for _, tt := range tests { 66 p1 := &person{ 67 Name: tt.in, 68 } 69 c.Set(tt.in, p1, 1*time.Second) 70 var p *person 71 if e := c.Get(tt.in, &p); e != nil { 72 t.Errorf("expected e=nil; actual e=%v", e) 73 } else { 74 if p.Name != tt.expected { 75 t.Errorf("set %s; get %s; expected %s", tt.in, p.Name, tt.expected) 76 } 77 } 78 } 79 } 80 81 func TestLocalTypes(t *testing.T) { 82 c := Local() 83 84 sl := []string{"wa", "ha", "ha"} 85 m := make(map[string]int) 86 m["name"] = 120 87 m["age"] = 23 88 var p1 *person //nil 89 p2 := &person{ 90 Name: "2d", 91 Age: 30, 92 } 93 94 var tests = []struct { 95 k string 96 v interface{} 97 }{ 98 {"rabbit", "rabbit"}, 99 {"", ""}, 100 101 {"911", 911}, 102 103 {"true", true}, 104 {"false", false}, 105 106 {"slice", sl}, 107 {"map", m}, 108 {"p1", p1}, 109 {"p2", p2}, 110 {"nil", nil}, 111 } 112 for _, tt := range tests { 113 c.Set(tt.k, tt.v, 1*time.Second) 114 switch tt.k { 115 case "rabbit": 116 s := "" 117 if e := c.Get(tt.k, &s); e != nil { 118 t.Errorf("expected e=nil; actual e=%v", e) 119 } 120 if s != tt.v { 121 t.Errorf("expected %v; actual %v", tt.v, s) 122 } 123 case "": 124 s := "hi" 125 if e := c.Get(tt.k, &s); e != nil { 126 t.Errorf("expected e=nil; actual e=%v", e) 127 } 128 if s != tt.v { 129 t.Errorf("expected %v; actual %v", tt.v, s) 130 } 131 case "911": 132 i := 0 133 if e := c.Get(tt.k, &i); e != nil { 134 t.Errorf("expected e=nil; actual e=%v", e) 135 } 136 if i != tt.v { 137 t.Errorf("expected %v; actual %v", tt.v, i) 138 } 139 case "true": 140 b := false 141 if e := c.Get(tt.k, &b); e != nil { 142 t.Errorf("expected e=nil; actual e=%v", e) 143 } 144 if b != tt.v { 145 t.Errorf("expected %v; actual %v", tt.v, b) 146 } 147 case "false": 148 b := true 149 if e := c.Get(tt.k, &b); e != nil { 150 t.Errorf("expected e=nil; actual e=%v", e) 151 } 152 if b != tt.v { 153 t.Errorf("expected %v; actual %v", tt.v, b) 154 } 155 case "slice": 156 var sl2 []string 157 if e := c.Get(tt.k, &sl2); e != nil { 158 t.Errorf("expected e=nil; actual e=%v", e) 159 } 160 if len(sl2) != len(sl) { 161 t.Errorf("expected %v; actual %v", sl, sl2) 162 } 163 case "map": 164 var m2 map[string]int 165 if e := c.Get(tt.k, &m2); e != nil { 166 t.Errorf("expected e=nil; actual e=%v", e) 167 } 168 if m2["name"] != m["name"] { 169 t.Errorf("expected %v; actual %v", m, m2) 170 } 171 case "p1": 172 var p12 *person 173 if e := c.Get(tt.k, &p12); e != nil { 174 t.Errorf("expected e=nil; actual e=%v", e) 175 } 176 if p12 != nil { 177 t.Errorf("expected nil; actual %v", p12) 178 } 179 case "p2": 180 var p22 *person 181 if e := c.Get(tt.k, &p22); e != nil { 182 t.Errorf("expected e=nil; actual e=%v", e) 183 } 184 if p22.Name != p2.Name { 185 t.Errorf("expected %v; actual %v", p2, p22) 186 } 187 case "nil": 188 var kong interface{} 189 if e := c.Get(tt.k, &kong); e != nil { 190 t.Errorf("expected e=nil; actual e=%v", e) 191 } 192 if kong != nil { 193 t.Errorf("expected nil; actual %v", kong) 194 } 195 } 196 } 197 }