OSDN Git Service

delete miner
[bytom/vapor.git] / vendor / github.com / go-kit / kit / sd / eureka / integration_test.go
1 // +build integration
2
3 package eureka
4
5 import (
6         "os"
7         "testing"
8         "time"
9
10         "github.com/hudl/fargo"
11
12         "github.com/go-kit/kit/log"
13 )
14
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
19 //
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")
26         if eurekaAddr == "" {
27                 t.Skip("EUREKA_ADDR is not set")
28         }
29
30         logger := log.NewLogfmtLogger(os.Stderr)
31         logger = log.With(logger, "ts", log.DefaultTimestamp)
32
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
38
39         // Create a Fargo connection and a Eureka registrar.
40         fargoConnection := fargo.NewConnFromConfig(fargoConfig)
41         registrar1 := NewRegistrar(&fargoConnection, instanceTest1, log.With(logger, "component", "registrar1"))
42
43         // Register one instance.
44         registrar1.Register()
45         defer registrar1.Deregister()
46
47         // Build a Eureka instancer.
48         instancer := NewInstancer(
49                 &fargoConnection,
50                 appNameTest,
51                 log.With(logger, "component", "instancer"),
52         )
53         defer instancer.Stop()
54
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 {
60                                 return
61                         }
62                         time.Sleep(100 * time.Millisecond)
63                 }
64                 state := instancer.state()
65                 if state.Err != nil {
66                         t.Error(state.Err)
67                 }
68                 if want, have := 1, len(state.Instances); want != have {
69                         t.Errorf("want %d, have %d", want, have)
70                 }
71         }
72
73         // We should have one instance immediately after subscriber instantiation.
74         waitForInstances(1)
75
76         // Register a second instance
77         registrar2 := NewRegistrar(&fargoConnection, instanceTest2, log.With(logger, "component", "registrar2"))
78         registrar2.Register()
79         defer registrar2.Deregister() // In case of exceptional circumstances.
80
81         // This should be enough time for a scheduled update assuming Eureka is
82         // configured with the properties mentioned in the function comments.
83         waitForInstances(2)
84
85         // Deregister the second instance.
86         registrar2.Deregister()
87
88         // Wait for another scheduled update.
89         // And then there was one.
90         waitForInstances(1)
91 }