My contract is
contract SimpleContract {
uint storedData;
event SimpleEvent(uint p); // EVENT
function set(uint x) public
{
storedData = x;
SimpleEvent(x); // TRIGGER EVENT
}
function get() public returns (uint) { return storedData; }
}
When I execute this test with truffle test
, the console listens forever and the test never ends.
var SimpleContract = artifacts.require("SimpleContract");
contract('SimpleContract', function(accounts) {
let contract;
let owner;
let web3Contract;
before(async () => {
contract = await SimpleContract.deployed();
web3Contract = web3.eth.contract(contract.abi).at(contract.address);
owner = web3Contract._eth.coinbase;
});
it("test", async function() {
let event = contract.SimpleEvent({_from: owner}, {fromBlock: 0, toBlock: 'latest'});
// THIS TRIGGERS THE EVENT
await contract.set.sendTransaction(10);
// PRINT THE PAST EVENT, BUT DOESN'T STOP THE EXECUTION
event.get((error, logs) => {
console.log(logs)
})
});
});
If I keep executing the test (using truffle develop
and then test
multiple times), I get this message after 11 executions:
(node:382) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 unhandledRejection listeners added. Use emitter.setMaxListeners() to increase limit
Same behaviour with watch()
, but in that case I can fix with:
event.watch(function(error, result) {
if (error) {
console.log(error)
} else {
console.log(result)
}
event.stopWatching() // WITHOUT THIS, EXECUTION NEVER ENDS
})
There is some memory leak, I'm not releasing/unregistering correctly. Any idea?