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