...

Source file src/github.com/go-kit/kit/sd/eureka/integration_test.go

Documentation: github.com/go-kit/kit/sd/eureka

     1  //go:build integration
     2  // +build integration
     3  
     4  package eureka
     5  
     6  import (
     7  	"os"
     8  	"testing"
     9  	"time"
    10  
    11  	"github.com/hudl/fargo"
    12  
    13  	"github.com/go-kit/log"
    14  )
    15  
    16  // Package sd/eureka provides a wrapper around the Netflix Eureka service
    17  // registry by way of the Fargo library. This test assumes the user has an
    18  // instance of Eureka available at the address in the environment variable.
    19  // Example `${EUREKA_ADDR}` format: http://localhost:8761/eureka
    20  //
    21  // NOTE: when starting a Eureka server for integration testing, ensure
    22  // the response cache interval is reduced to one second. This can be
    23  // achieved with the following Java argument:
    24  // `-Deureka.server.responseCacheUpdateIntervalMs=1000`
    25  func TestIntegration(t *testing.T) {
    26  	eurekaAddr := os.Getenv("EUREKA_ADDR")
    27  	if eurekaAddr == "" {
    28  		t.Skip("EUREKA_ADDR is not set")
    29  	}
    30  
    31  	logger := log.NewLogfmtLogger(os.Stderr)
    32  	logger = log.With(logger, "ts", log.DefaultTimestamp)
    33  
    34  	var fargoConfig fargo.Config
    35  	// Target Eureka server(s).
    36  	fargoConfig.Eureka.ServiceUrls = []string{eurekaAddr}
    37  	// How often the subscriber should poll for updates.
    38  	fargoConfig.Eureka.PollIntervalSeconds = 1
    39  
    40  	// Create a Fargo connection and a Eureka registrar.
    41  	fargoConnection := fargo.NewConnFromConfig(fargoConfig)
    42  	registrar1 := NewRegistrar(&fargoConnection, instanceTest1, log.With(logger, "component", "registrar1"))
    43  
    44  	// Register one instance.
    45  	registrar1.Register()
    46  	defer registrar1.Deregister()
    47  
    48  	// Build a Eureka instancer.
    49  	instancer := NewInstancer(
    50  		&fargoConnection,
    51  		appNameTest,
    52  		log.With(logger, "component", "instancer"),
    53  	)
    54  	defer instancer.Stop()
    55  
    56  	// checks every 100ms (fr up to 10s) for the expected number of instances to be reported
    57  	waitForInstances := func(count int) {
    58  		for t := 0; t < 100; t++ {
    59  			state := instancer.state()
    60  			if len(state.Instances) == count {
    61  				return
    62  			}
    63  			time.Sleep(100 * time.Millisecond)
    64  		}
    65  		state := instancer.state()
    66  		if state.Err != nil {
    67  			t.Error(state.Err)
    68  		}
    69  		if want, have := 1, len(state.Instances); want != have {
    70  			t.Errorf("want %d, have %d", want, have)
    71  		}
    72  	}
    73  
    74  	// We should have one instance immediately after subscriber instantiation.
    75  	waitForInstances(1)
    76  
    77  	// Register a second instance
    78  	registrar2 := NewRegistrar(&fargoConnection, instanceTest2, log.With(logger, "component", "registrar2"))
    79  	registrar2.Register()
    80  	defer registrar2.Deregister() // In case of exceptional circumstances.
    81  
    82  	// This should be enough time for a scheduled update assuming Eureka is
    83  	// configured with the properties mentioned in the function comments.
    84  	waitForInstances(2)
    85  
    86  	// Deregister the second instance.
    87  	registrar2.Deregister()
    88  
    89  	// Wait for another scheduled update.
    90  	// And then there was one.
    91  	waitForInstances(1)
    92  }
    93  

View as plain text