-
Notifications
You must be signed in to change notification settings - Fork 8.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 <jyellick@us.ibm.com>
- Loading branch information
Jason Yellick
committed
Dec 12, 2016
1 parent
fcd00a1
commit 4dc372e
Showing
8 changed files
with
273 additions
and
137 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
/* | ||
Copyright IBM Corp. 2016 All Rights Reserved. | ||
Licensed 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 mocks | ||
|
||
import ( | ||
"github.com/hyperledger/fabric/orderer/common/filter" | ||
cb "github.com/hyperledger/fabric/protos/common" | ||
) | ||
|
||
import ( | ||
"github.com/op/go-logging" | ||
) | ||
|
||
var logger = logging.MustGetLogger("orderer/mocks/blockcutter") | ||
|
||
// Receiver mocks the blockcutter.Receiver interface | ||
type Receiver struct { | ||
// QueueNext causes Ordered returns nil false when not set to true | ||
QueueNext bool | ||
|
||
// IsolatedTx causes Ordered returns [][]{curBatch, []{newTx}}, true when set to true | ||
IsolatedTx bool | ||
|
||
// CutNext causes Ordered returns [][]{append(curBatch, newTx)}, true when set to true | ||
CutNext bool | ||
|
||
// CurBatch is the currently outstanding messages in the batch | ||
CurBatch []*cb.Envelope | ||
|
||
// Block is a channel which is read from before returning from Ordered, it is useful for synchronization | ||
// If you do not wish synchronization for whatever reason, simply close the channel | ||
Block chan struct{} | ||
} | ||
|
||
// NewReceiver returns the mock blockcutter.Receiver implemenation | ||
func NewReceiver() *Receiver { | ||
return &Receiver{ | ||
QueueNext: true, | ||
IsolatedTx: false, | ||
CutNext: false, | ||
Block: make(chan struct{}), | ||
} | ||
} | ||
|
||
func noopCommitters(size int) []filter.Committer { | ||
res := make([]filter.Committer, size) | ||
for i := range res { | ||
res[i] = filter.NoopCommitter | ||
} | ||
return res | ||
} | ||
|
||
// Ordered will add or cut the batch according to the state of Receiver, it blocks reading from Block on return | ||
func (mbc *Receiver) Ordered(env *cb.Envelope) ([][]*cb.Envelope, [][]filter.Committer, bool) { | ||
defer func() { | ||
<-mbc.Block | ||
}() | ||
|
||
if !mbc.QueueNext { | ||
logger.Debugf("Not queueing message") | ||
return nil, nil, false | ||
} | ||
|
||
if mbc.IsolatedTx { | ||
logger.Debugf("Receiver: Returning dual batch") | ||
res := [][]*cb.Envelope{mbc.CurBatch, []*cb.Envelope{env}} | ||
mbc.CurBatch = nil | ||
return res, [][]filter.Committer{noopCommitters(len(res[0])), noopCommitters(len(res[1]))}, true | ||
} | ||
|
||
mbc.CurBatch = append(mbc.CurBatch, env) | ||
|
||
if mbc.CutNext { | ||
logger.Debugf("Returning regular batch") | ||
res := [][]*cb.Envelope{mbc.CurBatch} | ||
mbc.CurBatch = nil | ||
return res, [][]filter.Committer{noopCommitters(len(res))}, true | ||
} | ||
|
||
logger.Debugf("Appending to batch") | ||
return nil, nil, true | ||
} | ||
|
||
// Cut terminates the current batch, returning it | ||
func (mbc *Receiver) Cut() ([]*cb.Envelope, []filter.Committer) { | ||
logger.Debugf("Cutting batch") | ||
res := mbc.CurBatch | ||
mbc.CurBatch = nil | ||
return res, noopCommitters(len(res)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
Copyright IBM Corp. 2016 All Rights Reserved. | ||
Licensed 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 mocks | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/hyperledger/fabric/orderer/common/blockcutter" | ||
) | ||
|
||
func TestBlockCutterInterface(t *testing.T) { | ||
_ = blockcutter.Receiver(NewReceiver()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
Copyright IBM Corp. 2016 All Rights Reserved. | ||
Licensed 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 multichain | ||
|
||
import ( | ||
"github.com/hyperledger/fabric/orderer/common/blockcutter" | ||
"github.com/hyperledger/fabric/orderer/common/filter" | ||
"github.com/hyperledger/fabric/orderer/common/sharedconfig" | ||
mockblockcutter "github.com/hyperledger/fabric/orderer/mocks/blockcutter" | ||
mocksharedconfig "github.com/hyperledger/fabric/orderer/mocks/sharedconfig" | ||
cb "github.com/hyperledger/fabric/protos/common" | ||
|
||
"github.com/op/go-logging" | ||
) | ||
|
||
var logger = logging.MustGetLogger("orderer/mocks/multichain") | ||
|
||
// ConsenterSupport is used to mock the multichain.ConsenterSupport interface | ||
// Whenever a block is written, it writes to the Batches channel to allow for synchronization | ||
type ConsenterSupport struct { | ||
// SharedConfigVal is the value returned by SharedConfig() | ||
SharedConfigVal *mocksharedconfig.Manager | ||
|
||
// BlockCutterVal is the value returned by BlockCutter() | ||
BlockCutterVal *mockblockcutter.Receiver | ||
|
||
// Batches is the channel which WriteBlock writes data to | ||
Batches chan []*cb.Envelope | ||
} | ||
|
||
// BlockCutter returns BlockCutterVal | ||
func (mcs *ConsenterSupport) BlockCutter() blockcutter.Receiver { | ||
return mcs.BlockCutterVal | ||
} | ||
|
||
// SharedConfig returns SharedConfigVal | ||
func (mcs *ConsenterSupport) SharedConfig() sharedconfig.Manager { | ||
return mcs.SharedConfigVal | ||
} | ||
|
||
// WriteBlock writes data to the Batches channel | ||
func (mcs *ConsenterSupport) WriteBlock(data []*cb.Envelope, metadata [][]byte, committers []filter.Committer) { | ||
logger.Debugf("mockWriter: attempting to write batch") | ||
mcs.Batches <- data | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
Copyright IBM Corp. 2016 All Rights Reserved. | ||
Licensed 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 multichain | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/hyperledger/fabric/orderer/multichain" | ||
) | ||
|
||
func TestConsenterSupportInterface(t *testing.T) { | ||
_ = multichain.ConsenterSupport(&ConsenterSupport{}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.