// TODO figure out why this test is flaky, should be > 2, but change to >= 2 so it passes
func TestBufferedForwarderBasicEvent(t *testing.T) {
	ctx := context.Background()
	config := Config{
		BufferSize:         210,
		MaxTotalDatapoints: 1000,
		MaxTotalEvents:     1000,
		NumDrainingThreads: 1,
		MaxDrainSize:       1000,
	}
	sendTo := dptest.NewBasicSink()
	bf := NewBufferedForwarder(ctx, config, sendTo)
	defer bf.Close()
	assert.NoError(t, bf.AddEvents(ctx, []*event.Event{}))
	time.Sleep(time.Millisecond)
	for i := 0; i < 100; i++ {
		datas := []*event.Event{
			dptest.E(),
			dptest.E(),
		}
		assert.NoError(t, bf.AddEvents(ctx, datas))
		if i == 0 {
			seen := <-sendTo.EventsChan
			assert.Equal(t, 2, len(seen), "The first send should eventually come back with the first two events")
		}
	}
	// Wait for more events
	seen := <-sendTo.EventsChan
	assert.True(t, len(seen) >= 2, fmt.Sprintf("Events should buffer: %d", len(seen)))
	assert.Equal(t, numStats, len(bf.Stats(map[string]string{})), "Checking returned stats size")
}
Example #2
0
func TestNew(t *testing.T) {
	ctx := context.Background()
	sendTo1 := dptest.NewBasicSink()
	sendTo2 := dptest.NewBasicSink()
	demux := New([]dpsink.Sink{sendTo1, sendTo2})

	pts := []*datapoint.Datapoint{dptest.DP(), dptest.DP()}
	es := []*event.Event{dptest.E(), dptest.E()}
	ctx2, _ := context.WithTimeout(ctx, time.Millisecond)
	assert.Error(t, demux.AddDatapoints(ctx2, pts))
	assert.Error(t, demux.AddEvents(ctx2, es))
	assert.NoError(t, demux.AddDatapoints(context.Background(), []*datapoint.Datapoint{}))
	assert.NoError(t, demux.AddEvents(context.Background(), []*event.Event{}))
}
Example #3
0
func TestFilenameForwarderBadWrite(t *testing.T) {
	ctx := context.Background()
	fileObj, _ := ioutil.TempFile("", "gotest")
	defer os.Remove(fileObj.Name())
	conf := &config.ForwardTo{
		Filename: workarounds.GolangDoesnotAllowPointerToStringLiteral(fileObj.Name()),
	}
	f, _ := ForwarderLoader(conf)
	f.writeString = func(f *os.File, s string) (ret int, err error) {
		return 0, errors.New("nope")
	}
	assert.Error(t, f.AddDatapoints(ctx, []*datapoint.Datapoint{dptest.DP()}))
	assert.Error(t, f.AddEvents(ctx, []*event.Event{dptest.E()}))
}
func TestConnectorProcessProtoError(t *testing.T) {
	expectedErr := errors.New("marshal error")
	f := Forwarder{
		protoMarshal: func(pb proto.Message) ([]byte, error) {
			return nil, expectedErr
		},
		jsonMarshal: func(interface{}) ([]byte, error) {
			return nil, expectedErr
		},
	}
	err := f.AddDatapoints(context.Background(), []*datapoint.Datapoint{dptest.DP()})
	assert.Equal(t, expectedErr, err.(*forwardError).originalError)
	err = f.AddEvents(context.Background(), []*event.Event{dptest.E()})
	assert.Equal(t, expectedErr, err.(*forwardError).originalError)
}
Example #5
0
func TestFilenameForwarder(t *testing.T) {
	ctx := context.Background()
	fileObj, _ := ioutil.TempFile("", "gotest")
	filename := fileObj.Name()
	defer os.Remove(filename)
	conf := &config.ForwardTo{
		Filename: workarounds.GolangDoesnotAllowPointerToStringLiteral(fileObj.Name()),
	}
	f, err := ForwarderLoader(conf)
	defer f.Close()
	assert.NoError(t, err)
	assert.NoError(t, f.AddDatapoints(ctx, []*datapoint.Datapoint{dptest.DP()}))
	assert.NoError(t, f.AddEvents(ctx, []*event.Event{dptest.E()}))
	assert.Equal(t, 0, len(f.Stats()))
}
func TestResponseBadBody(t *testing.T) {
	f := Forwarder{
		protoMarshal: proto.Marshal,
		jsonMarshal:  json.Marshal,
		client: &http.Client{
			Transport: roundTripTest(func(r *http.Request) (*http.Response, error) {
				r2 := http.Response{
					Body:       ioutil.NopCloser(bytes.NewBufferString(`"BAD"`)),
					StatusCode: 200,
				}
				return &r2, nil
			}),
		},
	}
	err := f.AddDatapoints(context.Background(), []*datapoint.Datapoint{dptest.DP()})
	assert.Contains(t, err.Error(), "body decode error")
	err = f.AddEvents(context.Background(), []*event.Event{dptest.E()})
	assert.Contains(t, err.Error(), "body decode error")
}
func TestResponseBadJSON(t *testing.T) {
	f := Forwarder{
		protoMarshal: proto.Marshal,
		jsonMarshal:  json.Marshal,
		client: &http.Client{
			Transport: roundTripTest(func(r *http.Request) (*http.Response, error) {
				r2 := http.Response{
					Body:       ioutil.NopCloser(bytes.NewBufferString("INVALID_JSON")),
					StatusCode: 200,
				}
				return &r2, nil
			}),
		},
	}
	err := f.AddDatapoints(context.Background(), []*datapoint.Datapoint{dptest.DP()})
	assert.IsType(t, &json.SyntaxError{}, err.(*forwardError).originalError)
	err = f.AddEvents(context.Background(), []*event.Event{dptest.E()})
	assert.IsType(t, &json.SyntaxError{}, err.(*forwardError).originalError)
}
func TestBasicSend(t *testing.T) {
	testServer := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
		assert.Equal(t, "abcd", req.Header.Get("User-Agent"))
		assert.Equal(t, "abcdefg", req.Header.Get(TokenHeaderName))
		rw.Write([]byte(`"OK"`))
	}))
	defer testServer.Close()

	f := NewSignalfxJSONForwarder("", time.Second, "", 10, "", "", "")
	f.UserAgent("abcd")
	f.AuthToken("abcdefg")
	f.Endpoint(testServer.URL)
	f.EventEndpoint(testServer.URL)
	ctx := context.Background()

	dp := dptest.DP()
	e := dptest.E()
	assert.NoError(t, f.AddDatapoints(ctx, []*datapoint.Datapoint{dp}))
	assert.NoError(t, f.AddEvents(ctx, []*event.Event{e}))
}
func TestCarbonNoWriteEvents(t *testing.T) {
	listenFrom := config.ListenFrom{}
	listenFrom.ListenAddr = workarounds.GolangDoesnotAllowPointerToStringLiteral("127.0.0.1:0")
	forwardTo := dptest.NewBasicSink()
	ctx := context.Background()
	l, err := ListenerLoader(ctx, forwardTo, &listenFrom)
	defer l.Close()
	assert.Equal(t, nil, err, "Expect no error")
	assert.Equal(t, numStats, len(l.Stats()), "Expect no stats")
	forwarder, err := NewForwarder("127.0.0.1", nettest.TCPPort(l.psocket), time.Second, []string{"zzfirst"}, 10)
	assert.Equal(t, nil, err, "Expect no error")
	assert.Equal(t, 1, len(forwarder.pool.conns))
	timeToSend := time.Now().Round(time.Second)
	eSent := dptest.E()
	eSent.Timestamp = timeToSend
	eSent.Meta["blarg"] = "abcd 123 123"
	log.Info("Sending a e")
	forwarder.AddEvents(ctx, []*event.Event{eSent})
	assert.Equal(t, 0, len(forwardTo.EventsChan))

}
Example #10
0
func TestRateLimitErrorLoggingEvent(t *testing.T) {
	expectedErr := errors.New("nope")
	end := dptest.NewBasicSink()
	end.RetError(expectedErr)
	ctx := context.Background()
	var lastErr error
	count := 0
	l := RateLimitErrorLogging{
		Callback: func(err error) {
			lastErr = err
			count++
		},
		LogThrottle: time.Second,
	}
	for i := 0; i < 1000; i++ {
		assert.Equal(t, expectedErr, l.AddEvents(ctx, []*event.Event{dptest.E()}, end))
	}
	assert.Equal(t, expectedErr, lastErr)
	assert.Equal(t, 1, count)

}