Example #1
0
    def test_compare_values(self):
        def assert_ge_0(obj):
            if isinstance(obj, tuple):
                for value in obj:
                    self.assertGreaterEqual(value, 0, msg=obj)
            elif isinstance(obj, (int, long, float)):
                self.assertGreaterEqual(obj, 0)
            else:
                assert 0  # case not handled which needs to be fixed

        def compare_with_tolerance(ret1, ret2, tolerance):
            if ret1 == ret2:
                return
            else:
                if isinstance(ret2, (int, long, float)):
                    diff = abs(ret1 - ret2)
                    self.assertLessEqual(diff, tolerance)
                elif isinstance(ret2, tuple):
                    for a, b in zip(ret1, ret2):
                        diff = abs(a - b)
                        self.assertLessEqual(diff, tolerance)

        from psutil._pswindows import ntpinfo
        failures = []
        for p in psutil.process_iter():
            try:
                nt = ntpinfo(*cext.proc_info(p.pid))
            except psutil.NoSuchProcess:
                continue
            assert_ge_0(nt)

            for name, tolerance in self.fun_names:
                if name == 'proc_memory_info' and p.pid == os.getpid():
                    continue
                if name == 'proc_create_time' and p.pid in (0, 4):
                    continue
                meth = wrap_exceptions(getattr(cext, name))
                try:
                    ret = meth(p.pid)
                except (psutil.NoSuchProcess, psutil.AccessDenied):
                    continue
                # compare values
                try:
                    if name == 'proc_cpu_times':
                        compare_with_tolerance(ret[0], nt.user_time, tolerance)
                        compare_with_tolerance(ret[1],
                                               nt.kernel_time, tolerance)
                    elif name == 'proc_create_time':
                        compare_with_tolerance(ret, nt.create_time, tolerance)
                    elif name == 'proc_num_handles':
                        compare_with_tolerance(ret, nt.num_handles, tolerance)
                    elif name == 'proc_io_counters':
                        compare_with_tolerance(ret[0], nt.io_rcount, tolerance)
                        compare_with_tolerance(ret[1], nt.io_wcount, tolerance)
                        compare_with_tolerance(ret[2], nt.io_rbytes, tolerance)
                        compare_with_tolerance(ret[3], nt.io_wbytes, tolerance)
                    elif name == 'proc_memory_info':
                        try:
                            rawtupl = cext.proc_memory_info_2(p.pid)
                        except psutil.NoSuchProcess:
                            continue
                        compare_with_tolerance(ret, rawtupl, tolerance)
                except AssertionError:
                    trace = traceback.format_exc()
                    msg = '%s\npid=%s, method=%r, ret_1=%r, ret_2=%r' % (
                        trace, p.pid, name, ret, nt)
                    failures.append(msg)
                    break

        if failures:
            self.fail('\n\n'.join(failures))
Example #2
0
    def test_compare_values(self):
        # Certain APIs on Windows have 2 internal implementations, one
        # based on documented Windows APIs, another one based
        # NtQuerySystemInformation() which gets called as fallback in
        # case the first fails because of limited permission error.
        # Here we test that the two methods return the exact same value,
        # see:
        # https://github.com/giampaolo/psutil/issues/304
        def assert_ge_0(obj):
            if isinstance(obj, tuple):
                for value in obj:
                    self.assertGreaterEqual(value, 0, msg=obj)
            elif isinstance(obj, (int, long, float)):
                self.assertGreaterEqual(obj, 0)
            else:
                assert 0  # case not handled which needs to be fixed

        def compare_with_tolerance(ret1, ret2, tolerance):
            if ret1 == ret2:
                return
            else:
                if isinstance(ret2, (int, long, float)):
                    diff = abs(ret1 - ret2)
                    self.assertLessEqual(diff, tolerance)
                elif isinstance(ret2, tuple):
                    for a, b in zip(ret1, ret2):
                        diff = abs(a - b)
                        self.assertLessEqual(diff, tolerance)

        from psutil._pswindows import ntpinfo
        failures = []
        for p in psutil.process_iter():
            try:
                nt = ntpinfo(*_psutil_windows.proc_info(p.pid))
            except psutil.NoSuchProcess:
                continue
            assert_ge_0(nt)

            for name, tolerance in self.fun_names:
                if name == 'proc_memory_info' and p.pid == os.getpid():
                    continue
                if name == 'proc_create_time' and p.pid in (0, 4):
                    continue
                meth = wrap_exceptions(getattr(_psutil_windows, name))
                try:
                    ret = meth(p.pid)
                except (psutil.NoSuchProcess, psutil.AccessDenied):
                    continue
                # compare values
                try:
                    if name == 'proc_cpu_times':
                        compare_with_tolerance(ret[0], nt.user_time, tolerance)
                        compare_with_tolerance(ret[1],
                                               nt.kernel_time, tolerance)
                    elif name == 'proc_create_time':
                        compare_with_tolerance(ret, nt.create_time, tolerance)
                    elif name == 'proc_num_handles':
                        compare_with_tolerance(ret, nt.num_handles, tolerance)
                    elif name == 'proc_io_counters':
                        compare_with_tolerance(ret[0], nt.io_rcount, tolerance)
                        compare_with_tolerance(ret[1], nt.io_wcount, tolerance)
                        compare_with_tolerance(ret[2], nt.io_rbytes, tolerance)
                        compare_with_tolerance(ret[3], nt.io_wbytes, tolerance)
                    elif name == 'proc_memory_info':
                        try:
                            rawtupl = _psutil_windows.proc_memory_info_2(p.pid)
                        except psutil.NoSuchProcess:
                            continue
                        compare_with_tolerance(ret, rawtupl, tolerance)
                except AssertionError:
                    trace = traceback.format_exc()
                    msg = '%s\npid=%s, method=%r, ret_1=%r, ret_2=%r' % (
                        trace, p.pid, name, ret, nt)
                    failures.append(msg)
                    break

        if failures:
            self.fail('\n\n'.join(failures))
Example #3
0
    def test_compare_values(self):
        # Certain APIs on Windows have 2 internal implementations, one
        # based on documented Windows APIs, another one based
        # NtQuerySystemInformation() which gets called as fallback in
        # case the first fails because of limited permission error.
        # Here we test that the two methods return the exact same value,
        # see:
        # https://github.com/giampaolo/psutil/issues/304
        def assert_ge_0(obj):
            if isinstance(obj, tuple):
                for value in obj:
                    self.assertGreaterEqual(value, 0, msg=obj)
            elif isinstance(obj, (int, long, float)):
                self.assertGreaterEqual(obj, 0)
            else:
                assert 0  # case not handled which needs to be fixed

        def compare_with_tolerance(ret1, ret2, tolerance):
            if ret1 == ret2:
                return
            else:
                if isinstance(ret2, (int, long, float)):
                    diff = abs(ret1 - ret2)
                    self.assertLessEqual(diff, tolerance)
                elif isinstance(ret2, tuple):
                    for a, b in zip(ret1, ret2):
                        diff = abs(a - b)
                        self.assertLessEqual(diff, tolerance)

        from psutil._pswindows import ntpinfo
        failures = []
        for p in psutil.process_iter():
            try:
                nt = ntpinfo(*_psutil_windows.proc_info(p.pid))
            except psutil.NoSuchProcess:
                continue
            assert_ge_0(nt)

            for name, tolerance in self.fun_names:
                if name == 'proc_memory_info' and p.pid == os.getpid():
                    continue
                if name == 'proc_create_time' and p.pid in (0, 4):
                    continue
                meth = wrap_exceptions(getattr(_psutil_windows, name))
                try:
                    ret = meth(p.pid)
                except (psutil.NoSuchProcess, psutil.AccessDenied):
                    continue
                # compare values
                try:
                    if name == 'proc_cpu_times':
                        compare_with_tolerance(ret[0], nt.user_time, tolerance)
                        compare_with_tolerance(ret[1], nt.kernel_time,
                                               tolerance)
                    elif name == 'proc_create_time':
                        compare_with_tolerance(ret, nt.create_time, tolerance)
                    elif name == 'proc_num_handles':
                        compare_with_tolerance(ret, nt.num_handles, tolerance)
                    elif name == 'proc_io_counters':
                        compare_with_tolerance(ret[0], nt.io_rcount, tolerance)
                        compare_with_tolerance(ret[1], nt.io_wcount, tolerance)
                        compare_with_tolerance(ret[2], nt.io_rbytes, tolerance)
                        compare_with_tolerance(ret[3], nt.io_wbytes, tolerance)
                    elif name == 'proc_memory_info':
                        try:
                            rawtupl = _psutil_windows.proc_memory_info_2(p.pid)
                        except psutil.NoSuchProcess:
                            continue
                        compare_with_tolerance(ret, rawtupl, tolerance)
                except AssertionError:
                    trace = traceback.format_exc()
                    msg = '%s\npid=%s, method=%r, ret_1=%r, ret_2=%r' % (
                        trace, p.pid, name, ret, nt)
                    failures.append(msg)
                    break

        if failures:
            self.fail('\n\n'.join(failures))
Example #4
0
    def test_compare_values(self):
        def assert_ge_0(obj):
            if isinstance(obj, tuple):
                for value in obj:
                    self.assertGreaterEqual(value, 0, msg=obj)
            elif isinstance(obj, (int, long, float)):
                self.assertGreaterEqual(obj, 0)
            else:
                assert 0  # case not handled which needs to be fixed

        def compare_with_tolerance(ret1, ret2, tolerance):
            if ret1 == ret2:
                return
            else:
                if isinstance(ret2, (int, long, float)):
                    diff = abs(ret1 - ret2)
                    self.assertLessEqual(diff, tolerance)
                elif isinstance(ret2, tuple):
                    for a, b in zip(ret1, ret2):
                        diff = abs(a - b)
                        self.assertLessEqual(diff, tolerance)

        from psutil._pswindows import ntpinfo
        failures = []
        for p in psutil.process_iter():
            try:
                nt = ntpinfo(*cext.proc_info(p.pid))
            except psutil.NoSuchProcess:
                continue
            assert_ge_0(nt)

            for name, tolerance in self.fun_names:
                if name == 'proc_memory_info' and p.pid == os.getpid():
                    continue
                if name == 'proc_create_time' and p.pid in (0, 4):
                    continue
                meth = wrap_exceptions(getattr(cext, name))
                try:
                    ret = meth(p.pid)
                except (psutil.NoSuchProcess, psutil.AccessDenied):
                    continue
                # compare values
                try:
                    if name == 'proc_cpu_times':
                        compare_with_tolerance(ret[0], nt.user_time, tolerance)
                        compare_with_tolerance(ret[1], nt.kernel_time,
                                               tolerance)
                    elif name == 'proc_create_time':
                        compare_with_tolerance(ret, nt.create_time, tolerance)
                    elif name == 'proc_num_handles':
                        compare_with_tolerance(ret, nt.num_handles, tolerance)
                    elif name == 'proc_io_counters':
                        compare_with_tolerance(ret[0], nt.io_rcount, tolerance)
                        compare_with_tolerance(ret[1], nt.io_wcount, tolerance)
                        compare_with_tolerance(ret[2], nt.io_rbytes, tolerance)
                        compare_with_tolerance(ret[3], nt.io_wbytes, tolerance)
                    elif name == 'proc_memory_info':
                        try:
                            rawtupl = cext.proc_memory_info_2(p.pid)
                        except psutil.NoSuchProcess:
                            continue
                        compare_with_tolerance(ret, rawtupl, tolerance)
                except AssertionError:
                    trace = traceback.format_exc()
                    msg = '%s\npid=%s, method=%r, ret_1=%r, ret_2=%r' % (
                        trace, p.pid, name, ret, nt)
                    failures.append(msg)
                    break

        if failures:
            self.fail('\n\n'.join(failures))