You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
60 lines
1.4 KiB
Python
60 lines
1.4 KiB
Python
5 years ago
|
|
||
|
import tempfile
|
||
|
import os
|
||
|
|
||
|
import mock
|
||
|
import unittest2
|
||
|
import testlib
|
||
|
|
||
|
import mitogen.parent
|
||
|
import ansible_mitogen.affinity
|
||
|
|
||
|
|
||
|
@unittest2.skipIf(
|
||
|
reason='Linux only',
|
||
|
condition=os.uname()[0] != 'Linux'
|
||
|
)
|
||
|
class LinuxPolicyTest(testlib.TestCase):
|
||
|
klass = ansible_mitogen.affinity.LinuxPolicy
|
||
|
|
||
|
def setUp(self):
|
||
|
self.policy = self.klass()
|
||
|
|
||
|
def _get_cpus(self, path='/proc/self/status'):
|
||
|
fp = open(path)
|
||
|
try:
|
||
|
for line in fp:
|
||
|
if line.startswith('Cpus_allowed'):
|
||
|
return int(line.split()[1], 16)
|
||
|
finally:
|
||
|
fp.close()
|
||
|
|
||
|
def test_set_clear(self):
|
||
|
before = self._get_cpus()
|
||
|
self.policy._set_cpu(3)
|
||
|
self.assertEquals(self._get_cpus(), 1 << 3)
|
||
|
self.policy._clear()
|
||
|
self.assertEquals(self._get_cpus(), before)
|
||
|
|
||
|
def test_clear_on_popen(self):
|
||
|
tf = tempfile.NamedTemporaryFile()
|
||
|
try:
|
||
|
before = self._get_cpus()
|
||
|
self.policy._set_cpu(3)
|
||
|
my_cpu = self._get_cpus()
|
||
|
|
||
|
pid = mitogen.parent.detach_popen(
|
||
|
args=['cp', '/proc/self/status', tf.name]
|
||
|
)
|
||
|
os.waitpid(pid, 0)
|
||
|
|
||
|
his_cpu = self._get_cpus(tf.name)
|
||
|
self.assertNotEquals(my_cpu, his_cpu)
|
||
|
self.policy._clear()
|
||
|
finally:
|
||
|
tf.close()
|
||
|
|
||
|
|
||
|
if __name__ == '__main__':
|
||
|
unittest2.main()
|