Skip to content

Commit 4dc372e

Browse files
author
Jason Yellick
committed
Mock testing infrastructure enhancements
One known piece of common mock infrastructure is the blockcutter.Receiver and multichain.ConsenterSupport used in both Solo, and Kafka tests. This changeset factors these structures out of the solo tests and into the mock infrastructure. Because of dependency cycles which were created, it also breaks the mocks package into subpackages per mock type. Change-Id: I924cf18d18ba9e6b1e9853ace105e1c4703ac4aa Signed-off-by: Jason Yellick <[email protected]>
1 parent fcd00a1 commit 4dc372e

File tree

8 files changed

+273
-137
lines changed

8 files changed

+273
-137
lines changed

orderer/common/blockcutter/blockcutter_test.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121
"testing"
2222

2323
"github.com/hyperledger/fabric/orderer/common/filter"
24-
"github.com/hyperledger/fabric/orderer/mocks"
24+
mocksharedconfig "github.com/hyperledger/fabric/orderer/mocks/sharedconfig"
2525
cb "github.com/hyperledger/fabric/protos/common"
2626
)
2727

@@ -74,7 +74,7 @@ var unmatchedTx = &cb.Envelope{Payload: []byte("UNMATCHED")}
7474
func TestNormalBatch(t *testing.T) {
7575
filters := getFilters()
7676
batchSize := 2
77-
r := NewReceiverImpl(&mocks.SharedConfigManager{BatchSizeVal: batchSize}, filters)
77+
r := NewReceiverImpl(&mocksharedconfig.Manager{BatchSizeVal: batchSize}, filters)
7878

7979
batches, committers, ok := r.Ordered(goodTx)
8080

@@ -101,7 +101,7 @@ func TestNormalBatch(t *testing.T) {
101101
func TestBadMessageInBatch(t *testing.T) {
102102
filters := getFilters()
103103
batchSize := 2
104-
r := NewReceiverImpl(&mocks.SharedConfigManager{BatchSizeVal: batchSize}, filters)
104+
r := NewReceiverImpl(&mocksharedconfig.Manager{BatchSizeVal: batchSize}, filters)
105105

106106
batches, committers, ok := r.Ordered(badTx)
107107

@@ -137,7 +137,7 @@ func TestBadMessageInBatch(t *testing.T) {
137137
func TestUnmatchedMessageInBatch(t *testing.T) {
138138
filters := getFilters()
139139
batchSize := 2
140-
r := NewReceiverImpl(&mocks.SharedConfigManager{BatchSizeVal: batchSize}, filters)
140+
r := NewReceiverImpl(&mocksharedconfig.Manager{BatchSizeVal: batchSize}, filters)
141141

142142
batches, committers, ok := r.Ordered(unmatchedTx)
143143

@@ -173,7 +173,7 @@ func TestUnmatchedMessageInBatch(t *testing.T) {
173173
func TestIsolatedEmptyBatch(t *testing.T) {
174174
filters := getFilters()
175175
batchSize := 2
176-
r := NewReceiverImpl(&mocks.SharedConfigManager{BatchSizeVal: batchSize}, filters)
176+
r := NewReceiverImpl(&mocksharedconfig.Manager{BatchSizeVal: batchSize}, filters)
177177

178178
batches, committers, ok := r.Ordered(isolatedTx)
179179

@@ -197,7 +197,7 @@ func TestIsolatedEmptyBatch(t *testing.T) {
197197
func TestIsolatedPartialBatch(t *testing.T) {
198198
filters := getFilters()
199199
batchSize := 2
200-
r := NewReceiverImpl(&mocks.SharedConfigManager{BatchSizeVal: batchSize}, filters)
200+
r := NewReceiverImpl(&mocksharedconfig.Manager{BatchSizeVal: batchSize}, filters)
201201

202202
batches, committers, ok := r.Ordered(goodTx)
203203

+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/*
2+
Copyright IBM Corp. 2016 All Rights Reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package mocks
18+
19+
import (
20+
"github.com/hyperledger/fabric/orderer/common/filter"
21+
cb "github.com/hyperledger/fabric/protos/common"
22+
)
23+
24+
import (
25+
"github.com/op/go-logging"
26+
)
27+
28+
var logger = logging.MustGetLogger("orderer/mocks/blockcutter")
29+
30+
// Receiver mocks the blockcutter.Receiver interface
31+
type Receiver struct {
32+
// QueueNext causes Ordered returns nil false when not set to true
33+
QueueNext bool
34+
35+
// IsolatedTx causes Ordered returns [][]{curBatch, []{newTx}}, true when set to true
36+
IsolatedTx bool
37+
38+
// CutNext causes Ordered returns [][]{append(curBatch, newTx)}, true when set to true
39+
CutNext bool
40+
41+
// CurBatch is the currently outstanding messages in the batch
42+
CurBatch []*cb.Envelope
43+
44+
// Block is a channel which is read from before returning from Ordered, it is useful for synchronization
45+
// If you do not wish synchronization for whatever reason, simply close the channel
46+
Block chan struct{}
47+
}
48+
49+
// NewReceiver returns the mock blockcutter.Receiver implemenation
50+
func NewReceiver() *Receiver {
51+
return &Receiver{
52+
QueueNext: true,
53+
IsolatedTx: false,
54+
CutNext: false,
55+
Block: make(chan struct{}),
56+
}
57+
}
58+
59+
func noopCommitters(size int) []filter.Committer {
60+
res := make([]filter.Committer, size)
61+
for i := range res {
62+
res[i] = filter.NoopCommitter
63+
}
64+
return res
65+
}
66+
67+
// Ordered will add or cut the batch according to the state of Receiver, it blocks reading from Block on return
68+
func (mbc *Receiver) Ordered(env *cb.Envelope) ([][]*cb.Envelope, [][]filter.Committer, bool) {
69+
defer func() {
70+
<-mbc.Block
71+
}()
72+
73+
if !mbc.QueueNext {
74+
logger.Debugf("Not queueing message")
75+
return nil, nil, false
76+
}
77+
78+
if mbc.IsolatedTx {
79+
logger.Debugf("Receiver: Returning dual batch")
80+
res := [][]*cb.Envelope{mbc.CurBatch, []*cb.Envelope{env}}
81+
mbc.CurBatch = nil
82+
return res, [][]filter.Committer{noopCommitters(len(res[0])), noopCommitters(len(res[1]))}, true
83+
}
84+
85+
mbc.CurBatch = append(mbc.CurBatch, env)
86+
87+
if mbc.CutNext {
88+
logger.Debugf("Returning regular batch")
89+
res := [][]*cb.Envelope{mbc.CurBatch}
90+
mbc.CurBatch = nil
91+
return res, [][]filter.Committer{noopCommitters(len(res))}, true
92+
}
93+
94+
logger.Debugf("Appending to batch")
95+
return nil, nil, true
96+
}
97+
98+
// Cut terminates the current batch, returning it
99+
func (mbc *Receiver) Cut() ([]*cb.Envelope, []filter.Committer) {
100+
logger.Debugf("Cutting batch")
101+
res := mbc.CurBatch
102+
mbc.CurBatch = nil
103+
return res, noopCommitters(len(res))
104+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
Copyright IBM Corp. 2016 All Rights Reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package mocks
18+
19+
import (
20+
"testing"
21+
22+
"github.com/hyperledger/fabric/orderer/common/blockcutter"
23+
)
24+
25+
func TestBlockCutterInterface(t *testing.T) {
26+
_ = blockcutter.Receiver(NewReceiver())
27+
}
+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
Copyright IBM Corp. 2016 All Rights Reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package multichain
18+
19+
import (
20+
"github.com/hyperledger/fabric/orderer/common/blockcutter"
21+
"github.com/hyperledger/fabric/orderer/common/filter"
22+
"github.com/hyperledger/fabric/orderer/common/sharedconfig"
23+
mockblockcutter "github.com/hyperledger/fabric/orderer/mocks/blockcutter"
24+
mocksharedconfig "github.com/hyperledger/fabric/orderer/mocks/sharedconfig"
25+
cb "github.com/hyperledger/fabric/protos/common"
26+
27+
"github.com/op/go-logging"
28+
)
29+
30+
var logger = logging.MustGetLogger("orderer/mocks/multichain")
31+
32+
// ConsenterSupport is used to mock the multichain.ConsenterSupport interface
33+
// Whenever a block is written, it writes to the Batches channel to allow for synchronization
34+
type ConsenterSupport struct {
35+
// SharedConfigVal is the value returned by SharedConfig()
36+
SharedConfigVal *mocksharedconfig.Manager
37+
38+
// BlockCutterVal is the value returned by BlockCutter()
39+
BlockCutterVal *mockblockcutter.Receiver
40+
41+
// Batches is the channel which WriteBlock writes data to
42+
Batches chan []*cb.Envelope
43+
}
44+
45+
// BlockCutter returns BlockCutterVal
46+
func (mcs *ConsenterSupport) BlockCutter() blockcutter.Receiver {
47+
return mcs.BlockCutterVal
48+
}
49+
50+
// SharedConfig returns SharedConfigVal
51+
func (mcs *ConsenterSupport) SharedConfig() sharedconfig.Manager {
52+
return mcs.SharedConfigVal
53+
}
54+
55+
// WriteBlock writes data to the Batches channel
56+
func (mcs *ConsenterSupport) WriteBlock(data []*cb.Envelope, metadata [][]byte, committers []filter.Committer) {
57+
logger.Debugf("mockWriter: attempting to write batch")
58+
mcs.Batches <- data
59+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
Copyright IBM Corp. 2016 All Rights Reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package multichain
18+
19+
import (
20+
"testing"
21+
22+
"github.com/hyperledger/fabric/orderer/multichain"
23+
)
24+
25+
func TestConsenterSupportInterface(t *testing.T) {
26+
_ = multichain.ConsenterSupport(&ConsenterSupport{})
27+
}

orderer/mocks/sharedconfig.go orderer/mocks/sharedconfig/sharedconfig.go

+9-10
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
package mocks
17+
package sharedconfig
1818

19-
// MockSharedConfigManager is a mock implementation of sharedconfig.Manager
20-
type SharedConfigManager struct {
19+
// Manager is a mock implementation of sharedconfig.Manager
20+
type Manager struct {
2121
// ConsensusTypeVal is returned as the result of ConsensusType()
2222
ConsensusTypeVal string
2323
// BatchSizeVal is returned as the result of BatchSize()
@@ -26,18 +26,17 @@ type SharedConfigManager struct {
2626
ChainCreatorsVal []string
2727
}
2828

29-
// ConsensusType returns the configured consensus type
30-
func (scm *SharedConfigManager) ConsensusType() string {
29+
// ConsensusType returns the ConsensusTypeVal
30+
func (scm *Manager) ConsensusType() string {
3131
return scm.ConsensusTypeVal
3232
}
3333

34-
// BatchSize returns the maximum number of messages to include in a block
35-
func (scm *SharedConfigManager) BatchSize() int {
34+
// BatchSize returns the BatchSizeVal
35+
func (scm *Manager) BatchSize() int {
3636
return scm.BatchSizeVal
3737
}
3838

39-
// ChainCreators returns the policy names which are allowed for chain creation
40-
// This field is only set for the system ordering chain
41-
func (scm *SharedConfigManager) ChainCreators() []string {
39+
// ChainCreators returns the ChainCreatorsVal
40+
func (scm *Manager) ChainCreators() []string {
4241
return scm.ChainCreatorsVal
4342
}

orderer/mocks/sharedconfig_test.go orderer/mocks/sharedconfig/sharedconfig_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
package mocks
17+
package sharedconfig
1818

1919
import (
2020
"testing"
@@ -23,5 +23,5 @@ import (
2323
)
2424

2525
func TestSharedConfigInterface(t *testing.T) {
26-
_ = sharedconfig.Manager(&SharedConfigManager{})
26+
_ = sharedconfig.Manager(&Manager{})
2727
}

0 commit comments

Comments
 (0)