-
Notifications
You must be signed in to change notification settings - Fork 2
/
reflect_test.go
154 lines (126 loc) · 3.9 KB
/
reflect_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
package restate_test
import (
"context"
"testing"
restate "github.com/restatedev/sdk-go"
"github.com/restatedev/sdk-go/internal"
"github.com/restatedev/sdk-go/internal/state"
"github.com/stretchr/testify/require"
)
type reflectTestParams struct {
rcvr interface{}
opts []restate.ServiceDefinitionOption
serviceName string
serviceType internal.ServiceType
expectedMethods expectedMethods
shouldPanic bool
}
type expectedMethods = map[string]*internal.ServiceHandlerType
var exclusive = internal.ServiceHandlerType_EXCLUSIVE
var shared = internal.ServiceHandlerType_SHARED
var tests []reflectTestParams = []reflectTestParams{
{rcvr: validObject{}, serviceName: "validObject", expectedMethods: expectedMethods{
"Greet": &exclusive,
"GreetShared": &shared,
"NoInput": &exclusive,
"NoError": &exclusive,
"NoOutput": &exclusive,
"NoOutputNoError": &exclusive,
"NoInputNoError": &exclusive,
"NoInputNoOutput": &exclusive,
"NoInputNoOutputNoError": &exclusive,
}},
{rcvr: validService{}, serviceName: "validService", expectedMethods: expectedMethods{
"Greet": nil,
}},
{rcvr: namedService{}, serviceName: "foobar", expectedMethods: expectedMethods{
"Greet": nil,
}},
{rcvr: mixed{}, shouldPanic: true},
{rcvr: empty{}, shouldPanic: true},
}
func TestReflect(t *testing.T) {
for _, test := range tests {
t.Run(test.serviceName, func(t *testing.T) {
defer func() {
if err := recover(); err != nil {
if test.shouldPanic {
return
} else {
panic(err)
}
} else if test.shouldPanic {
t.Fatal("test should have panicked")
}
}()
def := restate.Reflect(test.rcvr, test.opts...)
foundMethods := make(map[string]*internal.ServiceHandlerType, len(def.Handlers()))
for k, foundHandler := range def.Handlers() {
t.Run(k, func(t *testing.T) {
foundMethods[k] = foundHandler.HandlerType()
// check for panics
_ = foundHandler.InputPayload()
_ = foundHandler.OutputPayload()
_, err := foundHandler.Call(&state.Context{}, []byte(`""`))
require.NoError(t, err)
})
}
require.Equal(t, test.expectedMethods, foundMethods)
require.Equal(t, test.serviceName, def.Name())
})
}
}
type validObject struct{}
func (validObject) Greet(ctx restate.ObjectContext, _ string) (string, error) {
return "", nil
}
func (validObject) GreetShared(ctx restate.ObjectSharedContext, _ string) (string, error) {
return "", nil
}
func (validObject) NoInput(ctx restate.ObjectContext) (string, error) {
return "", nil
}
func (validObject) NoError(ctx restate.ObjectContext, _ string) string {
return ""
}
func (validObject) NoOutput(ctx restate.ObjectContext, _ string) error {
return nil
}
func (validObject) NoOutputNoError(ctx restate.ObjectContext, _ string) {
}
func (validObject) NoInputNoError(ctx restate.ObjectContext) string {
return ""
}
func (validObject) NoInputNoOutput(ctx restate.ObjectContext) error {
return nil
}
func (validObject) NoInputNoOutputNoError(ctx restate.ObjectContext) {
}
func (validObject) SkipInvalidCtx(ctx context.Context, _ string) (string, error) {
return "", nil
}
func (validObject) SkipInvalidError(ctx restate.ObjectContext, _ string) (error, string) {
return nil, ""
}
func (validObject) skipUnexported(_ string) (string, error) {
return "", nil
}
type validService struct{}
func (validService) Greet(ctx restate.Context, _ string) (string, error) {
return "", nil
}
type namedService struct{}
func (namedService) ServiceName() string {
return "foobar"
}
func (namedService) Greet(ctx restate.Context, _ string) (string, error) {
return "", nil
}
type mixed struct{}
func (mixed) Greet(ctx restate.Context, _ string) (string, error) {
return "", nil
}
func (mixed) GreetShared(ctx restate.ObjectSharedContext, _ string) (string, error) {
return "", nil
}
type empty struct{}