summaryrefslogtreecommitdiff
path: root/plugins/factory/tests/factory_test.go
blob: 38e939e1b3bfeeb5b97a7ea33e7d0c4e54bf609c (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
package tests

import (
	"os"
	"os/signal"
	"testing"
	"time"

	"github.com/spiral/endure"
	"github.com/spiral/roadrunner/v2/plugins/config"
	"github.com/spiral/roadrunner/v2/plugins/factory"
	"github.com/stretchr/testify/assert"
)

func TestFactory(t *testing.T) {
	container, err := endure.NewContainer(endure.DebugLevel, endure.RetryOnFail(true))
	if err != nil {
		t.Fatal(err)
	}
	// config plugin
	vp := &config.ViperProvider{}
	vp.Path = ".rr.yaml"
	vp.Prefix = "rr"
	err = container.Register(vp)
	if err != nil {
		t.Fatal(err)
	}

	err = container.Register(&factory.App{})
	if err != nil {
		t.Fatal(err)
	}

	err = container.Register(&factory.WFactory{})
	if err != nil {
		t.Fatal(err)
	}

	err = container.Register(&Foo{})
	if err != nil {
		t.Fatal(err)
	}

	err = container.Register(&Foo2{})
	if err != nil {
		t.Fatal(err)
	}


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

	errCh, err := container.Serve()
	if err != nil {
		t.Fatal(err)
	}

	// stop by CTRL+C
	c := make(chan os.Signal)
	signal.Notify(c, os.Interrupt)

	tt := time.NewTicker(time.Second * 2)

	for {
		select {
		case e := <-errCh:
			assert.NoError(t, e.Error.Err)
			assert.NoError(t, container.Stop())
			return
		case <-c:
			er := container.Stop()
			if er != nil {
				panic(er)
			}
			return
		case <-tt.C:
			tt.Stop()
			assert.NoError(t, container.Stop())
			return
		}
	}

}