File: cache_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 (110 lines) | stat: -rw-r--r-- 3,236 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package instance

import (
	"context"
	"fmt"
	"io"
	"reflect"
	"testing"
	"time"

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

var _ sd.Instancer = (*Cache)(nil) // API check

// The test verifies the following:
//   registering causes initial notification of the current state
//   instances are sorted
//   different update causes new notification
//   identical notifications cause no updates
//   no updates after de-registering
func TestCache(t *testing.T) {
	e1 := sd.Event{Instances: []string{"y", "x"}} // not sorted
	e2 := sd.Event{Instances: []string{"c", "a", "b"}}

	cache := NewCache()
	if want, have := 0, len(cache.State().Instances); want != have {
		t.Fatalf("want %v instances, have %v", want, have)
	}

	cache.Update(e1) // sets initial state
	if want, have := 2, len(cache.State().Instances); want != have {
		t.Fatalf("want %v instances, have %v", want, have)
	}

	r1 := make(chan sd.Event)
	go cache.Register(r1)
	expectUpdate(t, r1, []string{"x", "y"})

	go cache.Update(e2) // different set
	expectUpdate(t, r1, []string{"a", "b", "c"})

	cache.Deregister(r1)
	close(r1)
}

func expectUpdate(t *testing.T, r chan sd.Event, expect []string) {
	select {
	case e := <-r:
		if want, have := expect, e.Instances; !reflect.DeepEqual(want, have) {
			t.Fatalf("want: %v, have: %v", want, have)
		}
	case <-time.After(time.Second):
		t.Fatalf("did not receive expected update %v", expect)
	}
}

func TestRegistry(t *testing.T) {
	reg := make(registry)
	c1 := make(chan sd.Event, 1)
	c2 := make(chan sd.Event, 1)
	reg.register(c1)
	reg.register(c2)

	// validate that both channels receive the update
	reg.broadcast(sd.Event{Instances: []string{"x", "y"}})
	if want, have := []string{"x", "y"}, (<-c1).Instances; !reflect.DeepEqual(want, have) {
		t.Fatalf("want: %v, have: %v", want, have)
	}
	if want, have := []string{"x", "y"}, (<-c2).Instances; !reflect.DeepEqual(want, have) {
		t.Fatalf("want: %v, have: %v", want, have)
	}

	reg.deregister(c1)
	reg.deregister(c2)
	close(c1)
	close(c2)
	// if deregister didn't work, broadcast would panic on closed channels
	reg.broadcast(sd.Event{Instances: []string{"x", "y"}})
}

// This test is meant to be run with the race detector enabled: -race.
// It ensures that every registered observer receives a copy
// of sd.Event.Instances because observers can directly modify the field.
// For example, endpointCache calls sort.Strings() on sd.Event.Instances.
func TestDataRace(t *testing.T) {
	instances := make([]string, 0)
	// the number of iterations here maters because we need sort.Strings to
	// perform a Swap in doPivot -> medianOfThree to cause a data race.
	for i := 1; i < 1000; i++ {
		instances = append(instances, fmt.Sprintf("%v", i))
	}
	e1 := sd.Event{Instances: instances}
	cache := NewCache()
	cache.Update(e1)
	nullEndpoint := func(_ context.Context, _ interface{}) (interface{}, error) {
		return nil, nil
	}
	nullFactory := func(instance string) (endpoint.Endpoint, io.Closer, error) {
		return nullEndpoint, nil, nil
	}
	logger := log.Logger(log.LoggerFunc(func(keyvals ...interface{}) error {
		return nil
	}))

	sd.NewEndpointer(cache, nullFactory, logger)
	sd.NewEndpointer(cache, nullFactory, logger)
}