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

Commit 6af4e83

Browse files
committed
wip
Signed-off-by: mathetake <[email protected]>
1 parent 77cbb1d commit 6af4e83

File tree

4 files changed

+104
-88
lines changed

4 files changed

+104
-88
lines changed

proxytest/http.go

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -128,26 +128,21 @@ func (h *httpHostEmulator) PutResponseBody(contextID uint32, body []byte) {
128128
}
129129

130130
func (h *httpHostEmulator) CompleteHttpStream(contextID uint32) {
131-
cs, ok := h.contexts[contextID]
132-
if !ok {
133-
log.Fatalf("invalid context id: %d", contextID)
134-
}
135-
136131
proxywasm.ProxyOnDone(contextID)
137132
}
138133

139-
func (h *httpHostEmulator) ProxyGetBufferBytes(bt types.BufferType, start int, maxSize int,
134+
func (h *httpHostEmulator) httpHostEmulatorProxyGetBufferBytes(bt types.BufferType, start int, maxSize int,
140135
returnBufferData **byte, returnBufferSize *int) types.Status {
141-
ctx := h.contexts[h.currentContextID]
136+
active := proxywasm.VMStateGetActiveContextID()
137+
ctx := h.contexts[active]
142138
var buf []byte
143139
switch bt {
144140
case types.BufferTypeHttpRequestBody:
145141
buf = ctx.requestBody
146142
case types.BufferTypeHttpResponseBody:
147143
buf = ctx.requestBody
148144
default:
149-
// delegate to baseHost
150-
return h.getBuffer(bt, start, maxSize, returnBufferData, returnBufferSize)
145+
panic("unreachable: maybe a bug in host emulation")
151146
}
152147

153148
if start >= len(buf) {

proxytest/network.go

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import (
2222
)
2323

2424
type networkHostEmulator struct {
25-
networkFilterStreamStates map[uint32]*streamState
25+
streamStates map[uint32]*streamState
2626
}
2727

2828
type streamState struct {
@@ -31,14 +31,14 @@ type streamState struct {
3131

3232
func newNetworkHostEmulator() *networkHostEmulator {
3333
host := &networkHostEmulator{
34-
networkFilterStreamStates: map[uint32]*streamState{},
34+
streamStates: map[uint32]*streamState{},
3535
}
3636

3737
return host
3838
}
3939

4040
func (n *networkHostEmulator) NetworkFilterPutUpstreamData(contextID uint32, data []byte) {
41-
stream, ok := n.networkFilterStreamStates[contextID]
41+
stream, ok := n.streamStates[contextID]
4242
if !ok {
4343
log.Fatalf("invalid context id: %d", contextID)
4444
}
@@ -60,7 +60,7 @@ func (n *networkHostEmulator) NetworkFilterPutUpstreamData(contextID uint32, dat
6060
}
6161

6262
func (n *networkHostEmulator) NetworkFilterPutDownstreamData(contextID uint32, data []byte) {
63-
stream, ok := n.networkFilterStreamStates[contextID]
63+
stream, ok := n.streamStates[contextID]
6464
if !ok {
6565
log.Fatalf("invalid context id: %d", contextID)
6666
}
@@ -96,5 +96,34 @@ func (n *networkHostEmulator) NetworkFilterCloseDownstreamConnection(contextID u
9696

9797
func (n *networkHostEmulator) NetworkFilterCompleteConnection(contextID uint32) {
9898
proxywasm.ProxyOnDone(contextID)
99-
delete(n.networkFilterStreamStates, contextID)
99+
delete(n.streamStates, contextID)
100+
}
101+
102+
func (n *networkHostEmulator) networkHostEmulatorProxyGetBufferBytes(bt types.BufferType, start int, maxSize int,
103+
returnBufferData **byte, returnBufferSize *int) types.Status {
104+
105+
active := proxywasm.VMStateGetActiveContextID()
106+
stream := n.streamStates[active]
107+
var buf []byte
108+
switch bt {
109+
case types.BufferTypeUpstreamData:
110+
buf = stream.upstream
111+
case types.BufferTypeDownstreamData:
112+
buf = stream.downstream
113+
default:
114+
panic("unreachable: maybe a bug in host emulation")
115+
}
116+
117+
if start >= len(buf) {
118+
log.Printf("start index out of range: %d (start) >= %d ", start, len(buf))
119+
return types.StatusBadArgument
120+
}
121+
122+
*returnBufferData = &buf[start]
123+
if maxSize > len(buf)-start {
124+
*returnBufferSize = len(buf) - start
125+
} else {
126+
*returnBufferSize = maxSize
127+
}
128+
return types.StatusOK
100129
}

proxytest/proxytest.go

Lines changed: 16 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55

66
"github.com/tetratelabs/proxy-wasm-go-sdk/proxywasm"
77
"github.com/tetratelabs/proxy-wasm-go-sdk/proxywasm/rawhostcall"
8+
"github.com/tetratelabs/proxy-wasm-go-sdk/proxywasm/types"
89
)
910

1011
type HostEmulator interface {
@@ -15,6 +16,10 @@ type HostEmulator interface {
1516
IsDispatchCalled(contextID uint32) bool
1617
PutCalloutResponse(contextID uint32, headers, trailers [][2]string, body []byte)
1718

19+
GetLogs(level types.LogLevel) []string
20+
GetTickPeriod() uint32
21+
GetQueueSize(queueID uint32) int
22+
1823
NetworkFilterInitConnection() (contextID uint32)
1924
NetworkFilterPutUpstreamData(contextID uint32, data []byte)
2025
NetworkFilterPutDownstreamData(contextID uint32, data []byte)
@@ -36,9 +41,11 @@ var (
3641
func NewHostEmulator(pluginConfiguration, vmConfiguration []byte) HostEmulator {
3742
root := newRootHostEmulator(pluginConfiguration, vmConfiguration)
3843
network := newNetworkHostEmulator()
44+
http := newHttpHostEmulator()
3945
emulator := &hostEmulator{
4046
root,
4147
network,
48+
http,
4249
}
4350

4451
hostMux.Lock() // acquire the lock of host emulation
@@ -57,75 +64,27 @@ func getNextContextID() (ret uint32) {
5764
type hostEmulator struct {
5865
*rootHostEmulator
5966
*networkHostEmulator
67+
*httpHostEmulator
6068
}
6169

6270
func (*hostEmulator) Done() {
6371
hostMux.Unlock()
6472
proxywasm.VMStateReset()
6573
}
6674

67-
/*
68-
69-
func (n *networkHostEmulator) ProxyGetBufferBytes(bt types.BufferType, start int, maxSize int,
75+
func (h *hostEmulator) ProxyGetBufferBytes(bt types.BufferType, start int, maxSize int,
7076
returnBufferData **byte, returnBufferSize *int) types.Status {
71-
stream := n.streams[n.currentContextID]
72-
var buf []byte
7377
switch bt {
74-
case types.BufferTypeUpstreamData:
75-
buf = stream.upstream
76-
case types.BufferTypeDownstreamData:
77-
buf = stream.downstream
78+
case types.BufferTypePluginConfiguration, types.BufferTypeVMConfiguration:
79+
return h.rootHostEmulatorProxyGetBufferBytes(bt, start, maxSize, returnBufferData, returnBufferSize)
80+
case types.BufferTypeDownstreamData, types.BufferTypeUpstreamData:
81+
return h.networkHostEmulatorProxyGetBufferBytes(bt, start, maxSize, returnBufferData, returnBufferSize)
82+
case types.BufferTypeHttpRequestBody, types.BufferTypeHttpResponseBody:
83+
return h.httpHostEmulatorProxyGetBufferBytes(bt, start, maxSize, returnBufferData, returnBufferSize)
7884
default:
79-
// delegate to baseHost
80-
return n.getBuffer(bt, start, maxSize, returnBufferData, returnBufferSize)
81-
}
82-
83-
if start >= len(buf) {
84-
log.Printf("start index out of range: %d (start) >= %d ", start, len(buf))
85-
return types.StatusBadArgument
86-
}
87-
88-
*returnBufferData = &buf[start]
89-
if maxSize > len(buf)-start {
90-
*returnBufferSize = len(buf) - start
91-
} else {
92-
*returnBufferSize = maxSize
85+
panic("unreachable: maybe a bug in host emulation")
9386
}
94-
return types.StatusOK
9587
}
96-
*/
97-
98-
/*
99-
100-
101-
func (r *rootHostEmulator) ProxyGetBufferBytes(bt types.BufferType, start int, maxSize int,
102-
returnBufferData **byte, returnBufferSize *int) types.Status {
103-
var buf []byte
104-
switch bt {
105-
case types.BufferTypePluginConfiguration:
106-
buf = r.pluginConfiguration
107-
case types.BufferTypeVMConfiguration:
108-
buf = r.vmConfiguration
109-
default:
110-
// delegate to baseHost
111-
return r.getBuffer(bt, start, maxSize, returnBufferData, returnBufferSize)
112-
}
113-
114-
if start >= len(buf) {
115-
log.Printf("start index out of range: %d (start) >= %d ", start, len(buf))
116-
return types.StatusBadArgument
117-
}
118-
119-
*returnBufferData = &buf[start]
120-
if maxSize > len(buf)-start {
121-
*returnBufferSize = len(buf) - start
122-
} else {
123-
*returnBufferSize = maxSize
124-
}
125-
return types.StatusOK
126-
}
127-
128-
*/
12988

13089
/*
13190

proxytest/root.go

Lines changed: 50 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package proxytest
1616

1717
import (
1818
"log"
19+
"time"
1920

2021
"github.com/tetratelabs/proxy-wasm-go-sdk/proxywasm"
2122
"github.com/tetratelabs/proxy-wasm-go-sdk/proxywasm/rawhostcall"
@@ -79,20 +80,14 @@ func (r *rootHostEmulator) ProxyLog(logLevel types.LogLevel, messageData *byte,
7980
return types.StatusOK
8081
}
8182

82-
func (r *rootHostEmulator) GetLogs(level types.LogLevel) []string {
83-
if level >= types.LogLevelMax {
84-
log.Fatalf("invalid log level: %d", level)
85-
}
86-
return r.logs[level]
87-
}
88-
8983
func (r *rootHostEmulator) ProxySetTickPeriodMilliseconds(period uint32) types.Status {
9084
r.tickPeriod = period
91-
return types.StatusOK
92-
}
9385

94-
func (r *rootHostEmulator) GetTickPeriod() uint32 {
95-
return r.tickPeriod
86+
go func() {
87+
time.Sleep(time.Millisecond * time.Duration(r.tickPeriod))
88+
proxywasm.ProxyOnTick(rootContextID)
89+
}()
90+
return types.StatusOK
9691
}
9792

9893
func (r *rootHostEmulator) ProxyRegisterSharedQueue(nameData *byte, nameSize int, returnID *uint32) types.Status {
@@ -135,15 +130,12 @@ func (r *rootHostEmulator) ProxyEnqueueSharedQueue(queueID uint32, valueData *by
135130

136131
r.queues[queueID] = append(queue, proxywasm.RawBytePtrToByteSlice(valueData, valueSize))
137132

138-
// TODO: should call OnQueueReady
139-
133+
// note that this behavior is not accurate for some old host implementations:
134+
// see: https://github.com/proxy-wasm/proxy-wasm-cpp-host/pull/36
135+
proxywasm.ProxyOnQueueReady(rootContextID, queueID) // Note that this behavior is not accurate on Istio before 1.8.x
140136
return types.StatusOK
141137
}
142138

143-
func (r *rootHostEmulator) GetQueueSize(queueID uint32) int {
144-
return len(r.queues[queueID])
145-
}
146-
147139
func (r *rootHostEmulator) ProxyGetSharedData(keyData *byte, keySize int,
148140
returnValueData **byte, returnValueSize *int, returnCas *uint32) types.Status {
149141
key := proxywasm.RawBytePtrToString(keyData, keySize)
@@ -242,6 +234,47 @@ func (r *rootHostEmulator) ProxyHttpCall(upstreamData *byte, upstreamSize int, h
242234
return types.StatusOK
243235
}
244236

237+
func (r *rootHostEmulator) rootHostEmulatorProxyGetBufferBytes(bt types.BufferType, start int, maxSize int,
238+
returnBufferData **byte, returnBufferSize *int) types.Status {
239+
var buf []byte
240+
switch bt {
241+
case types.BufferTypePluginConfiguration:
242+
buf = r.pluginConfiguration
243+
case types.BufferTypeVMConfiguration:
244+
buf = r.vmConfiguration
245+
default:
246+
panic("unreachable: maybe a bug in host emulation")
247+
}
248+
249+
if start >= len(buf) {
250+
log.Printf("start index out of range: %d (start) >= %d ", start, len(buf))
251+
return types.StatusBadArgument
252+
}
253+
254+
*returnBufferData = &buf[start]
255+
if maxSize > len(buf)-start {
256+
*returnBufferSize = len(buf) - start
257+
} else {
258+
*returnBufferSize = maxSize
259+
}
260+
return types.StatusOK
261+
}
262+
263+
func (r *rootHostEmulator) GetLogs(level types.LogLevel) []string {
264+
if level >= types.LogLevelMax {
265+
log.Fatalf("invalid log level: %d", level)
266+
}
267+
return r.logs[level]
268+
}
269+
270+
func (r *rootHostEmulator) GetTickPeriod() uint32 {
271+
return r.tickPeriod
272+
}
273+
274+
func (r *rootHostEmulator) GetQueueSize(queueID uint32) int {
275+
return len(r.queues[queueID])
276+
}
277+
245278
func (r *rootHostEmulator) IsDispatchCalled(contextID uint32) bool {
246279
return proxywasm.VMStateRegisteredCallback(rootContextID, contextID)
247280
}

0 commit comments

Comments
 (0)