-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathevents_test.go
executable file
·179 lines (151 loc) · 4.38 KB
/
events_test.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
*/
package events
import (
"fmt"
"net"
"os"
"sync"
"testing"
"time"
"github.com/openblockchain/obc-peer/events/consumer"
"github.com/openblockchain/obc-peer/events/producer"
ehpb "github.com/openblockchain/obc-peer/protos"
"github.com/spf13/viper"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/grpclog"
)
type Adapter struct {
sync.RWMutex
notfy chan struct{}
count int
}
var peerAddress string
var adapter *Adapter
var obcEHClient *consumer.OpenchainEventsClient
func (a *Adapter) GetInterestedEvents() ([]*ehpb.Interest, error) {
return []*ehpb.Interest{&ehpb.Interest{"block", ehpb.Interest_PROTOBUF}}, nil
//return [] *ehpb.Interest{ &ehpb.InterestedEvent{"block", ehpb.Interest_JSON }}, nil
}
func (a *Adapter) Recv(msg *ehpb.OpenchainEvent) (bool, error) {
//fmt.Printf("Adapter received %v\n", msg.Event)
switch x := msg.Event.(type) {
case *ehpb.OpenchainEvent_Block:
case *ehpb.OpenchainEvent_Generic:
case nil:
// The field is not set.
fmt.Printf("event not set\n")
return false, fmt.Errorf("event not set")
default:
fmt.Printf("unexpected type %T\n", x)
return false, fmt.Errorf("unexpected type %T", x)
}
a.Lock()
a.count--
if a.count <= 0 {
a.notfy <- struct{}{}
}
a.Unlock()
return true, nil
}
func (a *Adapter) Disconnected(err error) {
if err != nil {
fmt.Printf("Error: %s\n", err)
}
}
func createTestBlock() *ehpb.OpenchainEvent {
emsg := producer.CreateBlockEvent(&ehpb.Block{Transactions: []*ehpb.Transaction{}})
return emsg
}
func closeListenerAndSleep(l net.Listener) {
l.Close()
time.Sleep(2 * time.Second)
}
// Test the invocation of a transaction.
func TestReceiveMessage(t *testing.T) {
var err error
adapter.count = 1
emsg := createTestBlock()
if err = producer.Send(emsg); err != nil {
t.Fail()
t.Logf("Error sending message %s", err)
}
select {
case <-adapter.notfy:
case <-time.After(5 * time.Second):
t.Fail()
t.Logf("timed out on messge")
}
}
func BenchmarkMessages(b *testing.B) {
numMessages := 10000
adapter.count = numMessages
var err error
//b.ResetTimer()
for i := 0; i < numMessages; i++ {
go func() {
emsg := createTestBlock()
if err = producer.Send(emsg); err != nil {
b.Fail()
b.Logf("Error sending message %s", err)
}
}()
}
select {
case <-adapter.notfy:
case <-time.After(5 * time.Second):
b.Fail()
b.Logf("timed out on messge")
}
}
func TestMain(m *testing.M) {
SetupTestConfig()
var opts []grpc.ServerOption
if viper.GetBool("peer.tls.enabled") {
creds, err := credentials.NewServerTLSFromFile(viper.GetString("peer.tls.cert.file"), viper.GetString("peer.tls.key.file"))
if err != nil {
grpclog.Fatalf("Failed to generate credentials %v", err)
}
opts = []grpc.ServerOption{grpc.Creds(creds)}
}
grpcServer := grpc.NewServer(opts...)
//use a different address than what we usually use for "peer"
//we override the peerAddress set in chaincode_support.go
peerAddress = "0.0.0.0:40303"
lis, err := net.Listen("tcp", peerAddress)
if err != nil {
fmt.Printf("Error starting events listener %s....not doing tests", err)
return
}
// Register EventHub server
// use a buffer of 100 and blocking timeout
ehServer := producer.NewOpenchainEventsServer(100, 0)
ehpb.RegisterOpenchainEventsServer(grpcServer, ehServer)
fmt.Printf("Starting events server\n")
go grpcServer.Serve(lis)
done := make(chan struct{})
adapter = &Adapter{notfy: done}
obcEHClient = consumer.NewOpenchainEventsClient(peerAddress, adapter)
if err = obcEHClient.Start(); err != nil {
fmt.Printf("could not start chat %s\n", err)
obcEHClient.Stop()
return
}
time.Sleep(2 * time.Second)
os.Exit(m.Run())
}