-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver_test.go
178 lines (142 loc) · 3.72 KB
/
server_test.go
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
package raiden_test
import (
"errors"
"fmt"
"os"
"os/exec"
"syscall"
"testing"
"time"
"cloud.google.com/go/pubsub"
"github.com/go-co-op/gocron/v2"
"github.com/google/uuid"
"github.com/hashicorp/go-hclog"
"github.com/sev-2/raiden"
"github.com/stretchr/testify/assert"
)
type PrinHello struct {
raiden.JobBase
}
type LibA struct{}
type LibB struct{}
func (j *PrinHello) Name() string {
return "print-hello"
}
func (j *PrinHello) After(ctx raiden.JobContext, jobID uuid.UUID, jobName string) {
}
func (j *PrinHello) AfterErr(ctx raiden.JobContext, jobID uuid.UUID, jobName string, err error) {
}
func (j *PrinHello) Before(ctx raiden.JobContext, jobID uuid.UUID, jobName string) {
}
func (j *PrinHello) Duration() gocron.JobDefinition {
return gocron.DurationJob(time.Second * 20)
}
func (j *PrinHello) Task(ctx raiden.JobContext) error {
fmt.Printf("[%s] this message executed at %s \n", ctx.Config().ProjectName, time.Now().String())
return nil
}
type TestSubscriber struct {
raiden.SubscriberBase
}
func (s *TestSubscriber) Name() string {
return "Test New"
}
func (s *TestSubscriber) Provider() raiden.PubSubProviderType {
return raiden.PubSubProviderGoogle
}
func (s *TestSubscriber) Subscripbtion() string {
return "test.new-sub"
}
func (s *TestSubscriber) Consume(ctx raiden.SubscriberContext, message any) error {
msg, valid := message.(*pubsub.Message)
if !valid {
return errors.New("invalid google pubsub message")
}
raiden.Info("test new listener executed", "data", string(msg.Data))
return nil
}
func MockMiddlewareFn() raiden.MiddlewareFn {
return func(next raiden.RouteHandlerFn) raiden.RouteHandlerFn {
return nil
}
}
func TestServer_RegisterRoute(t *testing.T) {
conf := loadConfig()
s := raiden.NewServer(conf)
routes := []*raiden.Route{
{
Methods: []string{"GET"}, Path: "/",
},
}
s.RegisterRoute(routes)
assert.NotNil(t, s.Router)
}
func TestServer_RegisterJob(t *testing.T) {
conf := loadConfig()
s := raiden.NewServer(conf)
s.RegisterJobs(&PrinHello{})
assert.NotNil(t, s.RegisterJobs)
}
func TestServer_RegisterSubscriber(t *testing.T) {
conf := loadConfig()
s := raiden.NewServer(conf)
s.RegisterSubscribers(&TestSubscriber{})
assert.NotNil(t, s.RegisterSubscribers)
}
func TestServer_Use(t *testing.T) {
conf := loadConfig()
s := raiden.NewServer(conf)
middleware := MockMiddlewareFn()
s.Use(middleware)
}
func TestServer_Log(t *testing.T) {
conf := loadConfig()
conf.LogLevel = "trace"
s := raiden.NewServer(conf)
s.ConfigureLogLevel()
assert.Equal(t, hclog.Trace, raiden.GetLogLevel())
conf.LogLevel = "debug"
s = raiden.NewServer(conf)
s.ConfigureLogLevel()
assert.Equal(t, hclog.Debug, raiden.GetLogLevel())
conf.LogLevel = "warning"
s = raiden.NewServer(conf)
s.ConfigureLogLevel()
assert.Equal(t, hclog.Warn, raiden.GetLogLevel())
conf.LogLevel = "error"
s = raiden.NewServer(conf)
s.ConfigureLogLevel()
assert.Equal(t, hclog.Error, raiden.GetLogLevel())
}
func TestServer_StartStop(t *testing.T) {
if os.Getenv("TEST_RUN") == "1" {
conf := loadConfig()
s := raiden.NewServer(conf)
s.RegisterJobs(&PrinHello{})
s.RegisterSubscribers(&TestSubscriber{})
s.RegisterLibs(func(config *raiden.Config) any {
return &SomeLib{
config: config,
}
})
s.Run()
return
}
cmd := exec.Command(os.Args[0], "-test.run=TestServer_StartStop")
cmd.Env = append(os.Environ(), "TEST_RUN=1")
err := cmd.Start()
assert.NoError(t, err)
time.Sleep(1 * time.Second)
err1 := cmd.Process.Signal(syscall.SIGTERM)
assert.NoError(t, err1)
}
func TestServer_RegisterLibs(t *testing.T) {
conf := loadConfig()
s := raiden.NewServer(conf)
s.RegisterLibs(func(config *raiden.Config) any {
return &SomeLib{
config: config,
}
})
assert.NotNil(t, s.RegisterLibs)
}