-
-
Notifications
You must be signed in to change notification settings - Fork 402
Added MILL_TEST_FREE_PORT #4931
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
albassort
wants to merge
26
commits into
com-lihaoyi:main
Choose a base branch
from
albassort:ports
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 6 commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
5ed3b21
Added a Port cli option
9e6a6b2
Merge remote-tracking branch 'upstream/main' into ports
1e8fcfc
Added the EnvVar, and renamed the one in MillMain to be named properly
c653c4f
[autofix.ci] apply automated fixes
autofix-ci[bot] 7c16274
added a small test
fcb906e
Removed needless println
24dc402
Clarified some things, added a const for target
52551bc
[autofix.ci] apply automated fixes
autofix-ci[bot] 6cc4367
Accidentally repeated a line
a038193
Merge branch 'main' into ports
albassort 71fc484
[autofix.ci] apply automated fixes
autofix-ci[bot] 27a876d
Added PortManager external module and changed a lot of tests
bbb99ef
deleted duplicate
cc893ab
typo
e2f0250
fixed a broken test
77499c5
temporary revert
fc62b82
fixed a test
4b682ed
Removed dead code
21873fd
Merge remote-tracking branch 'upstream/main' into ports
8c312bd
[autofix.ci] apply automated fixes
autofix-ci[bot] 4c74e9f
resetting stray changes (1?)
3d47359
fixed some broken tests
014d164
Merge branch 'main' into ports
albassort 6511b51
[autofix.ci] apply automated fixes
autofix-ci[bot] 6d7d58b
forgort to set port
646ede7
Merge branch 'ports' of https://github.com/albassort/mill into ports
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 @@ | ||
|
22 changes: 22 additions & 0 deletions
22
integration/feature/ports-system-prop/src/ShutdownExitCodeTests.scala
This file contains hidden or 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,22 @@ | ||
package mill.integration | ||
|
||
import mill.testkit.UtestIntegrationTestSuite | ||
|
||
import utest._ | ||
|
||
object ShutdownExitCodeTests extends UtestIntegrationTestSuite { | ||
// Ensure that `shutdown` succeeds even if the prior command failed | ||
val tests: Tests = Tests { | ||
test("test") - integrationTest { tester => | ||
val result1 = tester.eval(("resolve", "_")) | ||
assert(result1.isSuccess == true) | ||
val result2 = tester.eval("shutdown") | ||
assert(result2.isSuccess == true) | ||
|
||
val result3 = tester.eval("doesnt-exit") | ||
assert(result3.isSuccess == false) | ||
val result4 = tester.eval("shutdown") | ||
assert(result4.isSuccess == true) | ||
} | ||
} | ||
} |
This file contains hidden or 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 hidden or 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 |
---|---|---|
|
@@ -20,6 +20,8 @@ import java.lang.reflect.InvocationTargetException | |
import scala.util.control.NonFatal | ||
import scala.util.Using | ||
import mill.server.Server | ||
import java.lang.NumberFormatException | ||
import java.net.{ServerSocket, BindException} | ||
|
||
@internal | ||
object MillMain { | ||
|
@@ -180,6 +182,7 @@ object MillMain { | |
(false, RunnerState.empty) | ||
|
||
case Result.Success(config) => | ||
|
||
val noColorViaEnv = env.get("NO_COLOR").exists(_.nonEmpty) | ||
val colored = config.color.getOrElse(mainInteractive && !noColorViaEnv) | ||
val colors = | ||
|
@@ -209,8 +212,19 @@ object MillMain { | |
(false, stateCache) | ||
|
||
} else { | ||
val ports = validatePorts(config, streams) | ||
if (ports == None) { | ||
return (true, stateCache) | ||
} | ||
val portStr = ports.get.mkString(",") | ||
|
||
val portJvmProperties = ( | ||
"MILL_TEST_FREE_PORT", | ||
portStr | ||
) | ||
|
||
val userSpecifiedProperties = | ||
userSpecifiedProperties0 ++ config.extraSystemProperties | ||
userSpecifiedProperties0 ++ config.extraSystemProperties + portJvmProperties | ||
|
||
val threadCount = Some(maybeThreadCount.toOption.get) | ||
|
||
|
@@ -348,6 +362,69 @@ object MillMain { | |
} | ||
} | ||
|
||
def validatePorts(config: MillCliConfig, streams: SystemStreams): Option[Set[Int]] = { | ||
val result = | ||
if (config.ports.isDefined) { | ||
var ports = Set.empty[Int] | ||
try { | ||
ports = config.ports.get.split(",").map(_.toInt).toSet | ||
} catch { | ||
case e: NumberFormatException => | ||
streams.err.println( | ||
"Failed to parse --ports, please provide comma sepperated values. E.g 4090,5000,2910" | ||
) | ||
None | ||
} | ||
// Port validation | ||
ports.foreach { case port => | ||
// 1024 is the min port on windows | ||
if (port < 1024 || port > 65535) { | ||
streams.err.println( | ||
s"Failed to parse $port. Ports provided must be within 1024 to 65535." | ||
) | ||
None | ||
} | ||
try { | ||
new ServerSocket(port).close() | ||
} catch { | ||
case e: BindException => { | ||
streams.err.println( | ||
s"Failed to bind to $port! Please only provide free ports." | ||
) | ||
None | ||
} | ||
} | ||
} | ||
Some(ports) | ||
} else { | ||
var i = 0 | ||
var ports = Set.empty[Int] | ||
for (_ <- 1 to 100) { | ||
if (i == 6) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Magic number |
||
return Some(ports) | ||
} | ||
val socket = new ServerSocket(0) | ||
try { | ||
val port = socket.getLocalPort | ||
if (!ports.contains(port)) { | ||
ports = ports + port | ||
i += 1 | ||
} | ||
|
||
} finally { | ||
socket.close() | ||
} | ||
} | ||
|
||
streams.err.println( | ||
"Failed to generate enough ports for servers to use. Either there isn't 6 free ports on your computer, or there is a firewall issue. " | ||
lefou marked this conversation as resolved.
Show resolved
Hide resolved
|
||
) | ||
|
||
None | ||
} | ||
result | ||
} | ||
|
||
def runBspSession( | ||
streams0: SystemStreams, | ||
logStream: Option[PrintStream], | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this a single port or multiple. The comment is a bit vague. If it is multi, that the variable should be
MILL_TEST_FREE_PORTS
and we should document here, which separator is used.