func TestInvalidPort(t *testing.T) {

	ft := config.ForwardTo{
		Host: workarounds.GolangDoesnotAllowPointerToStringLiteral("zzfbdsaj_)__@#$%.zzzzzzzzzzz"),
		Port: workarounds.GolangDoesnotAllowPointerToUint16Literal(1),
	}
	_, err := ForwarderLoader(context.Background(), &ft)
	assert.NotEqual(t, nil, err, "Expect an error")
}
func TestLoader(t *testing.T) {
	listenFrom := config.ListenFrom{}
	listenFrom.ListenAddr = workarounds.GolangDoesnotAllowPointerToStringLiteral("127.0.0.1:0")
	ctx := context.Background()
	forwardTo := dptest.NewBasicSink()
	l, err := ListenerLoader(ctx, forwardTo, &listenFrom)
	port := nettest.TCPPort(l.psocket)

	ft := config.ForwardTo{
		Host: workarounds.GolangDoesnotAllowPointerToStringLiteral("127.0.0.1"),
		Port: workarounds.GolangDoesnotAllowPointerToUint16Literal(port),
	}
	f, err := ForwarderLoader(context.Background(), &ft)
	assert.NoError(t, err)
	dpSent := dptest.DP()
	dpSent.Dimensions = map[string]string{}
	assert.NoError(t, f.AddDatapoints(ctx, []*datapoint.Datapoint{dpSent}))
	dpSeen := forwardTo.Next()
	assert.Equal(t, dpSent.Metric, dpSeen.Metric)
	assert.Equal(t, numStats+1, len(f.Stats()))
}
Example #3
0
		dimensionComparor: dpdimsort.NewOrdering(dimensionOrder),
		connectionTimeout: timeout,
		connectionAddress: connectionAddress,
		dialer:            net.DialTimeout,
		pool: connPool{
			conns: make([]net.Conn, 0, drainingThreads),
		},
	}
	ret.pool.Return(conn)
	return ret, nil
}

var defaultForwarderConfig = &config.ForwardTo{
	TimeoutDuration: workarounds.GolangDoesnotAllowPointerToTimeLiteral(time.Second * 30),
	BufferSize:      workarounds.GolangDoesnotAllowPointerToUintLiteral(uint32(10000)),
	Port:            workarounds.GolangDoesnotAllowPointerToUint16Literal(2003),
	DrainingThreads: workarounds.GolangDoesnotAllowPointerToUintLiteral(uint32(5)),
	Name:            workarounds.GolangDoesnotAllowPointerToStringLiteral("carbonforwarder"),
	MaxDrainSize:    workarounds.GolangDoesnotAllowPointerToUintLiteral(uint32(1000)),
	DimensionsOrder: []string{},
}

var errRequiredHost = errors.New("carbon forwarder requires host config")

// ForwarderLoader loads a carbon forwarder that is buffered
func ForwarderLoader(ctx context.Context, forwardTo *config.ForwardTo) (protocol.Forwarder, error) {
	structdefaults.FillDefaultFrom(forwardTo, defaultForwarderConfig)
	if forwardTo.Host == nil {
		return nil, errRequiredHost
	}
	fwd, err := NewForwarder(*forwardTo.Host, *forwardTo.Port, *forwardTo.TimeoutDuration, forwardTo.DimensionsOrder, *forwardTo.DrainingThreads)