github.com/alibaba/ilogtail/pkg@v0.0.0-20250526110833-c53b480d046c/helper/dumper_test.go (about)

     1  // Copyright 2023 iLogtail Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package helper
    16  
    17  import (
    18  	"bytes"
    19  	"encoding/binary"
    20  	"encoding/json"
    21  	"fmt"
    22  	"os"
    23  	"path"
    24  	"strconv"
    25  	"testing"
    26  	"time"
    27  
    28  	"github.com/alibaba/ilogtail/pkg/config"
    29  
    30  	"github.com/stretchr/testify/require"
    31  )
    32  
    33  func TestServiceHTTP_doDumpFile(t *testing.T) {
    34  	_, err := os.Stat(path.Join(config.LoongcollectorGlobalConfig.LoongCollectorDebugDir, "dump"))
    35  	if err == nil {
    36  		files, findErr := GetFileListByPrefix(path.Join(config.LoongcollectorGlobalConfig.LoongCollectorDebugDir, "dump"), "custom", true, 0)
    37  		require.NoError(t, findErr)
    38  		for _, file := range files {
    39  			_ = os.Remove(file)
    40  
    41  		}
    42  	}
    43  
    44  	var ch chan *DumpData
    45  
    46  	insertFun := func(num int, start int) {
    47  		for i := start; i < start+num; i++ {
    48  			m := map[string][]string{
    49  				"header": {strconv.Itoa(i)},
    50  			}
    51  			ch <- &DumpData{
    52  				Req: DumpDataReq{
    53  					Body:   []byte(fmt.Sprintf("body_%d", i)),
    54  					URL:    fmt.Sprintf("url_%d", i),
    55  					Header: m,
    56  				},
    57  			}
    58  
    59  		}
    60  	}
    61  	readFunc := func(file string, expectLen int) {
    62  		data, rerr := os.ReadFile(file)
    63  		require.NoError(t, rerr)
    64  		offset := 0
    65  		num := 0
    66  		for {
    67  			if offset == len(data) {
    68  				break
    69  			}
    70  			var length uint32
    71  			buffer := bytes.NewBuffer(data[offset:])
    72  			require.NoError(t, binary.Read(buffer, binary.BigEndian, &length))
    73  
    74  			data := data[offset+4 : offset+4+int(length)] //nolint: govet
    75  			var d DumpData
    76  			require.NoError(t, json.Unmarshal(data, &d))
    77  
    78  			require.Equal(t, fmt.Sprintf("url_%d", num), d.Req.URL)
    79  			require.Equal(t, fmt.Sprintf("body_%d", num), string(d.Req.Body))
    80  			require.Equal(t, len(d.Req.Header), 1)
    81  			require.Equal(t, strconv.Itoa(num), d.Req.Header["header"][0])
    82  			offset = offset + 4 + int(length)
    83  			num++
    84  		}
    85  		require.Equal(t, num, expectLen)
    86  	}
    87  
    88  	// test dump and read
    89  	s := NewDumper("custom", 3)
    90  	s.Init()
    91  	ch = s.InputChannel()
    92  	s.Start()
    93  	s.Begin(func() {})
    94  	insertFun(100, 0)
    95  	require.NoError(t, s.End(time.Second*2, 100))
    96  	s.Close()
    97  	readFunc(s.dumpDataKeepFiles[len(s.dumpDataKeepFiles)-1], 100)
    98  
    99  	// append
   100  	s2 := NewDumper("custom", 3)
   101  	s2.Init()
   102  	ch = s2.InputChannel()
   103  	s2.Start()
   104  	s2.Begin(func() {})
   105  	insertFun(100, 100)
   106  	require.NoError(t, s2.End(time.Second*2, 100))
   107  	s2.Close()
   108  	readFunc(s.dumpDataKeepFiles[len(s.dumpDataKeepFiles)-1], 200)
   109  }