summaryrefslogtreecommitdiff
path: root/lib/roadrunner_test.go
blob: a4e34f318d4a5a3ebfe1fcd26b266e64e340d9ed (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
package lib_test

import (
	"os"
	"testing"
	"time"

	"github.com/roadrunner-server/informer/v5"
	"github.com/roadrunner-server/resetter/v5"
	"github.com/roadrunner-server/roadrunner/v2024/lib"
	"github.com/stretchr/testify/assert"
)

func TestNewFailsOnMissingConfig(t *testing.T) {
	_, err := lib.NewRR("config/file/does/not/exist/.rr.yaml", []string{}, lib.DefaultPluginsList())
	assert.NotNil(t, err)
}

const testConfigWithVersion = `
version: '3'
server:
  command: "php src/index.php"
  relay:  "pipes"

endure:
  grace_period: 1s
`

const testConfig = `
server:
  command: "php src/index.php"
  relay:  "pipes"

endure:
  grace_period: 1s
`

func makeConfig(t *testing.T, configYaml string) string {
	cfgFile := os.TempDir() + "/.rr.yaml"
	err := os.WriteFile(cfgFile, []byte(configYaml), 0600)
	assert.NoError(t, err)

	return cfgFile
}

func TestNewWithOldConfig(t *testing.T) {
	cfgFile := makeConfig(t, testConfig)
	_, err := lib.NewRR(cfgFile, []string{}, lib.DefaultPluginsList())
	assert.Error(t, err)

	t.Cleanup(func() {
		_ = os.Remove(cfgFile)
	})
}

func TestNewWithConfig(t *testing.T) {
	cfgFile := makeConfig(t, testConfigWithVersion)
	rr, err := lib.NewRR(cfgFile, []string{}, lib.DefaultPluginsList())
	assert.NoError(t, err)
	assert.Equal(t, "3", rr.Version)

	t.Cleanup(func() {
		_ = os.Remove(cfgFile)
	})
}

func TestServeStop(t *testing.T) {
	cfgFile := makeConfig(t, testConfigWithVersion)
	plugins := []any{
		&informer.Plugin{},
		&resetter.Plugin{},
	}
	rr, err := lib.NewRR(cfgFile, []string{}, plugins)
	assert.NoError(t, err)

	errchan := make(chan error, 1)
	stopchan := make(chan struct{}, 1)

	go func() {
		errchan <- rr.Serve()
		stopchan <- struct{}{}
	}()

	rr.Stop()
	time.Sleep(time.Second * 2)

	assert.Equal(t, struct{}{}, <-stopchan)
	assert.Nil(t, <-errchan)

	t.Cleanup(func() {
		_ = os.Remove(cfgFile)
	})
}