forked from hyperledger/fabric-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mockehserver.go
executable file
·72 lines (60 loc) · 1.61 KB
/
mockehserver.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package mocks
import (
"fmt"
"io"
"sync"
"github.com/golang/protobuf/proto"
pb "github.com/hyperledger/fabric-sdk-go/third_party/github.com/hyperledger/fabric/protos/peer"
)
// MockEventhubServer is a mock event hub server
type MockEventhubServer struct {
sync.RWMutex
disconnErr error
}
// NewMockEventhubServer returns a new MockEventhubServer
func NewMockEventhubServer() *MockEventhubServer {
return new(MockEventhubServer)
}
// Disconnect terminates the stream and returns the given error to the client
func (s *MockEventhubServer) Disconnect(err error) {
s.Lock()
defer s.Unlock()
s.disconnErr = err
}
func (s *MockEventhubServer) disconnectErr() error {
s.RLock()
defer s.RUnlock()
return s.disconnErr
}
// Chat starts a listener on the given chat stream
func (s *MockEventhubServer) Chat(srv pb.Events_ChatServer) error {
for {
signedEvt, err := srv.Recv()
if err == io.EOF || signedEvt == nil {
break
}
err = s.disconnectErr()
if err != nil {
return err
}
var emsg pb.Event
if err := proto.Unmarshal(signedEvt.EventBytes, &emsg); err != nil {
panic(fmt.Sprintf("Error unmarshalling event bytes: %s", err))
}
switch emsg.Event.(type) {
case *pb.Event_Register:
// Send back the same event (which is what the event hub server currently does)
srv.Send(&emsg)
case *pb.Event_Unregister:
// Send back the same event (which is what the event hub server currently does)
srv.Send(&emsg)
default:
panic(fmt.Sprintf("Unsupported message type: %T", emsg))
}
}
return nil
}