github.com/Jeffail/benthos/v3@v3.65.0/lib/processor/cache_test.go (about) 1 package processor 2 3 import ( 4 "reflect" 5 "testing" 6 7 "github.com/Jeffail/benthos/v3/lib/cache" 8 "github.com/Jeffail/benthos/v3/lib/log" 9 "github.com/Jeffail/benthos/v3/lib/message" 10 "github.com/Jeffail/benthos/v3/lib/metrics" 11 "github.com/Jeffail/benthos/v3/lib/types" 12 ) 13 14 func TestCacheSetDeprecated(t *testing.T) { 15 memCache, err := cache.NewMemory(cache.NewConfig(), nil, log.Noop(), metrics.Noop()) 16 if err != nil { 17 t.Fatal(err) 18 } 19 mgr := &fakeMgr{ 20 caches: map[string]types.Cache{ 21 "foocache": memCache, 22 }, 23 } 24 25 conf := NewConfig() 26 conf.Cache.Key = "${!json(\"key\")}" 27 conf.Cache.Value = "${!json(\"value\")}" 28 conf.Cache.Cache = "foocache" 29 proc, err := NewCache(conf, mgr, log.Noop(), metrics.Noop()) 30 if err != nil { 31 t.Error(err) 32 return 33 } 34 35 input := message.New([][]byte{ 36 []byte(`{"key":"1","value":"foo 1"}`), 37 []byte(`{"key":"2","value":"foo 2"}`), 38 []byte(`{"key":"1","value":"foo 3"}`), 39 }) 40 41 output, res := proc.ProcessMessage(input) 42 if res != nil { 43 t.Fatal(res.Error()) 44 } 45 46 if len(output) != 1 { 47 t.Fatalf("Wrong count of result messages: %v", len(output)) 48 } 49 50 if exp, act := message.GetAllBytes(input), message.GetAllBytes(output[0]); !reflect.DeepEqual(exp, act) { 51 t.Errorf("Wrong result messages: %s != %s", act, exp) 52 } 53 54 actBytes, err := memCache.Get("1") 55 if err != nil { 56 t.Fatal(err) 57 } 58 if exp, act := "foo 3", string(actBytes); exp != act { 59 t.Errorf("Wrong result: %v != %v", act, exp) 60 } 61 62 actBytes, err = memCache.Get("2") 63 if err != nil { 64 t.Fatal(err) 65 } 66 if exp, act := "foo 2", string(actBytes); exp != act { 67 t.Errorf("Wrong result: %v != %v", act, exp) 68 } 69 } 70 71 func TestCacheSet(t *testing.T) { 72 memCache, err := cache.NewMemory(cache.NewConfig(), nil, log.Noop(), metrics.Noop()) 73 if err != nil { 74 t.Fatal(err) 75 } 76 mgr := &fakeMgr{ 77 caches: map[string]types.Cache{ 78 "foocache": memCache, 79 }, 80 } 81 82 conf := NewConfig() 83 conf.Cache.Key = "${!json(\"key\")}" 84 conf.Cache.Value = "${!json(\"value\")}" 85 conf.Cache.Resource = "foocache" 86 proc, err := NewCache(conf, mgr, log.Noop(), metrics.Noop()) 87 if err != nil { 88 t.Error(err) 89 return 90 } 91 92 input := message.New([][]byte{ 93 []byte(`{"key":"1","value":"foo 1"}`), 94 []byte(`{"key":"2","value":"foo 2"}`), 95 []byte(`{"key":"1","value":"foo 3"}`), 96 }) 97 98 output, res := proc.ProcessMessage(input) 99 if res != nil { 100 t.Fatal(res.Error()) 101 } 102 103 if len(output) != 1 { 104 t.Fatalf("Wrong count of result messages: %v", len(output)) 105 } 106 107 if exp, act := message.GetAllBytes(input), message.GetAllBytes(output[0]); !reflect.DeepEqual(exp, act) { 108 t.Errorf("Wrong result messages: %s != %s", act, exp) 109 } 110 111 actBytes, err := memCache.Get("1") 112 if err != nil { 113 t.Fatal(err) 114 } 115 if exp, act := "foo 3", string(actBytes); exp != act { 116 t.Errorf("Wrong result: %v != %v", act, exp) 117 } 118 119 actBytes, err = memCache.Get("2") 120 if err != nil { 121 t.Fatal(err) 122 } 123 if exp, act := "foo 2", string(actBytes); exp != act { 124 t.Errorf("Wrong result: %v != %v", act, exp) 125 } 126 } 127 128 func TestCacheSetParts(t *testing.T) { 129 memCache, err := cache.NewMemory(cache.NewConfig(), nil, log.Noop(), metrics.Noop()) 130 if err != nil { 131 t.Fatal(err) 132 } 133 mgr := &fakeMgr{ 134 caches: map[string]types.Cache{ 135 "foocache": memCache, 136 }, 137 } 138 139 conf := NewConfig() 140 conf.Cache.Key = "${!json(\"key\")}" 141 conf.Cache.Value = "${!json(\"value\")}" 142 conf.Cache.Resource = "foocache" 143 conf.Cache.Parts = []int{0, 1} 144 proc, err := NewCache(conf, mgr, log.Noop(), metrics.Noop()) 145 if err != nil { 146 t.Error(err) 147 return 148 } 149 150 input := message.New([][]byte{ 151 []byte(`{"key":"1","value":"foo 1"}`), 152 []byte(`{"key":"2","value":"foo 2"}`), 153 []byte(`{"key":"1","value":"foo 3"}`), 154 }) 155 156 output, res := proc.ProcessMessage(input) 157 if res != nil { 158 t.Fatal(res.Error()) 159 } 160 161 if len(output) != 1 { 162 t.Fatalf("Wrong count of result messages: %v", len(output)) 163 } 164 165 if exp, act := message.GetAllBytes(input), message.GetAllBytes(output[0]); !reflect.DeepEqual(exp, act) { 166 t.Errorf("Wrong result messages: %s != %s", act, exp) 167 } 168 169 actBytes, err := memCache.Get("1") 170 if err != nil { 171 t.Fatal(err) 172 } 173 if exp, act := "foo 1", string(actBytes); exp != act { 174 t.Errorf("Wrong result: %v != %v", act, exp) 175 } 176 177 actBytes, err = memCache.Get("2") 178 if err != nil { 179 t.Fatal(err) 180 } 181 if exp, act := "foo 2", string(actBytes); exp != act { 182 t.Errorf("Wrong result: %v != %v", act, exp) 183 } 184 } 185 186 func TestCacheAdd(t *testing.T) { 187 memCache, err := cache.NewMemory(cache.NewConfig(), nil, log.Noop(), metrics.Noop()) 188 if err != nil { 189 t.Fatal(err) 190 } 191 mgr := &fakeMgr{ 192 caches: map[string]types.Cache{ 193 "foocache": memCache, 194 }, 195 } 196 197 conf := NewConfig() 198 conf.Cache.Key = "${!json(\"key\")}" 199 conf.Cache.Value = "${!json(\"value\")}" 200 conf.Cache.Resource = "foocache" 201 conf.Cache.Operator = "add" 202 proc, err := NewCache(conf, mgr, log.Noop(), metrics.Noop()) 203 if err != nil { 204 t.Error(err) 205 return 206 } 207 208 input := message.New([][]byte{ 209 []byte(`{"key":"1","value":"foo 1"}`), 210 []byte(`{"key":"2","value":"foo 2"}`), 211 []byte(`{"key":"1","value":"foo 3"}`), 212 }) 213 214 output, res := proc.ProcessMessage(input) 215 if res != nil { 216 t.Fatal(res.Error()) 217 } 218 219 if len(output) != 1 { 220 t.Fatalf("Wrong count of result messages: %v", len(output)) 221 } 222 223 if exp, act := message.GetAllBytes(input), message.GetAllBytes(output[0]); !reflect.DeepEqual(exp, act) { 224 t.Errorf("Wrong result messages: %s != %s", act, exp) 225 } 226 227 if exp, act := false, HasFailed(output[0].Get(0)); exp != act { 228 t.Errorf("Wrong fail flag: %v != %v", act, exp) 229 } 230 if exp, act := false, HasFailed(output[0].Get(1)); exp != act { 231 t.Errorf("Wrong fail flag: %v != %v", act, exp) 232 } 233 if exp, act := true, HasFailed(output[0].Get(2)); exp != act { 234 t.Errorf("Wrong fail flag: %v != %v", act, exp) 235 } 236 237 actBytes, err := memCache.Get("1") 238 if err != nil { 239 t.Fatal(err) 240 } 241 if exp, act := "foo 1", string(actBytes); exp != act { 242 t.Errorf("Wrong result: %v != %v", act, exp) 243 } 244 245 actBytes, err = memCache.Get("2") 246 if err != nil { 247 t.Fatal(err) 248 } 249 if exp, act := "foo 2", string(actBytes); exp != act { 250 t.Errorf("Wrong result: %v != %v", act, exp) 251 } 252 } 253 254 func TestCacheGet(t *testing.T) { 255 memCache, err := cache.NewMemory(cache.NewConfig(), nil, log.Noop(), metrics.Noop()) 256 if err != nil { 257 t.Fatal(err) 258 } 259 mgr := &fakeMgr{ 260 caches: map[string]types.Cache{ 261 "foocache": memCache, 262 }, 263 } 264 265 memCache.Set("1", []byte("foo 1")) 266 memCache.Set("2", []byte("foo 2")) 267 268 conf := NewConfig() 269 conf.Cache.Key = "${!json(\"key\")}" 270 conf.Cache.Resource = "foocache" 271 conf.Cache.Operator = "get" 272 proc, err := NewCache(conf, mgr, log.Noop(), metrics.Noop()) 273 if err != nil { 274 t.Error(err) 275 return 276 } 277 278 input := message.New([][]byte{ 279 []byte(`{"key":"1"}`), 280 []byte(`{"key":"2"}`), 281 []byte(`{"key":"3"}`), 282 }) 283 expParts := [][]byte{ 284 []byte(`foo 1`), 285 []byte(`foo 2`), 286 []byte(`{"key":"3"}`), 287 } 288 289 output, res := proc.ProcessMessage(input) 290 if res != nil { 291 t.Fatal(res.Error()) 292 } 293 294 if len(output) != 1 { 295 t.Fatalf("Wrong count of result messages: %v", len(output)) 296 } 297 298 if exp, act := expParts, message.GetAllBytes(output[0]); !reflect.DeepEqual(exp, act) { 299 t.Errorf("Wrong result messages: %s != %s", act, exp) 300 } 301 302 if exp, act := false, HasFailed(output[0].Get(0)); exp != act { 303 t.Errorf("Wrong fail flag: %v != %v", act, exp) 304 } 305 if exp, act := false, HasFailed(output[0].Get(1)); exp != act { 306 t.Errorf("Wrong fail flag: %v != %v", act, exp) 307 } 308 if exp, act := true, HasFailed(output[0].Get(2)); exp != act { 309 t.Errorf("Wrong fail flag: %v != %v", act, exp) 310 } 311 } 312 313 func TestCacheDelete(t *testing.T) { 314 memCache, err := cache.NewMemory(cache.NewConfig(), nil, log.Noop(), metrics.Noop()) 315 if err != nil { 316 t.Fatal(err) 317 } 318 mgr := &fakeMgr{ 319 caches: map[string]types.Cache{ 320 "foocache": memCache, 321 }, 322 } 323 324 memCache.Set("1", []byte("foo 1")) 325 memCache.Set("2", []byte("foo 2")) 326 memCache.Set("3", []byte("foo 3")) 327 328 conf := NewConfig() 329 conf.Cache.Key = "${!json(\"key\")}" 330 conf.Cache.Resource = "foocache" 331 conf.Cache.Operator = "delete" 332 proc, err := NewCache(conf, mgr, log.Noop(), metrics.Noop()) 333 if err != nil { 334 t.Error(err) 335 return 336 } 337 338 input := message.New([][]byte{ 339 []byte(`{"key":"1"}`), 340 []byte(`{"key":"3"}`), 341 []byte(`{"key":"4"}`), 342 }) 343 344 output, res := proc.ProcessMessage(input) 345 if res != nil { 346 t.Fatal(res.Error()) 347 } 348 349 if len(output) != 1 { 350 t.Fatalf("Wrong count of result messages: %v", len(output)) 351 } 352 353 if exp, act := message.GetAllBytes(input), message.GetAllBytes(output[0]); !reflect.DeepEqual(exp, act) { 354 t.Errorf("Wrong result messages: %s != %s", act, exp) 355 } 356 357 if exp, act := false, HasFailed(output[0].Get(0)); exp != act { 358 t.Errorf("Wrong fail flag: %v != %v", act, exp) 359 } 360 if exp, act := false, HasFailed(output[0].Get(1)); exp != act { 361 t.Errorf("Wrong fail flag: %v != %v", act, exp) 362 } 363 if exp, act := false, HasFailed(output[0].Get(2)); exp != act { 364 t.Errorf("Wrong fail flag: %v != %v", act, exp) 365 } 366 367 _, err = memCache.Get("1") 368 if err != types.ErrKeyNotFound { 369 t.Errorf("Wrong result: %v != %v", err, types.ErrKeyNotFound) 370 } 371 372 actBytes, err := memCache.Get("2") 373 if err != nil { 374 t.Fatal(err) 375 } 376 if exp, act := "foo 2", string(actBytes); exp != act { 377 t.Errorf("Wrong result: %v != %v", act, exp) 378 } 379 380 _, err = memCache.Get("3") 381 if err != types.ErrKeyNotFound { 382 t.Errorf("Wrong result: %v != %v", err, types.ErrKeyNotFound) 383 } 384 }