@@ -4,14 +4,15 @@ Copyright IBM Corp. All Rights Reserved.
44SPDX-License-Identifier: Apache-2.0
55*/
66
7- package view
7+ package mock
88
99import (
1010 "context"
1111 "sync"
1212
1313 "github.com/hyperledger-labs/fabric-smart-client/pkg/utils/errors"
1414 "github.com/hyperledger-labs/fabric-smart-client/platform/view/services/comm/session"
15+ view2 "github.com/hyperledger-labs/fabric-smart-client/platform/view/services/view"
1516 "github.com/hyperledger-labs/fabric-smart-client/platform/view/view"
1617 "go.opentelemetry.io/otel/trace"
1718 "go.opentelemetry.io/otel/trace/noop"
@@ -25,61 +26,61 @@ type Responders struct {
2526 Channel * session.LocalBidirectionalChannel
2627}
2728
28- type MockContext struct {
29+ type DelegatedContext struct {
2930 Ctx view.Context
3031 responders []* Responders
3132}
3233
33- func (c * MockContext ) StartSpanFrom (context.Context , string , ... trace.SpanStartOption ) (context.Context , trace.Span ) {
34+ func (c * DelegatedContext ) StartSpanFrom (context.Context , string , ... trace.SpanStartOption ) (context.Context , trace.Span ) {
3435 return c .Context (), noop.Span {}
3536}
3637
37- func (c * MockContext ) StartSpan (string , ... trace.SpanStartOption ) trace.Span {
38+ func (c * DelegatedContext ) StartSpan (string , ... trace.SpanStartOption ) trace.Span {
3839 return noop.Span {}
3940}
4041
41- func (c * MockContext ) GetService (v interface {}) (interface {}, error ) {
42+ func (c * DelegatedContext ) GetService (v interface {}) (interface {}, error ) {
4243 return c .Ctx .GetService (v )
4344}
4445
45- func (c * MockContext ) ID () string {
46+ func (c * DelegatedContext ) ID () string {
4647 return c .Ctx .ID ()
4748}
4849
49- func (c * MockContext ) RunView (view view.View , opts ... view.RunViewOption ) (interface {}, error ) {
50+ func (c * DelegatedContext ) RunView (view view.View , opts ... view.RunViewOption ) (interface {}, error ) {
5051 return c .Ctx .RunView (view , opts ... )
5152}
5253
53- func (c * MockContext ) Me () view.Identity {
54+ func (c * DelegatedContext ) Me () view.Identity {
5455 return c .Ctx .Me ()
5556}
5657
57- func (c * MockContext ) IsMe (id view.Identity ) bool {
58+ func (c * DelegatedContext ) IsMe (id view.Identity ) bool {
5859 return c .Ctx .IsMe (id )
5960}
6061
61- func (c * MockContext ) Initiator () view.View {
62+ func (c * DelegatedContext ) Initiator () view.View {
6263 return c .Ctx .Initiator ()
6364}
6465
65- func (c * MockContext ) GetSessionByID (id string , party view.Identity ) (view.Session , error ) {
66+ func (c * DelegatedContext ) GetSessionByID (id string , party view.Identity ) (view.Session , error ) {
6667 // TODO: check among the responders
6768 return c .Ctx .GetSessionByID (id , party )
6869}
6970
70- func (c * MockContext ) Session () view.Session {
71+ func (c * DelegatedContext ) Session () view.Session {
7172 return c .Ctx .Session ()
7273}
7374
74- func (c * MockContext ) Context () context.Context {
75+ func (c * DelegatedContext ) Context () context.Context {
7576 return c .Ctx .Context ()
7677}
7778
78- func (c * MockContext ) OnError (callback func ()) {
79+ func (c * DelegatedContext ) OnError (callback func ()) {
7980 c .Ctx .OnError (callback )
8081}
8182
82- func (c * MockContext ) GetSession (caller view.View , party view.Identity , boundToViews ... view.View ) (view.Session , error ) {
83+ func (c * DelegatedContext ) GetSession (caller view.View , party view.Identity , boundToViews ... view.View ) (view.Session , error ) {
8384 for _ , responder := range c .responders {
8485 if responder .InitiatorView == caller && responder .ResponderID .Equal (party ) {
8586 responder .Lock .RLock ()
@@ -103,7 +104,7 @@ func (c *MockContext) GetSession(caller view.View, party view.Identity, boundToV
103104 }
104105 left := biChannel .LeftSession ()
105106 right := biChannel .RightSession ()
106- RunView (c , responder .ResponderView , view .AsResponder (right ))
107+ view2 . RunView (c , responder .ResponderView , view .AsResponder (right ))
107108
108109 responder .Channel = biChannel
109110 return left , nil
@@ -113,30 +114,10 @@ func (c *MockContext) GetSession(caller view.View, party view.Identity, boundToV
113114 return c .Ctx .GetSession (caller , party )
114115}
115116
116- func (c * MockContext ) RespondToAs (initiator view.View , responder view.Identity , r view.View ) {
117+ func (c * DelegatedContext ) RespondToAs (initiator view.View , responder view.Identity , r view.View ) {
117118 c .responders = append (c .responders , & Responders {
118119 InitiatorView : initiator ,
119120 ResponderID : responder ,
120121 ResponderView : r ,
121122 })
122123}
123-
124- // RunView runs passed view within the passed context and using the passed options in a separate goroutine
125- func RunView (context view.Context , view view.View , opts ... view.RunViewOption ) {
126- defer func () {
127- if r := recover (); r != nil {
128- logger .Debugf ("panic in RunView: %v" , r )
129- }
130- }()
131- go func () {
132- defer func () {
133- if r := recover (); r != nil {
134- logger .Debugf ("panic in RunView: %v" , r )
135- }
136- }()
137- _ , err := context .RunView (view , opts ... )
138- if err != nil {
139- logger .Errorf ("failed to run view: %s" , err )
140- }
141- }()
142- }
0 commit comments