-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinvoke_test.go
164 lines (142 loc) · 3.72 KB
/
invoke_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
//nolint:forbidigo,funlen
package main
import (
"context"
"fmt"
"log"
"net"
"net/rpc"
"testing"
"time"
"github.com/aws/aws-lambda-go/lambda/messages"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)
type MockFunction struct {
mock.Mock
}
func (fn *MockFunction) setValues() messages.InvokeResponse {
args := fn.Called()
return args.Get(0).(messages.InvokeResponse) //nolint:forcetypeassert
}
func (fn *MockFunction) Invoke(_ *messages.InvokeRequest, response *messages.InvokeResponse) error { //nolint:unparam
responseNew := fn.setValues()
response.Error = responseNew.Error
response.Payload = responseNew.Payload
return nil
}
func mustStartRPCServer(ctx context.Context, service any, address string) {
if err := rpc.Register(service); err != nil {
panic(fmt.Sprintf("failed to register RPC service: %v", err))
}
listener, err := net.Listen("tcp", address)
defer func() {
if err := listener.Close(); err != nil {
log.Printf("failed to close listener: %v", err)
}
}()
if err != nil { //nolint:wsl
panic(fmt.Sprintf("Failed to listen: %v", err))
}
fmt.Printf("Server is running on %s\n", address) //nolint:forbidigo
go func() {
for {
conn, err := listener.Accept()
if err != nil {
if ctx.Err() != nil {
log.Println("ctx.Err(), Server closed")
return
}
log.Println("Accept error:", err)
}
rpc.ServeConn(conn)
}
}()
<-ctx.Done()
fmt.Println("Server is shutting down")
}
func TestLambdaRPC_Invoke(t *testing.T) { //nolint:nolintlint,paralleltest,cyclop
// run test RPC client
mockService := new(MockFunction)
ctx, cancel := context.WithCancel(context.Background())
defer func() {
cancel()
fmt.Println("Cancelled")
}()
go mustStartRPCServer(ctx, mockService, "localhost:8000")
tests := map[string]struct {
mockReturn func()
inputAddress string
serviceMethod string
executionLimit time.Duration
input []byte
expectedOutput messages.InvokeResponse
expectError bool
}{
"successful invocation": {
mockReturn: func() {
mockService.On("setValues").
Return(
messages.InvokeResponse{
Payload: []byte("response"),
},
).
Once()
},
inputAddress: "localhost:8000",
serviceMethod: "MockFunction.Invoke",
executionLimit: time.Second * 5,
input: []byte("test"),
expectedOutput: messages.InvokeResponse{
Payload: []byte("response"),
},
expectError: false,
},
"invalid inputAddress": {
mockReturn: func() {},
inputAddress: "localhost:3000",
serviceMethod: "MockFunction.Invoke",
executionLimit: time.Second * 5,
input: []byte("test"),
expectedOutput: messages.InvokeResponse{},
expectError: true,
},
"invalid service method": {
mockReturn: func() {},
inputAddress: "localhost:8000",
serviceMethod: "MockFunction.DoesNotExist",
executionLimit: time.Second * 5,
input: []byte("test"),
expectedOutput: messages.InvokeResponse{},
expectError: true,
},
"invalid service": {
mockReturn: func() {},
inputAddress: "localhost:8000",
serviceMethod: "DoesNotExist.methodName",
executionLimit: time.Second * 5,
input: []byte("test"),
expectedOutput: messages.InvokeResponse{},
expectError: true,
},
}
for name, tc := range tests { //nolint:paralleltest
t.Run(
name, func(t *testing.T) {
tc.mockReturn()
lambdaRPC := NewLambdaLambdaRPCClient(
tc.inputAddress,
tc.executionLimit,
WithServiceMethod(tc.serviceMethod),
)
output, err := lambdaRPC.Invoke(tc.input)
assert.Equal(t, tc.expectedOutput, output)
if tc.expectError {
assert.Error(t, err)
} else {
assert.NoError(t, err)
}
},
)
}
}