示例#1
0
    def test_big_return_data(self):
        print("Testing big return values")
        wrapped_function = pynisher2.enforce_limits()(return_big_array)

        for num_elements in [4, 16, 64, 256, 1024, 4096, 16384, 65536, 262144]:
            bla = wrapped_function(num_elements)
            self.assertEqual(len(bla), num_elements)
示例#2
0
    def test_kill_subprocesses(self):
        wrapped_function = pynisher2.enforce_limits(
            wall_time_in_s=1)(spawn_rogue_subprocess)
        wrapped_function(5)

        time.sleep(1)
        p = psutil.Process()
        self.assertEqual(len(p.children(recursive=True)), 0)
示例#3
0
    def test_high_cpu_percentage(self):
        print("Testing cpu time constraint.")
        cpu_time_in_s = 2
        grace_period = 1
        wrapped_function = pynisher2.enforce_limits(
            cpu_time_in_s=cpu_time_in_s,
            grace_period_in_s=grace_period)(cpu_usage)

        self.assertEqual(None, wrapped_function())
        self.assertEqual(wrapped_function.exit_status,
                         pynisher2.CpuTimeoutException)
示例#4
0
    def test_capture_output(self):
        print("Testing capturing of output.")
        global logger

        time_limit = 2
        grace_period = 1

        def print_and_sleep(t):
            for i in range(t):
                print(i)
                time.sleep(1)

        wrapped_function = pynisher2.enforce_limits(
            wall_time_in_s=time_limit,
            mem_in_mb=None,
            grace_period_in_s=grace_period,
            logger=logger,
            capture_output=True)(print_and_sleep)
        wrapped_function(5)

        self.assertTrue('0' in wrapped_function.stdout)
        self.assertTrue(wrapped_function.stderr == '')

        def print_and_fail():
            print(0)
            raise RuntimeError()

        wrapped_function = pynisher2.enforce_limits(
            wall_time_in_s=time_limit,
            mem_in_mb=None,
            grace_period_in_s=grace_period,
            logger=logger,
            capture_output=True)(print_and_fail)
        wrapped_function()

        self.assertTrue('0' in wrapped_function.stdout)
        self.assertTrue('RuntimeError' in wrapped_function.stderr)
示例#5
0
    def test_busy_in_C_library(self):

        global logger

        wrapped_function = pynisher2.enforce_limits(
            wall_time_in_s=2)(svm_example)

        start = time.time()
        wrapped_function(16384, 128)
        duration = time.time() - start

        time.sleep(1)
        p = psutil.Process()
        self.assertEqual(len(p.children(recursive=True)), 0)
        self.assertTrue(duration < 2.1)
示例#6
0
def nested_pynisher(level=2,
                    cputime=5,
                    walltime=5,
                    memlimit=10e24,
                    increment=-1,
                    grace_period=1):
    print("this is level {}".format(level))
    if level == 0:
        spawn_rogue_subprocess(10)
    else:
        func = pynisher2.enforce_limits(
            mem_in_mb=memlimit,
            cpu_time_in_s=cputime,
            wall_time_in_s=walltime,
            grace_period_in_s=grace_period)(nested_pynisher)
        func(level - 1, None, walltime + increment, memlimit, increment)
示例#7
0
    def test_time_out(self):
        print("Testing wall clock time constraint.")
        local_mem_in_mb = None
        local_wall_time_in_s = 1
        local_cpu_time_in_s = None
        local_grace_period = None

        wrapped_function = pynisher2.enforce_limits(
            mem_in_mb=local_mem_in_mb,
            wall_time_in_s=local_wall_time_in_s,
            cpu_time_in_s=local_cpu_time_in_s,
            grace_period_in_s=local_grace_period)(simulate_work)

        for mem in range(1, 10):
            self.assertIsNone(wrapped_function(mem, 10, 0))
            self.assertEqual(wrapped_function.exit_status,
                             pynisher2.TimeoutException)
示例#8
0
    def test_out_of_memory(self):
        print("Testing memory constraint.")
        local_mem_in_mb = 32
        local_wall_time_in_s = None
        local_cpu_time_in_s = None
        local_grace_period = None

        wrapped_function = pynisher2.enforce_limits(
            mem_in_mb=local_mem_in_mb,
            wall_time_in_s=local_wall_time_in_s,
            cpu_time_in_s=local_cpu_time_in_s,
            grace_period_in_s=local_grace_period)(simulate_work)

        for mem in [1024, 2048, 4096]:
            self.assertIsNone(wrapped_function(mem, 0, 0))
            self.assertEqual(wrapped_function.exit_status,
                             pynisher2.MemorylimitException)
示例#9
0
    def test_success(self):

        print("Testing unbounded function call which have to run through!")
        local_mem_in_mb = None
        local_wall_time_in_s = None
        local_cpu_time_in_s = None
        local_grace_period = None

        wrapped_function = pynisher2.enforce_limits(
            mem_in_mb=local_mem_in_mb,
            wall_time_in_s=local_wall_time_in_s,
            cpu_time_in_s=local_cpu_time_in_s,
            grace_period_in_s=local_grace_period)(simulate_work)

        for mem in [1, 2, 4, 8, 16]:
            self.assertEqual((mem, 0, 0), wrapped_function(mem, 0, 0))
            self.assertEqual(wrapped_function.exit_status, 0)
示例#10
0
    def test_liblinear_svc(self):

        global logger

        time_limit = 2
        grace_period = 1

        wrapped_function = pynisher2.enforce_limits(
            cpu_time_in_s=time_limit,
            mem_in_mb=None,
            grace_period_in_s=grace_period,
            logger=logger)(svc_example)
        start = time.time()
        wrapped_function(16384, 1000)
        duration = time.time() - start

        time.sleep(1)
        p = psutil.Process()
        self.assertEqual(len(p.children(recursive=True)), 0)
        # fails with pynisher.AnythingException for some reason
        # self.assertTrue(wrapped_function.exit_status == pynisher.CpuTimeoutException)
        self.assertTrue(duration > time_limit - 0.1)
        self.assertTrue(duration < time_limit + grace_period + 0.1)
示例#11
0
# using it as a decorator for every call to this function
@pynisher2.enforce_limits(wall_time_in_s=2)
def my_function(t):
    time.sleep(t)
    return t


for t in range(5):
    print(my_function(t))


def my_other_function(t):
    print('foo')
    time.sleep(t)
    print('bar')
    return t


# explicitly create a new function without wrapping the original everytime
my_wrapped_function = pynisher2.enforce_limits(
    wall_time_in_s=3, capture_output=True)(my_other_function)

for t in range(5):
    print(my_wrapped_function(t))
    print(vars(my_wrapped_function))

import IPython

IPython.embed()
示例#12
0
 def test_crash_unexpectedly(self):
     print("Testing an unexpected signal simulating a crash.")
     wrapped_function = pynisher2.enforce_limits()(crash_unexpectedly)
     self.assertIsNone(wrapped_function(signal.SIGQUIT))
     self.assertEqual(wrapped_function.exit_status,
                      pynisher2.AnythingException)