Skip to content

Commit a126d13

Browse files
committed
add TestNewLocalView
Signed-off-by: Angelo De Caro <[email protected]>
1 parent d2d4f56 commit a126d13

File tree

2 files changed

+55
-2
lines changed

2 files changed

+55
-2
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
Copyright IBM Corp. All Rights Reserved.
3+
4+
SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
package view_test
8+
9+
import (
10+
"testing"
11+
12+
"github.com/hyperledger-labs/fabric-smart-client/pkg/utils/errors"
13+
"github.com/hyperledger-labs/fabric-smart-client/platform/view/services/view"
14+
view2 "github.com/hyperledger-labs/fabric-smart-client/platform/view/view"
15+
"github.com/stretchr/testify/assert"
16+
"github.com/stretchr/testify/require"
17+
)
18+
19+
type Hello struct {
20+
Message string
21+
}
22+
23+
type HelloView struct {
24+
Hello
25+
}
26+
27+
func (h *HelloView) Call(context view2.Context) (any, error) {
28+
return h.Message, nil
29+
}
30+
31+
type HelloLocalFactory struct{}
32+
33+
func (h *HelloLocalFactory) NewViewWithArg(arg any) (view2.View, error) {
34+
hello, ok := arg.(*Hello)
35+
if !ok || arg == nil {
36+
return nil, errors.New("arg is not a Hello")
37+
}
38+
return &HelloView{Hello: *hello}, nil
39+
}
40+
41+
func TestNewLocalView(t *testing.T) {
42+
registry := view.NewRegistry()
43+
require.NoError(t, registry.RegisterLocalFactory("hello", &HelloLocalFactory{}))
44+
45+
view, err := registry.NewLocalView("hello", &Hello{Message: "hello world"})
46+
require.NoError(t, err)
47+
msgBoxed, err := view.Call(nil)
48+
require.NoError(t, err)
49+
msg, ok := msgBoxed.(string)
50+
assert.True(t, ok)
51+
assert.Equal(t, msg, "hello world")
52+
}

platform/view/view/context.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ type RunViewOptions struct {
1919
AsInitiator bool
2020
Call func(Context) (interface{}, error)
2121
SameContext bool
22-
Timeout time.Duration
22+
// Timeout defines how long a view should run before cancelling its context
23+
Timeout time.Duration
2324
}
2425

2526
// CompileRunViewOptions compiles a set of RunViewOption to a RunViewOptions
@@ -68,7 +69,7 @@ func WithSameContext() RunViewOption {
6869
}
6970
}
7071

71-
// WithTimeout is used to set a timeout
72+
// WithTimeout is used to set a timeout that defines how long a view should run before cancelling its context.
7273
func WithTimeout(timeout time.Duration) RunViewOption {
7374
return func(o *RunViewOptions) error {
7475
o.Timeout = timeout

0 commit comments

Comments
 (0)