github.com/GuanceCloud/cliutils@v1.1.21/pipeline/offload/offload_test.go (about)

     1  // Unless explicitly stated otherwise all files in this repository are licensed
     2  // under the MIT License.
     3  // This product includes software developed at Guance Cloud (https://www.guance.com/).
     4  // Copyright 2021-present Guance, Inc.
     5  
     6  package offload
     7  
     8  import (
     9  	"context"
    10  	"strconv"
    11  	"testing"
    12  	"time"
    13  
    14  	"github.com/GuanceCloud/cliutils/point"
    15  )
    16  
    17  type sender4test struct {
    18  	d []*point.Point
    19  }
    20  
    21  func (sender *sender4test) Send(s uint64, cat point.Category, pts []*point.Point) error {
    22  	sender.d = append(sender.d, pts...)
    23  	return nil
    24  }
    25  
    26  func TestWkr(t *testing.T) {
    27  	td := func(n []int) [][]*point.Point {
    28  		r := make([][]*point.Point, len(n))
    29  		c := 0
    30  		for idx, v := range n {
    31  			r[idx] = make([]*point.Point, v)
    32  			for i := 0; i < v; i++ {
    33  				kvs := point.NewTags(nil)
    34  				kvs = append(kvs, point.NewKVs(map[string]interface{}{
    35  					"n": c + i,
    36  				})...)
    37  				r[idx][i] = point.NewPointV2(strconv.FormatInt(int64(c+i), 10), kvs, point.CommonLoggingOptions()...)
    38  			}
    39  			c += v
    40  		}
    41  		return r
    42  	}
    43  
    44  	cases := []struct {
    45  		n []int
    46  	}{
    47  		{
    48  			n: []int{10, 20, 40},
    49  		},
    50  		{
    51  			n: []int{0, 10, ptsBuf, 20, 40, ptsBuf, 100},
    52  		},
    53  		{
    54  			n: []int{ptsBuf, ptsBuf * 2, ptsBuf*3 - 3},
    55  		},
    56  	}
    57  
    58  	for _, v := range cases {
    59  		t.Run("t1", func(t *testing.T) {
    60  			ptsLi := td(v.n)
    61  			s := &sender4test{d: []*point.Point{}}
    62  			wkr := OffloadWorker{
    63  				ch:       newDataChan(),
    64  				stopChan: make(chan struct{}),
    65  				sender:   s,
    66  			}
    67  
    68  			ctx := context.Background()
    69  
    70  			go func() {
    71  				time.Sleep(time.Millisecond * 20)
    72  				for _, pts := range ptsLi {
    73  					wkr.Send(point.Logging, pts)
    74  				}
    75  				time.Sleep(time.Millisecond * 50)
    76  				wkr.Stop()
    77  			}()
    78  
    79  			wkr.Customer(ctx, point.Logging)
    80  
    81  			total := 0
    82  			for _, v := range v.n {
    83  				total += v
    84  			}
    85  			if len(s.d) != total {
    86  				t.Fatal(len(s.d), total)
    87  			}
    88  
    89  			for i := 0; i < total; i++ {
    90  				if strconv.FormatInt(int64(i), 10) != s.d[i].Name() {
    91  					t.Fatal(strconv.FormatInt(int64(i), 10), s.d[i].Name())
    92  				}
    93  			}
    94  		})
    95  	}
    96  }
    97  
    98  func TestDataKitHTTP(t *testing.T) {
    99  	_, err := NewOffloader(&OffloadConfig{
   100  		Receiver:  DKRcv,
   101  		Addresses: []string{"aaa"},
   102  	})
   103  	if err != nil {
   104  		t.Fatal(err)
   105  	}
   106  
   107  	_, err = NewOffloader(&OffloadConfig{
   108  		Receiver:  "abc",
   109  		Addresses: []string{"aaa"},
   110  	})
   111  
   112  	if err == nil {
   113  		t.Fatal("err == nil")
   114  	} else {
   115  		t.Log(err)
   116  	}
   117  }