-
Notifications
You must be signed in to change notification settings - Fork 507
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change-Id: I1ff5114c7d64f6aa2d82f2e6c4b0b69d3b1786cc Signed-off-by: Sandra Vrtikapa <[email protected]>
- Loading branch information
Showing
6 changed files
with
302 additions
and
29 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,169 @@ | ||
/* | ||
Copyright SecureKey Technologies Inc. All Rights Reserved. | ||
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
package event | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/context" | ||
"github.com/hyperledger/fabric-sdk-go/pkg/fab/mocks" | ||
) | ||
|
||
func Example() { | ||
|
||
ec, err := New(mockChannelProvider("mychannel")) | ||
if err != nil { | ||
fmt.Println("failed to create client") | ||
} | ||
|
||
registration, notifier, err := ec.RegisterChaincodeEvent("examplecc", "event123") | ||
if err != nil { | ||
fmt.Println("failed to register chaincode event") | ||
} | ||
defer ec.Unregister(registration) | ||
|
||
select { | ||
case ccEvent := <-notifier: | ||
fmt.Printf("received chaincode event %v", ccEvent) | ||
case <-time.After(time.Second * 5): | ||
fmt.Println("timeout while waiting for chaincode event") | ||
} | ||
|
||
// Timeout is expected since there is no event producer | ||
|
||
// Output: timeout while waiting for chaincode event | ||
|
||
} | ||
|
||
func ExampleNew() { | ||
|
||
ctx := mockChannelProvider("mychannel") | ||
|
||
ec, err := New(ctx) | ||
if err != nil { | ||
fmt.Println(err) | ||
} | ||
|
||
ec, err = New(ctx, WithBlockEvents()) | ||
if err != nil { | ||
fmt.Println(err) | ||
} | ||
|
||
if ec != nil { | ||
fmt.Println("event client created") | ||
} else { | ||
fmt.Println("event client is nil") | ||
} | ||
|
||
// Output: event client created | ||
|
||
} | ||
|
||
func ExampleClient_RegisterChaincodeEvent() { | ||
|
||
ec, err := New(mockChannelProvider("mychannel")) | ||
if err != nil { | ||
fmt.Println("failed to create client") | ||
} | ||
|
||
registration, _, err := ec.RegisterChaincodeEvent("examplecc", "event123") | ||
if err != nil { | ||
fmt.Println("failed to register chaincode event") | ||
} | ||
defer ec.Unregister(registration) | ||
|
||
fmt.Println("chaincode event registered successfully") | ||
|
||
// Output: chaincode event registered successfully | ||
|
||
} | ||
|
||
func ExampleClient_RegisterChaincodeEvent_withPayload() { | ||
|
||
// If you require payload for chaincode events you have to use WithBlockEvents() option | ||
ec, err := New(mockChannelProvider("mychannel"), WithBlockEvents()) | ||
if err != nil { | ||
fmt.Println("failed to create client") | ||
} | ||
|
||
registration, _, err := ec.RegisterChaincodeEvent("examplecc", "event123") | ||
if err != nil { | ||
fmt.Println("failed to register chaincode event") | ||
} | ||
defer ec.Unregister(registration) | ||
|
||
fmt.Println("chaincode event registered successfully") | ||
|
||
// Output: chaincode event registered successfully | ||
|
||
} | ||
|
||
func ExampleClient_RegisterTxStatusEvent() { | ||
|
||
ec, err := New(mockChannelProvider("mychannel")) | ||
if err != nil { | ||
fmt.Println("failed to create client") | ||
} | ||
|
||
registration, _, err := ec.RegisterTxStatusEvent("tx123") | ||
if err != nil { | ||
fmt.Println("failed to register tx status event") | ||
} | ||
defer ec.Unregister(registration) | ||
|
||
fmt.Println("tx status event registered successfully") | ||
|
||
// Output: tx status event registered successfully | ||
|
||
} | ||
|
||
func ExampleClient_RegisterBlockEvent() { | ||
|
||
ec, err := New(mockChannelProvider("mychannel"), WithBlockEvents()) | ||
if err != nil { | ||
fmt.Println("failed to create client") | ||
} | ||
|
||
registration, _, err := ec.RegisterBlockEvent() | ||
if err != nil { | ||
fmt.Println("failed to register block event") | ||
} | ||
defer ec.Unregister(registration) | ||
|
||
fmt.Println("block event registered successfully") | ||
|
||
// Output: block event registered successfully | ||
|
||
} | ||
|
||
func ExampleClient_RegisterFilteredBlockEvent() { | ||
|
||
ec, err := New(mockChannelProvider("mychannel")) | ||
if err != nil { | ||
fmt.Println("failed to create client") | ||
} | ||
|
||
registration, _, err := ec.RegisterFilteredBlockEvent() | ||
if err != nil { | ||
fmt.Println("failed to register filtered block event") | ||
} | ||
defer ec.Unregister(registration) | ||
|
||
fmt.Println("filtered block event registered successfully") | ||
|
||
// Output: filtered block event registered successfully | ||
|
||
} | ||
|
||
func mockChannelProvider(channelID string) context.ChannelProvider { | ||
|
||
channelProvider := func() (context.Channel, error) { | ||
return mocks.NewMockChannel(channelID) | ||
} | ||
|
||
return channelProvider | ||
} |
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,106 @@ | ||
/* | ||
Copyright SecureKey Technologies Inc. All Rights Reserved. | ||
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package mocks | ||
|
||
import ( | ||
"github.com/pkg/errors" | ||
|
||
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/context" | ||
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/fab" | ||
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/msp" | ||
mspmocks "github.com/hyperledger/fabric-sdk-go/pkg/msp/test/mockmsp" | ||
) | ||
|
||
//Channel supplies the configuration for channel context client | ||
type Channel struct { | ||
context.Client | ||
discovery fab.DiscoveryService | ||
selection fab.SelectionService | ||
channelService fab.ChannelService | ||
channelID string | ||
} | ||
|
||
//Providers returns core providers | ||
func (c *Channel) Providers() context.Client { | ||
return c | ||
} | ||
|
||
//DiscoveryService returns discovery service | ||
func (c *Channel) DiscoveryService() fab.DiscoveryService { | ||
return c.discovery | ||
} | ||
|
||
//SelectionService returns selection service | ||
func (c *Channel) SelectionService() fab.SelectionService { | ||
return c.selection | ||
} | ||
|
||
//ChannelService returns channel service | ||
func (c *Channel) ChannelService() fab.ChannelService { | ||
return c.channelService | ||
} | ||
|
||
//ChannelID returns channel ID | ||
func (c *Channel) ChannelID() string { | ||
return c.channelID | ||
} | ||
|
||
type mockClientContext struct { | ||
context.Providers | ||
msp.SigningIdentity | ||
} | ||
|
||
//NewMockChannel creates new mock channel | ||
func NewMockChannel(channelID string) (*Channel, error) { | ||
|
||
ctx := &mockClientContext{ | ||
Providers: NewMockProviderContext(), | ||
SigningIdentity: mspmocks.NewMockSigningIdentity("user", "user"), | ||
} | ||
|
||
// Set up mock channel service | ||
chProvider, err := NewMockChannelProvider(ctx) | ||
if err != nil { | ||
return nil, errors.WithMessage(err, "new mock channel provider failed") | ||
} | ||
channelService, err := chProvider.ChannelService(ctx, channelID) | ||
if err != nil { | ||
return nil, errors.WithMessage(err, "failed to create mock channel service") | ||
} | ||
|
||
peers := []fab.Peer{NewMockPeer("Peer1", "http://peer1.com")} | ||
|
||
// Set up mock discovery service | ||
mockDiscovery, err := NewMockDiscoveryProvider(nil, peers) | ||
if err != nil { | ||
return nil, errors.WithMessage(err, "NewMockDiscoveryProvider failed") | ||
} | ||
discoveryService, err := mockDiscovery.CreateDiscoveryService(channelID) | ||
if err != nil { | ||
return nil, errors.WithMessage(err, "failed to create discovery service") | ||
} | ||
|
||
// Set up mock selection service | ||
mockSelection, err := NewMockSelectionProvider(nil, peers) | ||
if err != nil { | ||
return nil, errors.WithMessage(err, "NewMockSelectinProvider failed") | ||
} | ||
selectionService, err := mockSelection.CreateSelectionService("mychannel") | ||
if err != nil { | ||
return nil, errors.WithMessage(err, "failed to create selection service") | ||
} | ||
|
||
channel := &Channel{ | ||
Client: ctx, | ||
selection: selectionService, | ||
discovery: discoveryService, | ||
channelService: channelService, | ||
channelID: channelID, | ||
} | ||
|
||
return channel, nil | ||
} |
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