forked from aws/aws-lambda-runtime-interface-emulator
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathflowtesting.go
More file actions
160 lines (130 loc) · 5.43 KB
/
flowtesting.go
File metadata and controls
160 lines (130 loc) · 5.43 KB
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
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package testdata
import (
"context"
"io"
"io/ioutil"
"net/http"
"go.amzn.com/lambda/appctx"
"go.amzn.com/lambda/core"
"go.amzn.com/lambda/core/statejson"
"go.amzn.com/lambda/interop"
"go.amzn.com/lambda/rapi/rendering"
"go.amzn.com/lambda/telemetry"
"go.amzn.com/lambda/testdata/mockthread"
)
type MockInteropServer struct {
Response []byte
ErrorResponse *interop.ErrorResponse
ResponseContentType string
ActiveInvokeID string
}
// StartAcceptingDirectInvokes
func (i *MockInteropServer) StartAcceptingDirectInvokes() error { return nil }
// SendResponse writes response to a shared memory.
func (i *MockInteropServer) SendResponse(invokeID string, contentType string, reader io.Reader) error {
bytes, err := ioutil.ReadAll(reader)
if err != nil {
return err
}
if len(bytes) > interop.MaxPayloadSize {
return &interop.ErrorResponseTooLarge{
ResponseSize: len(bytes),
MaxResponseSize: interop.MaxPayloadSize,
}
}
i.Response = bytes
i.ResponseContentType = contentType
return nil
}
// SendErrorResponse writes error response to a shared memory and sends GIRD FAULT.
func (i *MockInteropServer) SendErrorResponse(invokeID string, response *interop.ErrorResponse) error {
i.ErrorResponse = response
i.ResponseContentType = response.ContentType
return nil
}
func (i *MockInteropServer) GetCurrentInvokeID() string {
return i.ActiveInvokeID
}
func (i *MockInteropServer) CommitResponse() error { return nil }
// SendRunning sends GIRD RUNNING.
func (i *MockInteropServer) SendRunning(*interop.Running) error { return nil }
// SendDone sends GIRD DONE.
func (i *MockInteropServer) SendDone(*interop.Done) error { return nil }
// SendDoneFail sends GIRD DONEFAIL.
func (i *MockInteropServer) SendDoneFail(*interop.DoneFail) error { return nil }
// StartChan returns Start emitter
func (i *MockInteropServer) StartChan() <-chan *interop.Start { return nil }
// InvokeChan returns Invoke emitter
func (i *MockInteropServer) InvokeChan() <-chan *interop.Invoke { return nil }
// ResetChan returns Reset emitter
func (i *MockInteropServer) ResetChan() <-chan *interop.Reset { return nil }
// ShutdownChan returns Shutdown emitter
func (i *MockInteropServer) ShutdownChan() <-chan *interop.Shutdown { return nil }
// TransportErrorChan emits errors if there was parsing/connection issue
func (i *MockInteropServer) TransportErrorChan() <-chan error { return nil }
func (i *MockInteropServer) Clear() {}
func (i *MockInteropServer) IsResponseSent() bool {
return !(i.Response == nil && i.ErrorResponse == nil)
}
func (i *MockInteropServer) SendRuntimeReady() error { return nil }
func (i *MockInteropServer) SetInternalStateGetter(isd interop.InternalStateGetter) {}
func (m *MockInteropServer) Init(i *interop.Start, invokeTimeoutMs int64) {}
func (m *MockInteropServer) Invoke(w http.ResponseWriter, i *interop.Invoke) error { return nil }
func (m *MockInteropServer) Shutdown(shutdown *interop.Shutdown) *statejson.InternalStateDescription {
return nil
}
// FlowTest provides configuration for tests that involve synchronization flows.
type FlowTest struct {
AppCtx appctx.ApplicationContext
InitFlow core.InitFlowSynchronization
InvokeFlow core.InvokeFlowSynchronization
RegistrationService core.RegistrationService
RenderingService *rendering.EventRenderingService
Runtime *core.Runtime
InteropServer *MockInteropServer
LogsSubscriptionAPI *telemetry.NoOpLogsSubscriptionAPI
CredentialsService core.CredentialsService
}
// ConfigureForInit initialize synchronization gates and states for init.
func (s *FlowTest) ConfigureForInit() {
s.RegistrationService.PreregisterRuntime(s.Runtime)
}
// ConfigureForInvoke initialize synchronization gates and states for invoke.
func (s *FlowTest) ConfigureForInvoke(ctx context.Context, invoke *interop.Invoke) {
s.InteropServer.ActiveInvokeID = invoke.ID
s.InvokeFlow.InitializeBarriers()
s.RenderingService.SetRenderer(rendering.NewInvokeRenderer(ctx, invoke, telemetry.GetCustomerTracingHeader))
}
func (s *FlowTest) ConfigureForInitCaching(token, awsKey, awsSecret, awsSession string) {
s.CredentialsService.SetCredentials(token, awsKey, awsSecret, awsSession)
}
func (s *FlowTest) ConfigureForBlockedInitCaching(token, awsKey, awsSecret, awsSession string) {
s.CredentialsService.SetCredentials(token, awsKey, awsSecret, awsSession)
s.CredentialsService.BlockService()
}
// NewFlowTest returns new FlowTest configuration.
func NewFlowTest() *FlowTest {
appCtx := appctx.NewApplicationContext()
initFlow := core.NewInitFlowSynchronization()
invokeFlow := core.NewInvokeFlowSynchronization()
registrationService := core.NewRegistrationService(initFlow, invokeFlow)
renderingService := rendering.NewRenderingService()
credentialsService := core.NewCredentialsService()
runtime := core.NewRuntime(initFlow, invokeFlow)
runtime.ManagedThread = &mockthread.MockManagedThread{}
interopServer := &MockInteropServer{}
appctx.StoreInteropServer(appCtx, interopServer)
return &FlowTest{
AppCtx: appCtx,
InitFlow: initFlow,
InvokeFlow: invokeFlow,
RegistrationService: registrationService,
RenderingService: renderingService,
LogsSubscriptionAPI: &telemetry.NoOpLogsSubscriptionAPI{},
Runtime: runtime,
InteropServer: interopServer,
CredentialsService: credentialsService,
}
}