summaryrefslogtreecommitdiff
path: root/plugins/reload/tests/reload_plugin_test.go
blob: 9f1e551782c0bfea82cb6c89893f925968a5b6ae (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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
package tests

import (
	"io"
	"io/ioutil"
	"math/rand"
	"net/http"
	"os"
	"os/signal"
	"path/filepath"
	"strconv"
	"sync"
	"syscall"
	"testing"
	"time"

	"github.com/golang/mock/gomock"
	"github.com/spiral/endure"
	"github.com/spiral/errors"
	"github.com/spiral/roadrunner/v2/mocks"
	"github.com/spiral/roadrunner/v2/plugins/config"
	httpPlugin "github.com/spiral/roadrunner/v2/plugins/http"
	"github.com/spiral/roadrunner/v2/plugins/logger"
	"github.com/spiral/roadrunner/v2/plugins/reload"
	"github.com/spiral/roadrunner/v2/plugins/resetter"
	"github.com/spiral/roadrunner/v2/plugins/server"
	"github.com/stretchr/testify/assert"
)

const testDir string = "unit_tests"
const hugeNumberOfFiles uint = 5000

func TestReloadInit(t *testing.T) {
	cont, err := endure.NewContainer(nil, endure.SetLogLevel(endure.DebugLevel))
	assert.NoError(t, err)

	cfg := &config.Viper{
		Path:   "configs/.rr-reload.yaml",
		Prefix: "rr",
	}

	// try to remove, skip error
	assert.NoError(t, freeResources(testDir))
	err = os.Mkdir(testDir, 0755)
	assert.NoError(t, err)

	defer func() {
		assert.NoError(t, freeResources(testDir))
	}()

	controller := gomock.NewController(t)
	mockLogger := mocks.NewMockLogger(controller)

	mockLogger.EXPECT().Debug("file was created", "path", gomock.Any(), "name", "file.txt", "size", gomock.Any()).Times(2)
	mockLogger.EXPECT().Info("HTTP plugin got restart request. Restarting...").Times(1)
	mockLogger.EXPECT().Info("HTTP workers Pool successfully restarted").Times(1)
	mockLogger.EXPECT().Info("HTTP listeners successfully re-added").Times(1)
	mockLogger.EXPECT().Info("HTTP plugin successfully restarted").Times(1)

	err = cont.RegisterAll(
		cfg,
		mockLogger,
		&server.Plugin{},
		&httpPlugin.Plugin{},
		&reload.Plugin{},
		&resetter.Plugin{},
	)
	assert.NoError(t, err)

	err = cont.Init()
	if err != nil {
		t.Fatal(err)
	}

	ch, err := cont.Serve()
	assert.NoError(t, err)

	sig := make(chan os.Signal, 1)
	signal.Notify(sig, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)

	wg := &sync.WaitGroup{}
	wg.Add(1)

	tt := time.NewTimer(time.Second * 10)

	go func() {
		defer wg.Done()
		for {
			select {
			case e := <-ch:
				assert.Fail(t, "error", e.Error.Error())
				err = cont.Stop()
				if err != nil {
					assert.FailNow(t, "error", err.Error())
				}
			case <-sig:
				err = cont.Stop()
				if err != nil {
					assert.FailNow(t, "error", err.Error())
				}
				return
			case <-tt.C:
				// timeout
				err = cont.Stop()
				if err != nil {
					assert.FailNow(t, "error", err.Error())
				}
				return
			}
		}
	}()

	t.Run("ReloadTestInit", reloadTestInit)

	wg.Wait()
}

func reloadTestInit(t *testing.T) {
	err := ioutil.WriteFile(filepath.Join(testDir, "file.txt"), //nolint:gosec
		[]byte{}, 0755)
	assert.NoError(t, err)
}

func TestReloadHugeNumberOfFiles(t *testing.T) {
	cont, err := endure.NewContainer(nil, endure.SetLogLevel(endure.DebugLevel))
	assert.NoError(t, err)

	cfg := &config.Viper{
		Path:   "configs/.rr-reload.yaml",
		Prefix: "rr",
	}

	// try to remove, skip error
	assert.NoError(t, freeResources(testDir))
	err = os.Mkdir(testDir, 0755)
	assert.NoError(t, err)

	defer func() {
		assert.NoError(t, freeResources(testDir))
	}()

	// controller := gomock.NewController(t)
	// mockLogger := mocks.NewMockLogger(controller)
	//
	// mockLogger.EXPECT().Debug("file was created", "path", gomock.Any(), "name", "file.txt", "size", gomock.Any()).Times(2)
	// mockLogger.EXPECT().Info("Resetting http plugin").Times(1)

	err = cont.RegisterAll(
		cfg,
		// mockLogger,
		&logger.ZapLogger{},
		&server.Plugin{},
		&httpPlugin.Plugin{},
		&reload.Plugin{},
		&resetter.Plugin{},
	)
	assert.NoError(t, err)

	err = cont.Init()
	if err != nil {
		t.Fatal(err)
	}

	ch, err := cont.Serve()
	assert.NoError(t, err)

	sig := make(chan os.Signal, 1)
	signal.Notify(sig, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)

	wg := &sync.WaitGroup{}
	wg.Add(1)

	tt := time.NewTimer(time.Second * 160)

	go func() {
		defer wg.Done()
		for {
			select {
			case e := <-ch:
				assert.Fail(t, "error", e.Error.Error())
				err = cont.Stop()
				if err != nil {
					assert.FailNow(t, "error", err.Error())
				}
			case <-sig:
				err = cont.Stop()
				if err != nil {
					assert.FailNow(t, "error", err.Error())
				}
				return
			case <-tt.C:
				// timeout
				err = cont.Stop()
				if err != nil {
					assert.FailNow(t, "error", err.Error())
				}
				return
			}
		}
	}()

	t.Run("ReloadTestHugeNumberOfFiles", reloadHugeNumberOfFiles)
	ttt := time.Now()
	t.Run("ReloadRandomlyChangeFile", randomlyChangeFile)
	if time.Since(ttt).Seconds() > 140 {
		t.Fatal("spend too much time on reloading")
	}
	t.Run("ReloadHTTPLiveAfterReset", reloadHTTPLiveAfterReset)

	wg.Wait()
}

func reloadHTTPLiveAfterReset(t *testing.T) {
	req, err := http.NewRequest("GET", "http://localhost:22388", nil)
	assert.NoError(t, err)

	r, err := http.DefaultClient.Do(req)
	assert.NoError(t, err)

	b, err := ioutil.ReadAll(r.Body)
	assert.NoError(t, err)

	assert.Equal(t, 200, r.StatusCode)
	assert.Equal(t, "hello world", string(b))

	err = r.Body.Close()
	assert.NoError(t, err)
}

func randomlyChangeFile(t *testing.T) {
	// we know, that directory contains 5000 files (0-4999)
	// let's try to randomly change it
	for i := 0; i < 100; i++ {
		// rand sleep
		rSleep := rand.Int63n(1000) // nolint:gosec
		time.Sleep(time.Millisecond * time.Duration(rSleep))
		rNum := rand.Int63n(int64(hugeNumberOfFiles))                                                                            // nolint:gosec
		err := ioutil.WriteFile(filepath.Join(testDir, "file_"+strconv.Itoa(int(rNum))+".txt"), []byte("Hello, Gophers!"), 0755) // nolint:gosec
		assert.NoError(t, err)
	}
}

func reloadHugeNumberOfFiles(t *testing.T) {
	for i := uint(0); i < hugeNumberOfFiles; i++ {
		assert.NoError(t, makeFile("file_"+strconv.Itoa(int(i))+".txt"))
	}
}

func freeResources(path string) error {
	return os.RemoveAll(path)
}

func makeFile(filename string) error {
	return ioutil.WriteFile(filepath.Join(testDir, filename), []byte{}, 0755) //nolint:gosec
}

func copyDir(src string, dst string) error {
	src = filepath.Clean(src)
	dst = filepath.Clean(dst)

	si, err := os.Stat(src)
	if err != nil {
		return err
	}
	if !si.IsDir() {
		return errors.E(errors.Str("source is not a directory"))
	}

	_, err = os.Stat(dst)
	if err != nil && !os.IsNotExist(err) {
		return err
	}
	if err == nil {
		return errors.E(errors.Str("destination already exists"))
	}

	err = os.MkdirAll(dst, si.Mode())
	if err != nil {
		return err
	}

	entries, err := ioutil.ReadDir(src)
	if err != nil {
		return err
	}

	for _, entry := range entries {
		srcPath := filepath.Join(src, entry.Name())
		dstPath := filepath.Join(dst, entry.Name())

		if entry.IsDir() {
			err = copyDir(srcPath, dstPath)
			if err != nil {
				return err
			}
		} else {
			// Skip symlinks.
			if entry.Mode()&os.ModeSymlink != 0 {
				continue
			}

			err = copyFile(srcPath, dstPath)
			if err != nil {
				return err
			}
		}
	}
	return nil
}

func copyFile(src, dst string) error {
	in, err := os.Open(src)
	if err != nil {
		return errors.E(err)
	}
	defer func() {
		_ = in.Close()
	}()

	out, err := os.Create(dst)
	if err != nil {
		return errors.E(err)
	}
	defer func() {
		_ = out.Close()
	}()

	_, err = io.Copy(out, in)
	if err != nil {
		return errors.E(err)
	}

	err = out.Sync()
	if err != nil {
		return errors.E(err)
	}

	si, err := os.Stat(src)
	if err != nil {
		return errors.E(err)
	}
	err = os.Chmod(dst, si.Mode())
	if err != nil {
		return errors.E(err)
	}
	return nil
}