From c4c861647cc409db6ed4301923e3c359aaeaf95a Mon Sep 17 00:00:00 2001 From: Diego Amaral Date: Sun, 11 Jun 2023 00:27:10 -0300 Subject: [PATCH] [receiver/purefa] Add more specific test for the validation of each scraper endpoint --- receiver/purefareceiver/receiver_test.go | 42 ++++++++++++++++-------- 1 file changed, 29 insertions(+), 13 deletions(-) diff --git a/receiver/purefareceiver/receiver_test.go b/receiver/purefareceiver/receiver_test.go index 6488d592245c..71d595f8a2d4 100644 --- a/receiver/purefareceiver/receiver_test.go +++ b/receiver/purefareceiver/receiver_test.go @@ -5,8 +5,15 @@ package purefareceiver // import "github.com/open-telemetry/opentelemetry-collec import ( "context" + "fmt" + "net/http" + "net/http/httptest" + "os" + "sync" "testing" + "time" + "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/purefareceiver/internal" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "go.opentelemetry.io/collector/component/componenttest" @@ -16,27 +23,31 @@ import ( func TestReceiverArray(t *testing.T) { // prepare - wg := &sync.WaitGroup() - + wg := &sync.WaitGroup{} + once := &sync.Once{} ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { var data []byte var err error - - once.Do(func(){ - data, err := os.ReadFile("testdata/array.txt") - require.NoError(t, err) + + once.Do(func() { + data, err = os.ReadFile("testdata/array.txt") + require.NoError(t, err) wg.Done() }) + fmt.Println("Received request:", r.URL) + _, err = w.Write(data) require.NoError(t, err) + fmt.Println(string(data)) + })) defer ts.Close() - + cfg, ok := createDefaultConfig().(*Config) require.True(t, ok) - + cfg.Endpoint = ts.URL cfg.Array = []internal.ScraperConfig{{ Address: "array01", @@ -46,18 +57,23 @@ func TestReceiverArray(t *testing.T) { Array: 10 * time.Millisecond, }, } - + sink := &consumertest.MetricsSink{} recv := newReceiver(cfg, receivertest.NewNopCreateSettings(), sink) - wg.Add(1) - + wg.Add(1) + // test - rr := recv.Start(context.Background(), componenttest.NewNopHost()) + err := recv.Start(context.Background(), componenttest.NewNopHost()) wg.Wait() // verify assert.NoError(t, err) - assert.Greater(t, len(sink.AllMetrics()), 0, "expected to have received more than 0 metrics") + //assert.Greater(t, len(sink.AllMetrics()), 0, "expected to have received more than 0 metrics") + //require.Equal(t, len(sink.AllMetrics()), 0) + assert.Eventually(t, func() bool { + return len(sink.AllMetrics()) == 1 + }, 10*time.Second, 10*time.Millisecond) + } func TestStart(t *testing.T) {