github.com/GuanceCloud/cliutils@v1.1.21/pipeline/ptinput/funcs/fn_pt_window_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/ptwindow"
     9  	"github.com/GuanceCloud/cliutils/point"
    10  	"github.com/stretchr/testify/assert"
    11  )
    12  
    13  type testCase struct {
    14  	name, pl string
    15  	in       []string
    16  	expected []string
    17  
    18  	commTags []string
    19  	tagsVal  [][]string
    20  	outkey   string
    21  }
    22  
    23  func TestPtWindow(t *testing.T) {
    24  	cases := []testCase{
    25  		{
    26  			name: "1",
    27  			pl: `point_window(3,3)
    28  			drop()
    29  			if _ == "4" {
    30  				window_hit()
    31  			}
    32  			`,
    33  			in: []string{
    34  				"1", "2", "3", "4", "5", "6",
    35  			},
    36  			commTags: []string{"host", "filepath"},
    37  			tagsVal: [][]string{
    38  				{"a", "b"}, {"a", "b"},
    39  				{"a", "b"}, {"a", "b"},
    40  				{"a", "b"}, {"a", "b"},
    41  			},
    42  			expected: []string{
    43  				"1", "2", "3", "4", "5", "6",
    44  			},
    45  			outkey: "message",
    46  		},
    47  		{
    48  			name: "2",
    49  			pl: `point_window(3,3)
    50  			point_window(2,2)
    51  			if _ == "4" {
    52  				window_hit()
    53  			}
    54  			if _ != "6" {
    55  				drop()
    56  			}
    57  			`,
    58  			in: []string{
    59  				"1", "2", "3", "xx", "4", "5", "6", "7",
    60  			},
    61  			commTags: []string{"host", "filepath"},
    62  			tagsVal: [][]string{
    63  				{"a", "b"}, {"a", "b"},
    64  				{"a", "b"}, {"a", "xx"},
    65  				{"a", "b"}, {"a", "b"},
    66  				{"a", "b"}, {"a", "b"},
    67  			},
    68  			expected: []string{
    69  				"1", "2", "3", "4", "5",
    70  			},
    71  			outkey: "message",
    72  		},
    73  		{
    74  			name: "3",
    75  			pl: `point_window(3,3, ["host", "filepath"])
    76  			if _ == "4" {
    77  				window_hit()
    78  			}
    79  			if _ != "3" {
    80  				drop()
    81  			}
    82  			`,
    83  			in: []string{
    84  				"1", "2", "3", "4", "5", "6", "7",
    85  			},
    86  			commTags: []string{"host", "filepath"},
    87  			tagsVal: [][]string{
    88  				{"a", "b"}, {"a", "b"},
    89  				{"a", "b"}, {"a", "b"},
    90  				{"a", "b"}, {"a", "b"},
    91  				{"a", "b"},
    92  			},
    93  			expected: []string{
    94  				"1", "2", "4", "5", "6",
    95  			},
    96  			outkey: "message",
    97  		},
    98  		{
    99  			name: "4",
   100  			pl: `point_window(3,3)
   101  			if _ == "4" {
   102  				window_hit()
   103  			}
   104  			if _ != "6" {
   105  				drop()
   106  			}
   107  			if _ == "7" {
   108  				window_hit()
   109  			}
   110  			`,
   111  			in: []string{
   112  				"1", "2", "3", "4", "5", "6", "7",
   113  			},
   114  			commTags: []string{"host", "filepath"},
   115  			tagsVal: [][]string{
   116  				{"a", "b"}, {"a", "b"},
   117  				{"a", "b"}, {"a", "b"},
   118  				{"a", "b"}, {"a", "b"},
   119  				{"a", "b"},
   120  			},
   121  			expected: []string{
   122  				"1", "2", "3", "4", "5", "7",
   123  			},
   124  			outkey: "message",
   125  		},
   126  		{
   127  			name: "5",
   128  			pl: `point_window(3,3)
   129  			drop()
   130  			if _ == "4" {
   131  				window_hit()
   132  			}
   133  			`,
   134  			in: []string{
   135  				"0", "1", "2", "3", "4", "5", "6",
   136  			},
   137  			commTags: []string{"host", "filepath"},
   138  			tagsVal: [][]string{
   139  				{"a", "b"}, {"a", "b"},
   140  				{"a", "b"}, {"a", "b"},
   141  				{"a", "b"}, {"a", "b"},
   142  				{"a", "b"},
   143  			},
   144  			expected: []string{
   145  				"3", "1", "2", "4", "5", "6",
   146  			},
   147  			outkey: "message",
   148  		},
   149  	}
   150  
   151  	for _, c := range cases {
   152  		t.Run(c.name, func(t *testing.T) {
   153  			ptPool := ptwindow.NewManager()
   154  			runner, err := NewTestingRunner(c.pl)
   155  			assert.NoError(t, err)
   156  			r := []string{}
   157  			for i, x := range c.in {
   158  				tags := map[string]string{}
   159  				for j, v := range c.commTags {
   160  					tags[v] = c.tagsVal[i][j]
   161  				}
   162  				pt := ptinput.NewPlPoint(
   163  					point.Logging, "test", tags, map[string]any{"message": x}, time.Now())
   164  				pt.SetPtWinPool(ptPool)
   165  				errR := runScript(runner, pt)
   166  				if errR != nil {
   167  					t.Fatal(errR)
   168  				}
   169  
   170  				pts := pt.CallbackPtWinMove()
   171  				for _, pt := range pts {
   172  					val := pt.Get("message")
   173  					r = append(r, val.(string))
   174  				}
   175  			}
   176  
   177  			assert.Equal(t, c.expected, r)
   178  		})
   179  	}
   180  }