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

     1  package aggregator
     2  
     3  import (
     4  	"context"
     5  	"encoding/json"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  	"github.com/stretchr/testify/require"
    10  	"github.com/yandex/pandora/core"
    11  	"github.com/yandex/pandora/core/datasink"
    12  	"go.uber.org/zap"
    13  )
    14  
    15  type jsonTestData struct {
    16  	String string `json:"string"`
    17  	Int    int    `json:"int"`
    18  }
    19  
    20  func TestNewJSONLinesAggregator(t *testing.T) {
    21  	samples := []jsonTestData{
    22  		{"A", 1},
    23  		{"B", 2},
    24  		{"C", 3},
    25  	}
    26  
    27  	conf := DefaultJSONLinesAggregatorConfig()
    28  	sink := &datasink.Buffer{}
    29  	conf.Sink = sink
    30  	testee := NewJSONLinesAggregator(conf)
    31  	ctx, cancel := context.WithCancel(context.Background())
    32  
    33  	runErr := make(chan error)
    34  	go func() {
    35  		runErr <- testee.Run(ctx, core.AggregatorDeps{Log: zap.L()})
    36  	}()
    37  
    38  	for _, sample := range samples {
    39  		testee.Report(sample)
    40  	}
    41  	cancel()
    42  	err := <-runErr
    43  	require.NoError(t, err)
    44  
    45  	for _, expected := range samples {
    46  		var actual jsonTestData
    47  		line, err := sink.ReadBytes('\n')
    48  		require.NoError(t, err)
    49  		err = json.Unmarshal(line, &actual)
    50  		require.NoError(t, err)
    51  		assert.Equal(t, expected, actual)
    52  	}
    53  
    54  	assert.Zero(t, sink.Len())
    55  }