Skip to content

Commit

Permalink
test: add proxy test
Browse files Browse the repository at this point in the history
  • Loading branch information
siyul-park committed Dec 9, 2023
1 parent 23e8181 commit 17d3de7
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 8 deletions.
2 changes: 1 addition & 1 deletion pkg/plugin/controllx/snippet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TestNewSnippetNode(t *testing.T) {
assert.NoError(t, err)
assert.NotNil(t, n)

_ = n.Close()
assert.NoError(t, n.Close())
}

func TestSnippetNode_Send(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/plugin/controllx/switch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func TestNewSwitchNode(t *testing.T) {
n := NewSwitchNode()
assert.NotNil(t, n)

_ = n.Close()
assert.NoError(t, n.Close())
}

func TestSwitchNode_Send(t *testing.T) {
Expand Down
98 changes: 98 additions & 0 deletions pkg/plugin/networkx/proxy_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package networkx

import (
"context"
"net/http"
"net/http/httptest"
"testing"
"time"

"github.com/go-faker/faker/v4"
"github.com/siyul-park/uniflow/pkg/node"
"github.com/siyul-park/uniflow/pkg/packet"
"github.com/siyul-park/uniflow/pkg/port"
"github.com/siyul-park/uniflow/pkg/primitive"
"github.com/siyul-park/uniflow/pkg/process"
"github.com/stretchr/testify/assert"
)

func TestNewProxyNode(t *testing.T) {
n, err := NewProxyNode(faker.URL())
assert.NoError(t, err)
assert.NotNil(t, n)

assert.NoError(t, n.Close())
}

func TestProxyNode_Send(t *testing.T) {
called := 0
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
called++
}))
defer server.Close()

proxy := NewProxyNode(server.URL)
defer proxy.Close()

io := port.New()
ioPort, _ := proxy.Port(node.PortIO)
ioPort.Link(io)

proc := process.New()
defer proc.Exit(nil)

ioStream := io.Open(proc)

inPayload, _ := primitive.MarshalText(HTTPPayload{
Method: http.MethodGet,
Path: "/",
})
inPck := packet.New(inPayload)

ioStream.Send(inPck)

ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()

select {
case outPck := <-ioStream.Receive():
assert.Equal(t, 1, called)

var outPayload HTTPPayload
err := primitive.Unmarshal(outPck.Payload(), &outPayload)
assert.NoError(t, err)
case <-ctx.Done():
assert.Fail(t, "timeout")
}
}

func BenchmarkProxyNode_Send(b *testing.B) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
}))
defer server.Close()

proxy := NewProxyNode(server.URL)
defer proxy.Close()

io := port.New()
ioPort, _ := proxy.Port(node.PortIO)
ioPort.Link(io)

proc := process.New()
defer proc.Exit(nil)

ioStream := io.Open(proc)

inPayload, _ := primitive.MarshalText(HTTPPayload{
Method: http.MethodGet,
Path: "/",
})
inPck := packet.New(inPayload)

b.ResetTimer()

for i := 0; i < b.N; i++ {
ioStream.Send(inPck)
<-ioStream.Receive()
}
}
2 changes: 1 addition & 1 deletion pkg/plugin/networkx/router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func TestNewRouterNode(t *testing.T) {
n := NewRouterNode()
assert.NotNil(t, n)

_ = n.Close()
assert.NoError(t, n.Close())
}

func TestRouterNode_Send(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/plugin/systemx/reflect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func TestNewReflectNode(t *testing.T) {
})
assert.NotNil(t, n)

_ = n.Close()
assert.NoError(t, n.Close())
}

func TestReflectNode_Send(t *testing.T) {
Expand Down
6 changes: 2 additions & 4 deletions pkg/primitive/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,10 +287,8 @@ func NewMapDecoder(decoder encoding.Decoder[Value, any]) encoding.Decoder[Value,
}

value, ok := s.Get(NewString(tag.alias))
if !ok || reflect.ValueOf(value.Interface()).IsZero() {
if tag.omitempty {
continue
} else {
if !ok {
if !tag.omitempty {
return errors.WithMessage(encoding.ErrUnsupportedValue, fmt.Sprintf("key(%v) is zero value", field.Name))
}
} else if err := decoder.Decode(value, v.Addr().Interface()); err != nil {
Expand Down

0 comments on commit 17d3de7

Please sign in to comment.