-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
gurobi_solver_license_retention_test.py
43 lines (36 loc) · 1.73 KB
/
gurobi_solver_license_retention_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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 pointer 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)