Skip to content
This repository was archived by the owner on Apr 22, 2025. It is now read-only.

Commit 168fc66

Browse files
Fix for scenario test failure (#597)
Added wait for events step for chaincode listener. Signed-off-by: sapthasurendran <[email protected]>
1 parent b67b27c commit 168fc66

File tree

4 files changed

+47
-4
lines changed

4 files changed

+47
-4
lines changed

test/ts-scenario/features/base_api_v1.feature

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ Scenario: Using only fabric-common V1 channel
3838
And I regisister a transaction listener named myFilteredTransactionListener with myFilteredEventService for all transactions as client leon
3939
When I create an event service myFullEventService as client leon on channel baseapichannel
4040
And I regisister a block listener named myFullBlockListener with myFullEventService for startBlock 1 and endBlock 4 as client leon
41-
And I regisister a chaincode listener named myFullChaincodeListener with myFullEventService for createCar event on contract fabcar as client leon
41+
And I regisister a chaincode listener named myFullChaincodeListener with myFullEventService with a maximum event count of 3 as client leon for createCar event on contract fabcar
4242
And I regisister a transaction listener named myFullTransactionListener with myFullEventService for all transactions as client leon
4343
And I start the event service myFilteredEventService as filtered blocks to start at block 0 and end at block 4 as client leon
4444
And I start the event service myFullEventService as full blocks to start at block 0 and end at block END as client leon
@@ -48,5 +48,6 @@ Scenario: Using only fabric-common V1 channel
4848
Then the event listener myFilteredChaincodeListener of myFilteredEventService has results matching {"createCar":""} as client leon
4949
Then the event listener myFilteredTransactionListener of myFilteredEventService has results matching {"transaction":"3"} as client leon
5050
Then the event listener myFullBlockListener of myFullEventService has results matching {"block":"4"} as client leon
51+
Then I wait for events from chaincode listener named myFullChaincodeListener with myFullEventService as client leon
5152
Then the event listener myFullChaincodeListener of myFullEventService has results matching {"createCar":"Focus"} as client leon
5253
Then the event listener myFullTransactionListener of myFullEventService has results matching {"transaction":"4"} as client leon

test/ts-scenario/features/base_api_v2.feature

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ Scenario: Using only fabric-common on V2 channel
5050
And I regisister a transaction listener named myFilteredTransactionListener with myFilteredEventService for all transactions as client fred
5151
When I create an event service myFullEventService as client fred on channel basev2channel
5252
And I regisister a block listener named myFullBlockListener with myFullEventService for startBlock 1 and endBlock 4 as client fred
53-
And I regisister a chaincode listener named myFullChaincodeListener with myFullEventService for createCar event on contract fabcar as client fred
53+
And I regisister a chaincode listener named myFullChaincodeListener with myFullEventService with a maximum event count of 3 as client fred for createCar event on contract fabcar
5454
And I regisister a transaction listener named myFullTransactionListener with myFullEventService for all transactions as client fred
5555
And I start the event service myFilteredEventService as filtered blocks to start at block 0 and end at block 6 as client fred
5656
And I start the event service myFullEventService as full blocks to start at block 0 and end at block END as client fred
@@ -60,6 +60,7 @@ Scenario: Using only fabric-common on V2 channel
6060
Then the event listener myFilteredChaincodeListener of myFilteredEventService has results matching {"createCar":""} as client fred
6161
Then the event listener myFilteredTransactionListener of myFilteredEventService has results matching {"transaction":"5"} as client fred
6262
Then the event listener myFullBlockListener of myFullEventService has results matching {"block":"4"} as client fred
63+
Then I wait for events from chaincode listener named myFullChaincodeListener with myFullEventService as client fred
6364
Then the event listener myFullChaincodeListener of myFullEventService has results matching {"createCar":"Focus"} as client fred
6465
Then the event listener myFullTransactionListener of myFullEventService has results matching {"transaction":"7"} as client fred
6566

test/ts-scenario/steps/base_api.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,21 @@ Then(/^I regisister a chaincode listener named (.+?) with (.+?) for (.+?) event
8484
ClientHelper.registerEventListener(eventServiceName, clientName, listenerName, 'chaincode', '', '', eventName, contractName);
8585
});
8686

87+
Then(/^I regisister a chaincode listener named (.+?) with (.+?) with a maximum event count of ([0-9]+) as client (.+?) for (.+?) event on contract (.+?)$/, {timeout: Constants.INC_SHORT},
88+
(listenerName: string, eventServiceName: string, eventCount:number, clientName: string, eventName: string, contractName: string,) => {
89+
ClientHelper.registerEventListener(eventServiceName, clientName, listenerName, 'chaincode', '', '', eventName, contractName, eventCount);
90+
});
91+
8792
Then(/^I regisister a transaction listener named (.+?) with (.+?) for all transactions as client (.+?)$/, {timeout: Constants.INC_SHORT},
8893
(listenerName: string, eventServiceName: string, clientName: string) => {
8994
ClientHelper.registerEventListener(eventServiceName, clientName, listenerName, 'transaction', '', '', '', '');
9095
});
9196

97+
Then(/^I wait for events from chaincode listener named (.+?) with (.+?) as client (.+?)$/, {timeout: Constants.INC_SHORT},
98+
async(listenerName: string, eventServiceName: string, clientName: string) => {
99+
await ClientHelper.waitForEvent(eventServiceName, clientName, listenerName);
100+
});
101+
92102
Then(/^the event listener (.+?) of (.+?) has results matching (.+?) as client (.+?)$/, {timeout: Constants.INC_SHORT},
93103
(listenerName: string, eventServiceName: string, check: string, clientName: string) => {
94104
ClientHelper.checkEventListenerResults(eventServiceName, clientName, listenerName, check);

test/ts-scenario/steps/lib/utility/clientUtils.ts

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ interface ListenerState {
7070
block?: string;
7171
transaction?: string;
7272
},
73+
completePromise?: Promise<void>;
7374
}
7475

7576
function assertNoErrors(endorsementResults: ProposalResponse): void {
@@ -710,7 +711,7 @@ export async function startEventService(
710711
buildOptions.startBlock = Number.parseInt(startBlock, 10);
711712
}
712713
if (endBlock.localeCompare('END') === 0) {
713-
// do not add start block
714+
// do not add end block
714715
} else {
715716
buildOptions.endBlock = Number.parseInt(endBlock, 10);
716717
}
@@ -762,7 +763,7 @@ export async function startEventService(
762763
export function registerEventListener(
763764
eventServiceName: string, clientName: string, listenerName: string, type: string,
764765
startBlock: string, endBlock: string,
765-
chaincodeEventName: string, chaincodeName: string): void {
766+
chaincodeEventName: string, chaincodeName: string, count?:number): void {
766767

767768
const clientObject = retrieveClientObject(clientName);
768769
const eventServiceObject = clientObject.eventServices?.get(eventServiceName);
@@ -820,6 +821,10 @@ export function registerEventListener(
820821
);
821822
} else if (type === 'chaincode') {
822823
BaseUtils.logMsg(`Registering a chaincode event with chaincodeName ${chaincodeName} chaincodeEventName ${chaincodeEventName}`);
824+
825+
let resolve: () => void;
826+
const completePromise = new Promise<void>((_resolve) => resolve = _resolve);
827+
823828
listenerObject.eventListener = eventService.registerChaincodeListener(
824829
chaincodeName,
825830
chaincodeEventName,
@@ -837,15 +842,24 @@ export function registerEventListener(
837842

838843
if (event?.chaincodeEvents) {
839844
for (const chaincodeEvent of event.chaincodeEvents) {
845+
if (count !== undefined) {
846+
count--;
847+
}
840848
const results: any = {};
841849
results[chaincodeEvent.eventName] = chaincodeEvent.payload ? chaincodeEvent.payload.toString() : '';
842850
listenerObject.results = results;
843851
BaseUtils.logMsg(`Store chaincode event listener ${listenerName} results of ${JSON.stringify(results)}`);
852+
if (count === 0) {
853+
resolve();
854+
}
844855
}
845856
}
846857
},
847858
listenerOptions
848859
);
860+
861+
listenerObject.completePromise = completePromise;
862+
849863
} else if (type === 'transaction') {
850864
BaseUtils.logMsg('Registering a transaction event for all transactions');
851865

@@ -905,6 +919,23 @@ export function checkEventListenerResults(
905919
}
906920
}
907921

922+
export async function waitForEvent(
923+
eventServiceName: string, clientName: string, listenerName: string): Promise<void> {
924+
const clientObject: any = retrieveClientObject(clientName);
925+
const eventServiceObject: any = clientObject.eventServices.get(eventServiceName);
926+
const listenerObject: any = eventServiceObject.eventListeners.get(listenerName);
927+
928+
if (listenerObject) {
929+
if (listenerObject.error) {
930+
BaseUtils.logMsg(`Received an error for ${listenerName} of ${util.inspect(listenerObject.error)}`);
931+
throw listenerObject.error;
932+
}
933+
await listenerObject.completePromise;
934+
} else {
935+
BaseUtils.logAndThrow(`Listener object not found ${listenerName}`);
936+
}
937+
}
938+
908939
export function disconnectEventService(
909940
eventServiceName: string, clientName: string): void {
910941
const clientObject: any = retrieveClientObject(clientName);

0 commit comments

Comments
 (0)