-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Iff the contents of the file identified in GRB_LICENSE_FILE contains the magic keywork HOSTID, then any license that is obtained will never be destroyed. Resolves #19657. A new implementation and unit test strategy
- Loading branch information
1 parent
444ef41
commit fe4e54d
Showing
5 changed files
with
130 additions
and
7 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,43 @@ | ||
import copy | ||
import os | ||
from pathlib import Path | ||
import subprocess | ||
import unittest | ||
|
||
|
||
class TestGurobiSolverLicenseRetention(unittest.TestCase): | ||
|
||
def _subprocess_license_use_count(self, license_file_content): | ||
"""Sets GRB_LICENSE_FILE to a temp file with the given content, runs | ||
our test helper, and then returns the license pointer use_count. | ||
""" | ||
# Create a dummy license file. Note that the license filename is magic. | ||
# The License code in gurobi_solver.cc treats this filename specially. | ||
tmpdir = Path(os.environ["TEST_TMPDIR"]) | ||
license_file = tmpdir / "DRAKE_UNIT_TEST_NO_LICENSE.lic" | ||
with open(license_file, "w", encoding="utf-8") as f: | ||
f.write(license_file_content) | ||
|
||
# Override the built-in license file. | ||
env = copy.copy(os.environ) | ||
env["GRB_LICENSE_FILE"] = str(license_file) | ||
|
||
# Run the helper and return the poitner use_count. | ||
output = subprocess.check_output( | ||
["solvers/gurobi_solver_license_retention_test_helper"]) | ||
return int(output) | ||
|
||
def test_local_license(self): | ||
"""When the file named by GRB_LICENSE_FILE contains 'HOSTID', the | ||
license object is held in two places: the test helper main(), and | ||
a global variable within GurobiSolver::AcquireLicense. | ||
""" | ||
content = "HOSTID=foobar\n" | ||
self.assertEqual(self._subprocess_license_use_count(content), 2) | ||
|
||
def test_nonlocal_license(self): | ||
"""When the file named by GRB_LICENSE_FILE doesn't contain 'HOSTID', | ||
the license object is only held by main(), not any global variable. | ||
""" | ||
content = "TOKENSERVER=foobar.invalid.\n" | ||
self.assertEqual(self._subprocess_license_use_count(content), 1) |
11 changes: 11 additions & 0 deletions
11
solvers/test/gurobi_solver_license_retention_test_helper.cc
This file contains 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,11 @@ | ||
#include "drake/solvers/gurobi_solver.h" | ||
|
||
using drake::solvers::GurobiSolver; | ||
|
||
/* Acquire a license and report the overall use_count. */ | ||
int main() { | ||
std::shared_ptr<GurobiSolver::License> license = | ||
GurobiSolver::AcquireLicense(); | ||
fmt::print("{}\n", license.use_count()); | ||
return 0; | ||
} |