File: integration_test.go

package info (click to toggle)
golang-github-go-kit-kit 0.13.0-8
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,784 kB
  • sloc: sh: 22; makefile: 11
file content (92 lines) | stat: -rw-r--r-- 2,726 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
//go:build integration
// +build integration

package eureka

import (
	"os"
	"testing"
	"time"

	"github.com/hudl/fargo"

	"github.com/go-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()

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

	// checks every 100ms (fr up to 10s) for the expected number of instances to be reported
	waitForInstances := func(count int) {
		for t := 0; t < 100; t++ {
			state := instancer.state()
			if len(state.Instances) == count {
				return
			}
			time.Sleep(100 * time.Millisecond)
		}
		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)
		}
	}

	// We should have one instance immediately after subscriber instantiation.
	waitForInstances(1)

	// 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.
	waitForInstances(2)

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

	// Wait for another scheduled update.
	// And then there was one.
	waitForInstances(1)
}