Codebase list golang-github-go-kit-kit / 513c60c sd / eureka / integration_test.go
513c60c

Tree @513c60c (Download .tar.gz)

integration_test.go @513c60craw · history · blame

// +build integration

package eureka

import (
	"os"
	"testing"
	"time"

	"github.com/hudl/fargo"

	"github.com/go-kit/kit/log"
)

// Package sd/eureka provides a wrapper around the Netflix Eureka service
// registry by way of the Fargo library. This test assumes the user has an
// instance of Eureka available at the address in the environment variable.
// Example `${EUREKA_ADDR}` format: http://localhost:8761/eureka
//
// NOTE: when starting a Eureka server for integration testing, ensure
// the response cache interval is reduced to one second. This can be
// achieved with the following Java argument:
// `-Deureka.server.responseCacheUpdateIntervalMs=1000`
func TestIntegration(t *testing.T) {
	eurekaAddr := os.Getenv("EUREKA_ADDR")
	if eurekaAddr == "" {
		t.Skip("EUREKA_ADDR is not set")
	}

	logger := log.NewLogfmtLogger(os.Stderr)
	logger = log.With(logger, "ts", log.DefaultTimestamp)

	var fargoConfig fargo.Config
	// Target Eureka server(s).
	fargoConfig.Eureka.ServiceUrls = []string{eurekaAddr}
	// How often the subscriber should poll for updates.
	fargoConfig.Eureka.PollIntervalSeconds = 1

	// Create a Fargo connection and a Eureka registrar.
	fargoConnection := fargo.NewConnFromConfig(fargoConfig)
	registrar1 := NewRegistrar(&fargoConnection, instanceTest1, log.With(logger, "component", "registrar1"))

	// Register one instance.
	registrar1.Register()
	defer registrar1.Deregister()

	// This should be enough time for the Eureka server response cache to update.
	time.Sleep(time.Second)

	// Build a Eureka instancer.
	instancer := NewInstancer(
		&fargoConnection,
		appNameTest,
		log.With(logger, "component", "instancer"),
	)
	defer instancer.Stop()

	// We should have one instance immediately after subscriber instantiation.
	state := instancer.state()
	if state.Err != nil {
		t.Error(state.Err)
	}
	if want, have := 1, len(state.Instances); want != have {
		t.Errorf("want %d, have %d", want, have)
	}

	// Register a second instance
	registrar2 := NewRegistrar(&fargoConnection, instanceTest2, log.With(logger, "component", "registrar2"))
	registrar2.Register()
	defer registrar2.Deregister() // In case of exceptional circumstances.

	// This should be enough time for a scheduled update assuming Eureka is
	// configured with the properties mentioned in the function comments.
	time.Sleep(2 * time.Second)

	// Now we should have two instances.
	state = instancer.state()
	if state.Err != nil {
		t.Error(state.Err)
	}
	if want, have := 2, len(state.Instances); want != have {
		t.Errorf("want %d, have %d", want, have)
	}

	// Deregister the second instance.
	registrar2.Deregister()

	// Wait for another scheduled update.
	time.Sleep(2 * time.Second)

	// And then there was one.
	state = instancer.state()
	if state.Err != nil {
		t.Error(state.Err)
	}
	if want, have := 1, len(state.Instances); want != have {
		t.Errorf("want %d, have %d", want, have)
	}
}