-
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-3072] SDK Go - Add failed transaction events test
Change-Id: I2a21462e6e768ea75806f07c66d35b295901e6e0 Signed-off-by: Sandra Vrtikapa <[email protected]>
- Loading branch information
Showing
6 changed files
with
342 additions
and
85 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,108 @@ | ||
/* | ||
Copyright London Stock Exchange 2017 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 main | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
|
||
"github.com/hyperledger/fabric/core/chaincode/shim" | ||
pb "github.com/hyperledger/fabric/protos/peer" | ||
) | ||
|
||
// EventSender example simple Chaincode implementation | ||
type EventSender struct { | ||
} | ||
|
||
// Init function | ||
func (t *EventSender) Init(stub shim.ChaincodeStubInterface) pb.Response { | ||
err := stub.PutState("noevents", []byte("0")) | ||
if err != nil { | ||
return shim.Error(err.Error()) | ||
} | ||
return shim.Success(nil) | ||
} | ||
|
||
// Invoke function | ||
func (t *EventSender) invoke(stub shim.ChaincodeStubInterface) pb.Response { | ||
_ , args := stub.GetFunctionAndParameters() | ||
if len(args) != 2 { | ||
return shim.Error("Incorrect number of arguments. Expecting 2") | ||
} | ||
b, err := stub.GetState("noevents") | ||
if err != nil { | ||
return shim.Error("Failed to get state") | ||
} | ||
noevts, _ := strconv.Atoi(string(b)) | ||
|
||
tosend := "Event " + string(b) + args[1] | ||
eventName := "evtsender" + args[0] | ||
|
||
err = stub.PutState("noevents", []byte(strconv.Itoa(noevts+1))) | ||
if err != nil { | ||
return shim.Error(err.Error()) | ||
} | ||
|
||
err = stub.SetEvent(eventName, []byte(tosend)) | ||
if err != nil { | ||
return shim.Error(err.Error()) | ||
} | ||
return shim.Success(nil) | ||
} | ||
|
||
// Clear State function | ||
func (t *EventSender) clear(stub shim.ChaincodeStubInterface) pb.Response { | ||
err := stub.PutState("noevents", []byte("0")) | ||
if err != nil { | ||
return shim.Error(err.Error()) | ||
} | ||
return shim.Success(nil) | ||
} | ||
|
||
// Query function | ||
func (t *EventSender) query(stub shim.ChaincodeStubInterface) pb.Response { | ||
b, err := stub.GetState("noevents") | ||
if err != nil { | ||
return shim.Error("Failed to get state") | ||
} | ||
return shim.Success(b) | ||
} | ||
|
||
func (t *EventSender) Invoke(stub shim.ChaincodeStubInterface) pb.Response { | ||
function, args := stub.GetFunctionAndParameters() | ||
|
||
if function != "invoke" { | ||
return shim.Error("Unknown function call") | ||
} | ||
|
||
if args[0] == "invoke" { | ||
return t.invoke(stub) | ||
} else if args[0] == "query" { | ||
return t.query(stub) | ||
} else if args[0] == "clear" { | ||
return t.clear(stub) | ||
} | ||
|
||
return shim.Error("Invalid invoke function name. Expecting \"invoke\" \"query\"") | ||
} | ||
|
||
func main() { | ||
err := shim.Start(new(EventSender)) | ||
if err != nil { | ||
fmt.Printf("Error starting EventSender chaincode: %s", err) | ||
} | ||
} |
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
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,110 @@ | ||
/* | ||
Copyright SecureKey Technologies Inc. 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 integration | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
fabricClient "github.com/hyperledger/fabric-sdk-go/fabric-client" | ||
"github.com/hyperledger/fabric-sdk-go/fabric-client/events" | ||
) | ||
|
||
func TestEvents(t *testing.T) { | ||
|
||
testSetup := BaseSetupImpl{} | ||
|
||
testSetup.InitConfig() | ||
|
||
eventHub, err := testSetup.GetEventHubAndConnect() | ||
if err != nil { | ||
t.Fatalf("GetEventHub return error: %v", err) | ||
} | ||
chain, err := testSetup.GetChain() | ||
if err != nil { | ||
t.Fatalf("GetChain return error: %v", err) | ||
} | ||
// Create and join channel represented by 'chain' | ||
testSetup.CreateAndJoinChannel(t, chain) | ||
|
||
// Install and Instantiate Events CC | ||
err = testSetup.InstallCC(chain, chainCodeID, "github.com/events_cc", chainCodeVersion, nil, nil) | ||
if err != nil { | ||
t.Fatalf("installCC return error: %v", err) | ||
} | ||
err = testSetup.InstantiateCC(chain, eventHub) | ||
if err != nil { | ||
t.Fatalf("instantiateCC return error: %v", err) | ||
} | ||
|
||
testFailedTx(t, chain, eventHub) | ||
|
||
} | ||
|
||
func testFailedTx(t *testing.T, chain fabricClient.Chain, eventHub events.EventHub) { | ||
|
||
// Arguments for events CC | ||
var args []string | ||
args = append(args, "invoke") | ||
args = append(args, "invoke") | ||
args = append(args, "SEVERE") | ||
|
||
tpResponses1, tx1, err := CreateAndSendTransactionProposal(chain, chainCodeID, chainID, args, []fabricClient.Peer{chain.GetPrimaryPeer()}) | ||
if err != nil { | ||
t.Fatalf("CreateAndSendTransactionProposal return error: %v \n", err) | ||
} | ||
|
||
tpResponses2, tx2, err := CreateAndSendTransactionProposal(chain, chainCodeID, chainID, args, []fabricClient.Peer{chain.GetPrimaryPeer()}) | ||
if err != nil { | ||
t.Fatalf("CreateAndSendTransactionProposal return error: %v \n", err) | ||
} | ||
|
||
// Register tx1 and tx2 for commit/block event(s) | ||
done1, fail1 := RegisterEvent(tx1, eventHub) | ||
defer eventHub.UnregisterTxEvent(tx1) | ||
|
||
done2, fail2 := RegisterEvent(tx2, eventHub) | ||
defer eventHub.UnregisterTxEvent(tx2) | ||
|
||
// Test invalid transaction: create 2 invoke requests in quick succession that modify | ||
// the same state variable which should cause one invoke to be invalid | ||
_, err = CreateAndSendTransaction(chain, tpResponses1) | ||
if err != nil { | ||
t.Fatalf("First invoke failed err: %v", err) | ||
} | ||
_, err = CreateAndSendTransaction(chain, tpResponses2) | ||
if err != nil { | ||
t.Fatalf("Second invoke failed err: %v", err) | ||
} | ||
|
||
for i := 0; i < 2; i++ { | ||
select { | ||
case <-done1: | ||
case <-fail1: | ||
case <-done2: | ||
t.Fatalf("Received success for second invoke") | ||
case <-fail2: | ||
// success | ||
return | ||
case <-time.After(time.Second * 30): | ||
t.Fatalf("invoke Didn't receive block event for txid1(%s) or txid1(%s)", tx1, tx2) | ||
} | ||
} | ||
} |
Oops, something went wrong.