Welcome to mirror list, hosted at ThFree Co, Russian Federation.

worker_test.go « rubyserver « internal - gitlab.com/gitlab-org/gitaly.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 70bd21f0e27aacccbaf0d17579c65aeca715f2eb (plain)
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package rubyserver

import (
	"testing"
	"time"

	"github.com/stretchr/testify/require"
	"gitlab.com/gitlab-org/gitaly/internal/config"
	"gitlab.com/gitlab-org/gitaly/internal/supervisor"
)

func TestWorker(t *testing.T) {
	restartDelay := 100 * time.Millisecond
	defer func(old time.Duration) {
		config.Config.Ruby.RestartDelay = old
	}(config.Config.Ruby.RestartDelay)
	config.Config.Ruby.RestartDelay = restartDelay

	events := make(chan supervisor.Event)
	addr := "the address"
	w := newWorker(&supervisor.Process{Name: "testing"}, addr, events)

	mustIgnore := func(e supervisor.Event) {
		nothing := &nothingBalancer{t}
		w.balancerUpdate <- nothing
		t.Logf("sending Event %+v, expect nothing to happen", e)
		events <- e
		// This second balancer update is used to synchronize with the monitor
		// goroutine. When the channel send finishes, we know the event we sent
		// before must have been processed.
		w.balancerUpdate <- nothing
	}

	mustAdd := func(e supervisor.Event) {
		add := newAdd(t, addr)
		w.balancerUpdate <- add
		t.Logf("sending Event %+v, expect balancer add", e)
		events <- e
		add.wait()
	}

	mustRemove := func(e supervisor.Event) {
		remove := newRemove(t, addr)
		w.balancerUpdate <- remove
		t.Logf("sending Event %+v, expect balancer remove", e)
		events <- e
		remove.wait()
	}

	firstPid := 123

	mustAdd(upEvent(firstPid))

	t.Log("ignore repeated up event")
	mustIgnore(upEvent(firstPid))

	t.Log("send mem high events but too fast to trigger restart")
	for i := 0; i < 5; i++ {
		mustIgnore(memHighEvent(firstPid))
	}

	t.Log("mem low resets mem high counter")
	mustIgnore(memLowEvent(firstPid))

	t.Log("send mem high events but too fast to trigger restart")
	for i := 0; i < 5; i++ {
		mustIgnore(memHighEvent(firstPid))
	}

	time.Sleep(2 * restartDelay)
	t.Log("this mem high should push us over the threshold")
	mustRemove(memHighEvent(firstPid))

	secondPid := 456
	t.Log("time for a new PID")
	mustAdd(upEvent(secondPid))

	t.Log("ignore mem high events for the previous pid")
	mustIgnore(memHighEvent(firstPid))
	time.Sleep(2 * restartDelay)
	mustIgnore(memHighEvent(firstPid))

	t.Log("start high memory timer")
	mustIgnore(memHighEvent(secondPid))

	t.Log("ignore mem low event for wrong pid")
	mustIgnore(memLowEvent(firstPid))

	t.Log("send mem high count over the threshold")
	time.Sleep(2 * restartDelay)
	mustRemove(memHighEvent(secondPid))
}

func waitFail(t *testing.T, done chan struct{}) {
	select {
	case <-time.After(1 * time.Second):
		t.Fatal("timeout waiting for balancer method call")
	case <-done:
	}
}

func upEvent(pid int) supervisor.Event {
	return supervisor.Event{Type: supervisor.Up, Pid: pid}
}

func memHighEvent(pid int) supervisor.Event {
	return supervisor.Event{Type: supervisor.MemoryHigh, Pid: pid}
}

func memLowEvent(pid int) supervisor.Event {
	return supervisor.Event{Type: supervisor.MemoryLow, Pid: pid}
}

func newAdd(t *testing.T, addr string) *addBalancer {
	return &addBalancer{
		t:    t,
		addr: addr,
		done: make(chan struct{}),
	}
}

type addBalancer struct {
	addr string
	t    *testing.T
	done chan struct{}
}

func (ab *addBalancer) RemoveAddress(string) bool {
	ab.t.Fatal("unexpected RemoveAddress call")
	return false
}

func (ab *addBalancer) AddAddress(s string) {
	require.Equal(ab.t, ab.addr, s, "addBalancer expected AddAddress argument")
	close(ab.done)
}

func (ab *addBalancer) wait() {
	waitFail(ab.t, ab.done)
}

func newRemove(t *testing.T, addr string) *removeBalancer {
	return &removeBalancer{
		t:    t,
		addr: addr,
		done: make(chan struct{}),
	}
}

type removeBalancer struct {
	addr string
	t    *testing.T
	done chan struct{}
}

func (rb *removeBalancer) RemoveAddress(s string) bool {
	require.Equal(rb.t, rb.addr, s, "removeBalancer expected RemoveAddress argument")
	close(rb.done)
	return false
}

func (rb *removeBalancer) AddAddress(s string) {
	rb.t.Fatal("unexpected AddAddress call")
}

func (rb *removeBalancer) wait() {
	waitFail(rb.t, rb.done)
}

type nothingBalancer struct {
	t *testing.T
}

func (nb *nothingBalancer) RemoveAddress(s string) bool {
	nb.t.Fatal("unexpected RemoveAddress call")
	return false
}

func (nb *nothingBalancer) AddAddress(s string) {
	nb.t.Fatal("unexpected AddAddress call")
}