1

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?

  • I'm trying to solve the same issue. Did you get to any solution? – Gus Jun 7 '18 at 21:43
0

This library might help keep the test script concise.

https://github.com/rkalis/truffle-assertions

If can look like this:

truffleAssert.eventEmitted(result, 'TestEvent');

or this, if you want to inspect event data:

truffleAssert.eventEmitted(result, 'TestEvent', (ev) => {
    return ev.param1 === 10 && ev.param2 === ev.param3;
});

or even like this is you want to confirm that a "bad news" event was not emitted:

truffleAssert.eventNotEmitted(result, 'TestEvent');

Hope it helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.