-
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.
[FAB-8571] parent context opts integration test
Change-Id: I56c8777ef6a1996e6f9e9a89726c3a22b67ef56b Signed-off-by: Sudesh Shetty <sudesh.shetty@securekey.com>
- Loading branch information
1 parent
1309704
commit 05c52e8
Showing
1 changed file
with
79 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* | ||
Copyright SecureKey Technologies Inc. All Rights Reserved. | ||
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package sdk | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
"time" | ||
|
||
"github.com/hyperledger/fabric-sdk-go/pkg/client/channel" | ||
"github.com/hyperledger/fabric-sdk-go/pkg/client/ledger" | ||
"github.com/hyperledger/fabric-sdk-go/pkg/client/resmgmt" | ||
"github.com/hyperledger/fabric-sdk-go/pkg/context" | ||
"github.com/hyperledger/fabric-sdk-go/pkg/fabsdk" | ||
"github.com/hyperledger/fabric-sdk-go/test/integration" | ||
) | ||
|
||
//TestParentContext tests to make sure external grpc context can be passed as a parent context to highlevel functions | ||
func TestParentContext(t *testing.T) { | ||
|
||
// Using shared SDK instance to increase test speed. | ||
sdk := mainSDK | ||
target := mainTestSetup.Targets[0] | ||
chaincodeID := mainChaincodeID | ||
|
||
//prepare contexts | ||
org1AdminClientContext := sdk.Context(fabsdk.WithUser(org1AdminUser), fabsdk.WithOrg(org1Name)) | ||
org1AdminChannelContext := sdk.ChannelContext(mainTestSetup.ChannelID, fabsdk.WithUser(org1AdminUser), fabsdk.WithOrg(org1Name)) | ||
|
||
//prepare context | ||
ctx, err := org1AdminClientContext() | ||
if err != nil { | ||
t.Fatal("failed to get client context") | ||
} | ||
|
||
//get parent context and cancel | ||
parentContext, cancel := context.NewRequest(ctx, context.WithTimeout(20*time.Second)) | ||
//Cancel in advance - to make sure test fails with 'context cancelled' error | ||
cancel() | ||
|
||
// Resource management client | ||
resClient, err := resmgmt.New(org1AdminClientContext) | ||
if err != nil { | ||
t.Fatalf("Failed to create new resource management client: %s", err) | ||
} | ||
|
||
_, err = resClient.QueryChannels(resmgmt.WithTargetURLs(target), resmgmt.WithParentContext(parentContext)) | ||
if err == nil && !strings.Contains(err.Error(), "context canceled") { | ||
t.Fatalf("expected context cancelled error but got: %v", err) | ||
} | ||
|
||
// Channel client | ||
chClient, err := channel.New(org1AdminChannelContext) | ||
if err != nil { | ||
t.Fatalf("Failed to create new resource management client: %s", err) | ||
} | ||
|
||
_, err = chClient.Query(channel.Request{ChaincodeID: chaincodeID, Fcn: "invoke", Args: integration.ExampleCCQueryArgs()}, | ||
channel.WithParentContext(parentContext)) | ||
if err == nil && !strings.Contains(err.Error(), "context canceled") { | ||
t.Fatalf("expected context cancelled error but got: %v", err) | ||
} | ||
|
||
// ledger client | ||
legerClient, err := ledger.New(org1AdminChannelContext) | ||
if err != nil { | ||
t.Fatalf("Failed to create new resource management client: %s", err) | ||
} | ||
|
||
_, err = legerClient.QueryInfo(ledger.WithParentContext(parentContext)) | ||
if err == nil && !strings.Contains(err.Error(), "context canceled") { | ||
t.Fatalf("expected context cancelled error but got: %v", err) | ||
} | ||
|
||
} |