github.com/GuanceCloud/cliutils@v1.1.21/pipeline/ptinput/funcs/fn_cache_test.go (about) 1 package funcs 2 3 import ( 4 "testing" 5 "time" 6 7 "github.com/GuanceCloud/cliutils/pipeline/ptinput" 8 "github.com/GuanceCloud/cliutils/pipeline/ptinput/plcache" 9 "github.com/GuanceCloud/cliutils/point" 10 tu "github.com/GuanceCloud/cliutils/testutil" 11 ) 12 13 func TestCache(t *testing.T) { 14 cases := []struct { 15 name, pl, in string 16 expected interface{} 17 fail bool 18 outkey string 19 }{ 20 { 21 name: "test_set_get_with_exp", 22 pl: `cache_set("a", "123", 5) 23 a = cache_get("a") 24 add_key(abc, a)`, 25 in: `[]`, 26 expected: "123", 27 outkey: "abc", 28 }, 29 { 30 name: "test_set_get_without_exp", 31 pl: `a = cache_set("a", "123") 32 a = cache_get("a") 33 add_key(abc, a)`, 34 in: `[]`, 35 expected: "123", 36 outkey: "abc", 37 }, 38 } 39 40 for idx, tc := range cases { 41 t.Run(tc.name, func(t *testing.T) { 42 runner, err := NewTestingRunner(tc.pl) 43 if err != nil { 44 if tc.fail { 45 t.Logf("[%d]expect error: %s", idx, err) 46 } else { 47 t.Errorf("[%d] failed: %s", idx, err) 48 } 49 return 50 } 51 cache, _ := plcache.NewCache(time.Second, 100) 52 pt := ptinput.NewPlPoint( 53 point.Logging, "test", nil, map[string]any{"message": tc.in}, time.Now()) 54 pt.SetCache(cache) 55 errR := runScript(runner, pt) 56 57 if errR != nil { 58 t.Fatal(errR.Error()) 59 } 60 61 cache.Stop() 62 63 v, _, _ := pt.Get(tc.outkey) 64 // tu.Equals(t, nil, err) 65 tu.Equals(t, tc.expected, v) 66 67 t.Logf("[%d] PASS", idx) 68 }) 69 } 70 }