github.com/yandex/pandora@v0.5.32/core/aggregator/netsample/phout_test.go (about)

     1  package netsample
     2  
     3  import (
     4  	"context"
     5  	"strings"
     6  	"testing"
     7  	"time"
     8  
     9  	"github.com/spf13/afero"
    10  	"github.com/stretchr/testify/assert"
    11  	"github.com/stretchr/testify/require"
    12  	"github.com/yandex/pandora/core"
    13  )
    14  
    15  func TestPhout(t *testing.T) {
    16  	const fileName = "out.txt"
    17  
    18  	tests := []struct {
    19  		name      string
    20  		resetConf func(cfg *PhoutConfig)
    21  		reportCnt int
    22  		want      string
    23  	}{
    24  		{
    25  			name:      "no id by default",
    26  			reportCnt: 2,
    27  			want:      strings.Repeat(testSampleNoIDPhout+"\n", 2),
    28  		},
    29  		{
    30  			name: "id option set",
    31  			resetConf: func(cfg *PhoutConfig) {
    32  				cfg.ID = true
    33  			},
    34  			reportCnt: 1,
    35  			want:      testSamplePhout + "\n",
    36  		},
    37  	}
    38  	for _, tt := range tests {
    39  		t.Run(tt.name, func(t *testing.T) {
    40  			fs := afero.NewMemMapFs()
    41  			conf := DefaultPhoutConfig()
    42  			conf.Destination = fileName
    43  			if tt.resetConf != nil {
    44  				tt.resetConf(&conf)
    45  			}
    46  			ctx, cancel := context.WithCancel(context.Background())
    47  
    48  			var err error
    49  			testee, err := NewPhout(fs, conf)
    50  			require.NoError(t, err)
    51  			runErr := make(chan error)
    52  			go func() {
    53  				runErr <- testee.Run(ctx, core.AggregatorDeps{})
    54  			}()
    55  
    56  			for i := 0; i < tt.reportCnt; i++ {
    57  				testee.Report(newTestSample())
    58  			}
    59  			cancel()
    60  			err = <-runErr
    61  			assert.NoError(t, err)
    62  
    63  			data, err := afero.ReadFile(fs, fileName)
    64  			require.NoError(t, err)
    65  
    66  			assert.Equal(t, tt.want, string(data))
    67  		})
    68  	}
    69  }
    70  
    71  const (
    72  	testSamplePhout     = "1484660999.002	tag1|tag2#42	333333	0	0	0	0	0	0	0	13	999"
    73  	testSampleNoIDPhout = "1484660999.002	tag1|tag2	333333	0	0	0	0	0	0	0	13	999"
    74  )
    75  
    76  func newTestSample() *Sample {
    77  	s := &Sample{}
    78  	s.timeStamp = time.Unix(1484660999, 002*1000000)
    79  	s.SetID(42)
    80  	s.AddTag("tag1|tag2")
    81  	s.setDuration(keyRTTMicro, time.Second/3)
    82  	s.set(keyErrno, 13)
    83  	s.set(keyProtoCode, ProtoCodeError)
    84  	return s
    85  }