Skip to content

Commit

Permalink
[receiver/purefa] Add more specific test for the validation of each s…
Browse files Browse the repository at this point in the history
…craper endpoint
  • Loading branch information
Diego Amaral authored and dgoscn committed Jun 11, 2023
1 parent 6a251e1 commit c4c8616
Showing 1 changed file with 29 additions and 13 deletions.
42 changes: 29 additions & 13 deletions receiver/purefareceiver/receiver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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",
Expand All @@ -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) {
Expand Down

0 comments on commit c4c8616

Please sign in to comment.