Skip to content
This repository was archived by the owner on Apr 24, 2025. It is now read-only.

Commit 6a89e4b

Browse files
committed
proxytest: add Tick function
Signed-off-by: mathetake <[email protected]>
1 parent 8720a65 commit 6a89e4b

File tree

6 files changed

+73
-56
lines changed

6 files changed

+73
-56
lines changed

examples/helloworld/main_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package main
33
import (
44
"strings"
55
"testing"
6-
"time"
76

87
"github.com/stretchr/testify/require"
98

@@ -21,7 +20,8 @@ func TestHelloWorld_OnTick(t *testing.T) {
2120

2221
host.StartVM() // call OnVMStart
2322

24-
time.Sleep(time.Duration(tickMilliseconds) * 4 * time.Millisecond)
23+
assert.Equal(t, tickMilliseconds, host.GetTickPeriod())
24+
host.Tick() // call OnTick
2525

2626
logs := host.GetLogs(types.LogLevelInfo)
2727
require.Greater(t, len(logs), 0)

examples/shared_queue/main_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ package main
1717
import (
1818
"fmt"
1919
"testing"
20-
"time"
2120

2221
"github.com/stretchr/testify/assert"
2322
"github.com/stretchr/testify/require"
@@ -42,9 +41,12 @@ func TestQueue(t *testing.T) {
4241

4342
contextID := host.HttpFilterInitContext()
4443
host.HttpFilterPutRequestHeaders(contextID, nil) // call enqueue
44+
4545
assert.Equal(t, 4, host.GetQueueSize(queueID))
4646

47-
time.Sleep(time.Duration(tickMilliseconds*5) * time.Millisecond)
47+
for i := 0; i < 4; i++ {
48+
host.Tick()
49+
}
4850

4951
logs = host.GetLogs(types.LogLevelInfo)
5052
require.Greater(t, len(logs), 5)

proxytest/http.go

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func newHttpHostEmulator() *httpHostEmulator {
4747
return host
4848
}
4949

50-
// impl host rawhostcall.ProxyWASMHost: delegated from hostEmulator
50+
// impl rawhostcall.ProxyWASMHost: delegated from hostEmulator
5151
func (h *httpHostEmulator) httpHostEmulatorProxyGetBufferBytes(bt types.BufferType, start int, maxSize int,
5252
returnBufferData **byte, returnBufferSize *int) types.Status {
5353
active := proxywasm.VMStateGetActiveContextID()
@@ -76,7 +76,7 @@ func (h *httpHostEmulator) httpHostEmulatorProxyGetBufferBytes(bt types.BufferTy
7676
return types.StatusOK
7777
}
7878

79-
// impl host rawhostcall.ProxyWASMHost: delegated from hostEmulator
79+
// impl rawhostcall.ProxyWASMHost: delegated from hostEmulator
8080
func (h *httpHostEmulator) httpHostEmulatorProxyGetHeaderMapValue(mapType types.MapType, keyData *byte,
8181
keySize int, returnValueData **byte, returnValueSize *int) types.Status {
8282
key := proxywasm.RawBytePtrToString(keyData, keySize)
@@ -109,7 +109,7 @@ func (h *httpHostEmulator) httpHostEmulatorProxyGetHeaderMapValue(mapType types.
109109
return types.StatusNotFound
110110
}
111111

112-
// impl host rawhostcall.ProxyWASMHost
112+
// impl rawhostcall.ProxyWASMHost
113113
func (h *httpHostEmulator) ProxyAddHeaderMapValue(mapType types.MapType, keyData *byte,
114114
keySize int, valueData *byte, valueSize int) types.Status {
115115

@@ -145,7 +145,7 @@ func addMapValue(base [][2]string, key, value string) [][2]string {
145145
return append(base, [2]string{key, value})
146146
}
147147

148-
// impl host rawhostcall.ProxyWASMHost
148+
// impl rawhostcall.ProxyWASMHost
149149
func (h *httpHostEmulator) ProxyReplaceHeaderMapValue(mapType types.MapType, keyData *byte,
150150
keySize int, valueData *byte, valueSize int) types.Status {
151151
key := proxywasm.RawBytePtrToString(keyData, keySize)
@@ -168,7 +168,7 @@ func (h *httpHostEmulator) ProxyReplaceHeaderMapValue(mapType types.MapType, key
168168
return types.StatusOK
169169
}
170170

171-
// impl host rawhostcall.ProxyWASMHost
171+
// impl rawhostcall.ProxyWASMHost
172172
func replaceMapValue(base [][2]string, key, value string) [][2]string {
173173
for i, h := range base {
174174
if h[0] == key {
@@ -180,7 +180,7 @@ func replaceMapValue(base [][2]string, key, value string) [][2]string {
180180
return append(base, [2]string{key, value})
181181
}
182182

183-
// impl host rawhostcall.ProxyWASMHost
183+
// impl rawhostcall.ProxyWASMHost
184184
func (h *httpHostEmulator) ProxyRemoveHeaderMapValue(mapType types.MapType, keyData *byte, keySize int) types.Status {
185185
key := proxywasm.RawBytePtrToString(keyData, keySize)
186186
active := proxywasm.VMStateGetActiveContextID()
@@ -214,7 +214,7 @@ func removeHeaderMapValue(base [][2]string, key string) [][2]string {
214214
return base
215215
}
216216

217-
// impl host rawhostcall.ProxyWASMHost: delegated from hostEmulator
217+
// impl rawhostcall.ProxyWASMHost: delegated from hostEmulator
218218
func (h *httpHostEmulator) httpHostEmulatorProxyGetHeaderMapPairs(mapType types.MapType, returnValueData **byte,
219219
returnValueSize *int) types.Status {
220220
active := proxywasm.VMStateGetActiveContextID()
@@ -239,7 +239,7 @@ func (h *httpHostEmulator) httpHostEmulatorProxyGetHeaderMapPairs(mapType types.
239239
return types.StatusOK
240240
}
241241

242-
// impl host rawhostcall.ProxyWASMHost
242+
// impl rawhostcall.ProxyWASMHost
243243
func (h *httpHostEmulator) ProxySetHeaderMapPairs(mapType types.MapType, mapData *byte, mapSize int) types.Status {
244244
m := proxywasm.DeserializeMap(proxywasm.RawBytePtrToByteSlice(mapData, mapSize))
245245
active := proxywasm.VMStateGetActiveContextID()
@@ -260,15 +260,15 @@ func (h *httpHostEmulator) ProxySetHeaderMapPairs(mapType types.MapType, mapData
260260
return types.StatusOK
261261
}
262262

263-
// impl host rawhostcall.ProxyWASMHost
263+
// impl rawhostcall.ProxyWASMHost
264264
func (h *httpHostEmulator) ProxyContinueStream(types.StreamType) types.Status {
265265
active := proxywasm.VMStateGetActiveContextID()
266266
stream := h.httpStreams[active]
267267
stream.action = types.ActionContinue
268268
return types.StatusOK
269269
}
270270

271-
// impl host rawhostcall.ProxyWASMHost
271+
// impl rawhostcall.ProxyWASMHost
272272
func (h *httpHostEmulator) ProxySendLocalResponse(statusCode uint32,
273273
statusCodeDetailData *byte, statusCodeDetailsSize int, bodyData *byte, bodySize int,
274274
headersData *byte, headersSize int, grpcStatus int32) types.Status {
@@ -284,15 +284,15 @@ func (h *httpHostEmulator) ProxySendLocalResponse(statusCode uint32,
284284
return types.StatusOK
285285
}
286286

287-
// impl host HostEmulator
287+
// impl HostEmulator
288288
func (h *httpHostEmulator) HttpFilterInitContext() (contextID uint32) {
289289
contextID = getNextContextID()
290290
proxywasm.ProxyOnContextCreate(contextID, rootContextID)
291291
h.httpStreams[contextID] = &httpStreamState{action: types.ActionContinue}
292292
return
293293
}
294294

295-
// impl host HostEmulator
295+
// impl HostEmulator
296296
func (h *httpHostEmulator) HttpFilterPutRequestHeaders(contextID uint32, headers [][2]string) {
297297
cs, ok := h.httpStreams[contextID]
298298
if !ok {
@@ -304,7 +304,7 @@ func (h *httpHostEmulator) HttpFilterPutRequestHeaders(contextID uint32, headers
304304
len(headers), false) // TODO: allow for specifying end_of_stream
305305
}
306306

307-
// impl host HostEmulator
307+
// impl HostEmulator
308308
func (h *httpHostEmulator) HttpFilterPutResponseHeaders(contextID uint32, headers [][2]string) {
309309
cs, ok := h.httpStreams[contextID]
310310
if !ok {
@@ -317,7 +317,7 @@ func (h *httpHostEmulator) HttpFilterPutResponseHeaders(contextID uint32, header
317317
len(headers), false) // TODO: allow for specifying end_of_stream
318318
}
319319

320-
// impl host HostEmulator
320+
// impl HostEmulator
321321
func (h *httpHostEmulator) HttpFilterPutRequestTrailers(contextID uint32, headers [][2]string) {
322322
cs, ok := h.httpStreams[contextID]
323323
if !ok {
@@ -328,7 +328,7 @@ func (h *httpHostEmulator) HttpFilterPutRequestTrailers(contextID uint32, header
328328
cs.action = proxywasm.ProxyOnRequestTrailers(contextID, len(headers))
329329
}
330330

331-
// impl host HostEmulator
331+
// impl HostEmulator
332332
func (h *httpHostEmulator) HttpFilterPutResponseTrailers(contextID uint32, headers [][2]string) {
333333
cs, ok := h.httpStreams[contextID]
334334
if !ok {
@@ -339,7 +339,7 @@ func (h *httpHostEmulator) HttpFilterPutResponseTrailers(contextID uint32, heade
339339
cs.action = proxywasm.ProxyOnResponseTrailers(contextID, len(headers))
340340
}
341341

342-
// impl host HostEmulator
342+
// impl HostEmulator
343343
func (h *httpHostEmulator) HttpFilterPutRequestBody(contextID uint32, body []byte) {
344344
cs, ok := h.httpStreams[contextID]
345345
if !ok {
@@ -351,7 +351,7 @@ func (h *httpHostEmulator) HttpFilterPutRequestBody(contextID uint32, body []byt
351351
len(body), false) // TODO: allow for specifying end_of_stream
352352
}
353353

354-
// impl host HostEmulator
354+
// impl HostEmulator
355355
func (h *httpHostEmulator) HttpFilterPutResponseBody(contextID uint32, body []byte) {
356356
cs, ok := h.httpStreams[contextID]
357357
if !ok {
@@ -363,12 +363,12 @@ func (h *httpHostEmulator) HttpFilterPutResponseBody(contextID uint32, body []by
363363
len(body), false) // TODO: allow for specifying end_of_stream
364364
}
365365

366-
// impl host HostEmulator
366+
// impl HostEmulator
367367
func (h *httpHostEmulator) HttpFilterCompleteHttpStream(contextID uint32) {
368368
proxywasm.ProxyOnDone(contextID)
369369
}
370370

371-
// impl host HostEmulator
371+
// impl HostEmulator
372372
func (h *httpHostEmulator) HttpFilterGetCurrentStreamAction(contextID uint32) types.Action {
373373
stream, ok := h.httpStreams[contextID]
374374
if !ok {
@@ -377,7 +377,7 @@ func (h *httpHostEmulator) HttpFilterGetCurrentStreamAction(contextID uint32) ty
377377
return stream.action
378378
}
379379

380-
// impl host HostEmulator
380+
// impl HostEmulator
381381
func (h *httpHostEmulator) HttpFilterGetSentLocalResponse(contextID uint32) *LocalHttpResponse {
382382
return h.httpStreams[contextID].sentLocalResponse
383383
}

proxytest/network.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func newNetworkHostEmulator() *networkHostEmulator {
3737
return host
3838
}
3939

40-
// impl host rawhostcall.ProxyWASMHost: delegated from hostEmulator
40+
// impl rawhostcall.ProxyWASMHost: delegated from hostEmulator
4141
func (n *networkHostEmulator) networkHostEmulatorProxyGetBufferBytes(bt types.BufferType, start int, maxSize int,
4242
returnBufferData **byte, returnBufferSize *int) types.Status {
4343

@@ -67,7 +67,7 @@ func (n *networkHostEmulator) networkHostEmulatorProxyGetBufferBytes(bt types.Bu
6767
return types.StatusOK
6868
}
6969

70-
// impl host HostEmulator
70+
// impl HostEmulator
7171
func (n *networkHostEmulator) NetworkFilterPutUpstreamData(contextID uint32, data []byte) {
7272
stream, ok := n.streamStates[contextID]
7373
if !ok {
@@ -90,7 +90,7 @@ func (n *networkHostEmulator) NetworkFilterPutUpstreamData(contextID uint32, dat
9090
}
9191
}
9292

93-
// impl host HostEmulator
93+
// impl HostEmulator
9494
func (n *networkHostEmulator) NetworkFilterPutDownstreamData(contextID uint32, data []byte) {
9595
stream, ok := n.streamStates[contextID]
9696
if !ok {
@@ -112,7 +112,7 @@ func (n *networkHostEmulator) NetworkFilterPutDownstreamData(contextID uint32, d
112112
}
113113
}
114114

115-
// impl host HostEmulator
115+
// impl HostEmulator
116116
func (n *networkHostEmulator) NetworkFilterInitConnection() (contextID uint32) {
117117
contextID = getNextContextID()
118118
proxywasm.ProxyOnContextCreate(contextID, rootContextID)
@@ -121,17 +121,17 @@ func (n *networkHostEmulator) NetworkFilterInitConnection() (contextID uint32) {
121121
return
122122
}
123123

124-
// impl host HostEmulator
124+
// impl HostEmulator
125125
func (n *networkHostEmulator) NetworkFilterCloseUpstreamConnection(contextID uint32) {
126126
proxywasm.ProxyOnUpstreamConnectionClose(contextID, types.PeerTypeLocal) // peerType will be removed in the next ABI
127127
}
128128

129-
// impl host HostEmulator
129+
// impl HostEmulator
130130
func (n *networkHostEmulator) NetworkFilterCloseDownstreamConnection(contextID uint32) {
131131
proxywasm.ProxyOnDownstreamConnectionClose(contextID, types.PeerTypeLocal) // peerType will be removed in the next ABI
132132
}
133133

134-
// impl host HostEmulator
134+
// impl HostEmulator
135135
func (n *networkHostEmulator) NetworkFilterCompleteConnection(contextID uint32) {
136136
proxywasm.ProxyOnDone(contextID)
137137
delete(n.streamStates, contextID)

proxytest/proxytest.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ type HostEmulator interface {
2323

2424
GetLogs(level types.LogLevel) []string
2525
GetTickPeriod() uint32
26+
Tick()
2627
GetQueueSize(queueID uint32) int
2728

2829
// network
@@ -94,13 +95,13 @@ func getNextContextID() (ret uint32) {
9495
return
9596
}
9697

97-
// impl host HostEmulator
98+
// impl HostEmulator
9899
func (*hostEmulator) Done() {
99100
defer hostMux.Unlock()
100101
defer proxywasm.VMStateReset()
101102
}
102103

103-
// impl host rawhostcall.ProxyWASMHost
104+
// impl rawhostcall.ProxyWASMHost
104105
func (h *hostEmulator) ProxyGetBufferBytes(bt types.BufferType, start int, maxSize int,
105106
returnBufferData **byte, returnBufferSize *int) types.Status {
106107
switch bt {
@@ -115,7 +116,7 @@ func (h *hostEmulator) ProxyGetBufferBytes(bt types.BufferType, start int, maxSi
115116
}
116117
}
117118

118-
// impl host rawhostcall.ProxyWASMHost
119+
// impl rawhostcall.ProxyWASMHost
119120
func (h *hostEmulator) ProxyGetHeaderMapValue(mapType types.MapType, keyData *byte,
120121
keySize int, returnValueData **byte, returnValueSize *int) types.Status {
121122
switch mapType {
@@ -131,7 +132,7 @@ func (h *hostEmulator) ProxyGetHeaderMapValue(mapType types.MapType, keyData *by
131132
}
132133
}
133134

134-
// impl host rawhostcall.ProxyWASMHost
135+
// impl rawhostcall.ProxyWASMHost
135136
func (h *hostEmulator) ProxyGetHeaderMapPairs(mapType types.MapType, returnValueData **byte,
136137
returnValueSize *int) types.Status {
137138
switch mapType {
@@ -145,42 +146,42 @@ func (h *hostEmulator) ProxyGetHeaderMapPairs(mapType types.MapType, returnValue
145146
}
146147
}
147148

148-
// impl host rawhostcall.ProxyWASMHost
149+
// impl rawhostcall.ProxyWASMHost
149150
func (h *hostEmulator) ProxyGetCurrentTimeNanoseconds(returnTime *int64) types.Status {
150151
*returnTime = time.Now().UnixNano()
151152
return types.StatusOK
152153
}
153154

154-
// impl host rawhostcall.ProxyWASMHost
155+
// impl rawhostcall.ProxyWASMHost
155156
func (h *hostEmulator) ProxySetEffectiveContext(contextID uint32) types.Status {
156157
h.effectiveContextID = contextID
157158
return types.StatusOK
158159
}
159160

160-
// impl host rawhostcall.ProxyWASMHost
161+
// impl rawhostcall.ProxyWASMHost
161162
func (h *hostEmulator) ProxySetProperty(*byte, int, *byte, int) types.Status {
162163
panic("unimplemented")
163164
}
164165

165-
// impl host rawhostcall.ProxyWASMHost
166+
// impl rawhostcall.ProxyWASMHost
166167
func (h *hostEmulator) ProxyGetProperty(*byte, int, **byte, *int) types.Status {
167168
log.Printf("ProxyGetProperty not implemented in the host emulator yet")
168169
return 0
169170
}
170171

171-
// impl host rawhostcall.ProxyWASMHost
172+
// impl rawhostcall.ProxyWASMHost
172173
func (h *hostEmulator) ProxyResolveSharedQueue(vmIDData *byte, vmIDSize int, nameData *byte, nameSize int, returnID *uint32) types.Status {
173174
log.Printf("ProxyResolveSharedQueue not implemented in the host emulator yet")
174175
return 0
175176
}
176177

177-
// impl host rawhostcall.ProxyWASMHost
178+
// impl rawhostcall.ProxyWASMHost
178179
func (h *hostEmulator) ProxyCloseStream(streamType types.StreamType) types.Status {
179180
log.Printf("ProxyCloseStream not implemented in the host emulator yet")
180181
return 0
181182
}
182183

183-
// impl host rawhostcall.ProxyWASMHost
184+
// impl rawhostcall.ProxyWASMHost
184185
func (h *hostEmulator) ProxyDone() types.Status {
185186
log.Printf("ProxyDone not implemented in the host emulator yet")
186187
return 0

0 commit comments

Comments
 (0)