Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

truffle test hangs if no networks are specified and Ganache is running on port 7545 #1976

Closed
davidmurdoch opened this issue May 1, 2019 · 18 comments
Assignees
Labels
error better issue indicates improvement to error messaging priority3 🔧 truffle test

Comments

@davidmurdoch
Copy link
Member

From a quick look, I believe

var cmd = spawn("node", [chainPath, ipcNetwork, JSON.stringify(options)], {
detached: true,
stdio: "ignore"
});
is part of the issue.

If the child process can't start ganache then when it rejects (here:

) truffle never gets notified of the truffle.ready event (here:
supervisor.emit(socket, "truffle.ready");
)

Also, if the chain.js process crashes truffle doesn't seem to be listening. Might need to listen to the close event: https://nodejs.org/api/child_process.html#child_process_event_close

I don't seen any harm in using a different port for truffle's internal ganache server instance, but I don't have much experience with using truffle myself. For reliability you could (should?) try to start the ganache-core server on a random port, if it fails due to the port already being bound, try again on another random port, and maybe a few more tries before failing.

Alternatively, you could do what truffle migrate does and just (try to) use the already bound 7545 port.

To get a big speed boost you could experiement with running ganache-core as a provider (Ganache.provider(options) instead of Ganache.server(options).

Let me know if you have ganache-related questions!

@eggplantzzz
Copy link
Contributor

@davidmurdoch Is this still a relevant issue?

@davidmurdoch
Copy link
Member Author

Yes. I just tested with truffle v5.0.18

@CruzMolina
Copy link
Contributor

CruzMolina commented May 16, 2019

@davidmurdoch I don't seem able to reproduce your specific error. If I fire up Ganache (Version 2.0.1 (2.0.1.631)) using the default settings and I use latest truffle to run truffle test on a fresh metacoin box (completely commented out truffle config), I see:

Error: The network id specified in the truffle config (4447) does not match the one returned by the network (5777).  Ensure that both the network and the provider are properly configured.
    at Object.detectAndSetNetworkId (/Users/cruzmolina/.npm/_npx/29227/lib/node_modules/truffle/build/webpack:/packages/truffle-core/lib/environment.js:100:1)
    at process.internalTickCallback (internal/process/next_tick.js:77:7)
Truffle v5.0.18 (core: 5.0.18)
Node v11.4.0

If I change the Ganache network id to 4447, I get no errors, the tests pass, and tx's populate in the Ganache dashboard.

@davidmurdoch
Copy link
Member Author

Start up Ganache UI, make sure it is running on port 7545 then run:

mkdir test && cd test && truffle init && truffle test

@CruzMolina
Copy link
Contributor

The init box doesn't have any tests, but otherwise the behavior locally appears to be the same as my above comment when running those commands.

@davidmurdoch
Copy link
Member Author

If you don't have Ganache UI running on port 7545 and you run truffle test on an the init box it completes as expected:

david @ ~/work/teams/test (develop)
└─ $ ▶ truffle test

Compiling your contracts...
===========================
> Compiling ./contracts/Migrations.sol
> Artifacts written to /tmp/test-119416-20248-4z8y8d.rzlz9
> Compiled successfully using:
   - solc: 0.5.0+commit.1d4f565a.Emscripten.clang



  0 passing (0ms)

@davidmurdoch
Copy link
Member Author

Run Ganache UI on port 7545 then run mkdir test && cd test && truffle unbox metacoin && truffle test and it fails to complete (at least in a reasonable amount of time ... I've only ever waited up to a minute or so).

@CruzMolina
Copy link
Contributor

Huh, what OS are you using?

@davidmurdoch
Copy link
Member Author

Ubuntu 18.04

@davidmurdoch
Copy link
Member Author

Testing on Windows 10 and it returns the The network id specified in the truffle config (4447) error message.

The error message The network id specified in the truffle config (4447) is inaccurate though, as there is no network id specified in the truffle config (since there is no truffle config).

@CruzMolina
Copy link
Contributor

4447 is the default network_id truffle test uses to connectOrStart a ganache-core chain in the background (https://github.com/trufflesuite/truffle/blob/develop/packages/truffle-core/lib/commands/test.js#L153-L174).

#1577 is an outstanding PR to change the default truffle test network_id to match GanacheUI's defaults.

@davidmurdoch
Copy link
Member Author

Even if #1577 is fixed the error message would still be inaccurate in cases where the ganache network id was changed.

@CruzMolina
Copy link
Contributor

Good point! The default truffle test flow could probably be rewritten to detect Ganache's network_id and then start up a chain in the background if undetected. That wouldn't handle cases where Ganache's port is changed, but it's a step forward.

@gnidan gnidan added error better issue indicates improvement to error messaging truffle test labels Jun 12, 2019
@mallapurbharat
Copy link

I face a similar issue when I download a fresh instance of MetaCoin box. Truffle compile, truffle migrate works (looks like if Ganache is running, it detects Ganache anyway and deploys successfully), but truffle test just hangs. I was unable to understand why test was hanging, then I realized that in truffle-config.js, there was no network specified as by default all lines are commented out. Once I uncommented the "development" section, truffle test succeeded!

It would be good if truffle test command could check and warn that there is no network specified.

@unboxedtype
Copy link

Same issue with truffle v5.1.0

@eggplantzzz
Copy link
Contributor

When Ganache is running on port 7545, somehow the network id is set by Truffle to 4447 yet it somehow knows that Ganache is running at 7545 and the network ids conflict. It would be a good idea to test other ports for these same symptoms.

@eggplantzzz
Copy link
Contributor

eggplantzzz commented Jul 31, 2020

If no network options are set when running truffle test, then Truffle attempts to start Ganache at port 7545. I think then Truffle will find the already running instance and there will be a network id clash. Here are the default settings.

I think a solution would be to by default choose a different port to lessen the chance that this clash will occur. This will lessen the chance of a clash since common (past and present) port defaults for Ganache are 7545, 8545, and 9545. If we avoid these for defaults during truffle test, it should achieve this aim.

@ashu17188
Copy link

I am using Ubuntu 20.X, in my case as well "truffle test" hangs.
My Resolution:

  1. Again I have done => npm install -g truffle, truffle updated to v5.5.4 and it started working.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
error better issue indicates improvement to error messaging priority3 🔧 truffle test
Projects
None yet
Development

No branches or pull requests

8 participants