[英]GlobalMemoryStatus function reports invalid available page file in NUnit console run
我正在NUnit测试中调用GlobalMemoryStatus
:
[TestFixture]
public class UnitTest1
{
[DllImport("kernel32.dll")]
private static extern void GlobalMemoryStatus([In, Out] MemoryStatus status);
[Test]
public void TestMethod1()
{
var mem = new MemoryStatus();
GlobalMemoryStatus(mem);
Assert.AreNotEqual(0, mem.dwAvailPageFile);
}
[StructLayout(LayoutKind.Sequential)]
internal class MemoryStatus
{
public uint dwLength;
public uint dwMemoryLoad;
public uint dwTotalPhys;
public uint dwAvailPhys;
public uint dwTotalPageFile;
public uint dwAvailPageFile;
public uint dwTotalVirtual;
public uint dwAvailVirtual;
}
}
在Visual Studio中运行时,此测试工作正常。
但是如果使用nunit-console.exe
执行它会失败:
nunit\nunit-console.exe bin\Debug\UnitTestProject1.dll /framework:net-4.5
使用此输出:
PS C:\UnitTestProject1> nunit\nunit-console.exe bin\Debug\UnitTestProject1.dll /framework:net-4.5
NUnit-Console version 2.6.4.14350
Copyright (C) 2002-2012 Charlie Poole.
Copyright (C) 2002-2004 James W. Newkirk, Michael C. Two, Alexei A. Vorontsov.
Copyright (C) 2000-2002 Philip Craig.
All Rights Reserved.
Runtime Environment -
OS Version: Microsoft Windows NT 6.1.7601 Service Pack 1
CLR Version: 2.0.50727.5466 ( Net 3.5 )
ProcessModel: Default DomainUsage: Single
Execution Runtime: net-4.5
.F
Tests run: 1, Errors: 0, Failures: 1, Inconclusive: 0, Time: 0.1470761 seconds
Not run: 0, Invalid: 0, Ignored: 0, Skipped: 0
Errors and Failures:
1) Test Failure : UnitTestProject1.UnitTest1.TestMethod1
Expected: not 0
But was: 0
如果我使用MSTest
它工作正常:
mstest /testcontainer:bin/debug/unittestproject1.dll
使用此输出:
C:\UnitTestProject1>mstest /testcontainer:bin/debug/unittestproject1.dll
Microsoft (R) Test Execution Command Line Tool Version 11.0.61030.0
Copyright (c) Microsoft Corporation. All rights reserved.
Loading bin/debug/unittestproject1.dll...
Starting execution...
Results Top Level Tests
------- ---------------
Passed UnitTestProject1.UnitTest1.TestMethod1
1/1 test(s) Passed
Summary
-------
Test Run Completed.
Passed 1
---------
Total 1
Results file: C:\UnitTestProject1\TestResults\ddimitrov_BPCANALYTICS01 2015-04-02 14_09_16.trx
Test Settings: Default Test Settings
我在具有6GB RAM的Windows Server 2008 R2 Standard x64计算机上运行测试。 相同的nunit-console
测试在Windows 8.1上运行正常。
你知道我可能做错了什么,以便这个Win32函数报告错误的数据吗?
结构的定义如下:
typedef struct _MEMORYSTATUS {
DWORD dwLength;
DWORD dwMemoryLoad;
SIZE_T dwTotalPhys;
SIZE_T dwAvailPhys;
SIZE_T dwTotalPageFile;
SIZE_T dwAvailPageFile;
SIZE_T dwTotalVirtual;
SIZE_T dwAvailVirtual;
} MEMORYSTATUS;
SIZE_T
成员是指针大小,这意味着您的声明在64位进程中将是不正确的。 对这些成员使用UIntPtr
。
以下是MSDN的评论:
在具有4 GB以上内存的计算机上,MEMORYSTATUS结构可以返回不正确的信息,报告值-1表示溢出。 如果您的应用程序存在此行为风险,请使用GlobalMemoryStatusEx函数而不是GlobalMemoryStatus函数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.