Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add unit tests for ProcessMetricsCollector.

  • Loading branch information...
commit bd5015aaceda653ebf26aefbc021ff1d472c1c62 1 parent 76633ee
Hongli Lai FooBarWidget authored
1  .gitignore
View
@@ -41,4 +41,5 @@ test/stub/apache2/httpd.conf
test/stub/zsfa/mycook
test/stub/zsfa/foo
test/stub/rails_apps/3.0/*/help
+test/support/allocate_memory
pkg
4 build/cplusplus_support.rb
View
@@ -47,6 +47,10 @@ def create_executable(target, sources, linkflags = "#{PlatformInfo.portability_c
sh "#{CXX} #{sources} -o #{target} #{linkflags}"
end
+def create_c_executable(target, sources, linkflags = "#{PlatformInfo.portability_cflags} #{EXTRA_CXXFLAGS} #{PlatformInfo.portability_ldflags} #{EXTRA_LDFLAGS}")
+ sh "#{CC} #{sources} -o #{target} #{linkflags}"
+end
+
def create_shared_library(target, sources, flags = "#{PlatformInfo.portability_cflags} #{EXTRA_CXXFLAGS} #{PlatformInfo.portability_ldflags} #{EXTRA_LDFLAGS}")
if RUBY_PLATFORM =~ /darwin/
shlib_flag = "-flat_namespace -bundle -undefined dynamic_lookup"
2  build/cxx_tests.rb
View
@@ -189,7 +189,7 @@
}
desc "Run unit tests for the Apache 2 and Nginx C++ components"
-task 'test:cxx' => ['test/cxx/CxxTestMain', :native_support] do
+task 'test:cxx' => ['test/cxx/CxxTestMain', 'test/support/allocate_memory', :native_support] do
if ENV['GROUPS'].to_s.empty?
sh "cd test && ./cxx/CxxTestMain"
else
5 build/test_basics.rb
View
@@ -26,6 +26,11 @@
desc "Clean all compiled test files"
task 'test:clean' do
sh("rm -rf test/oxt/oxt_test_main test/oxt/*.o test/cxx/CxxTestMain test/cxx/*.o")
+ sh("rm -f test/support/allocate_memory")
end
task :clean => 'test:clean'
+
+file 'test/support/allocate_memory' => 'test/support/allocate_memory.c' do
+ create_c_executable('test/support/allocate_memory', 'test/support/allocate_memory.c')
+end
2  lib/phusion_passenger/packaging.rb
View
@@ -88,7 +88,7 @@ module Packaging
'dev/*',
'resources/*',
'test/*.example',
- 'test/support/*.{cpp,h,rb}',
+ 'test/support/*.{c,cpp,h,rb}',
'test/tut/*',
'test/cxx/*.{cpp,h}',
'test/oxt/*.{cpp,hpp}',
64 test/cxx/ProcessMetricsCollectorTest.cpp
View
@@ -1,4 +1,10 @@
+#include <sys/wait.h>
+#include <signal.h>
+#include <unistd.h>
+#include <cstdio>
+#include <cerrno>
#include "TestSupport.h"
+#include "Utils/StrIntUtils.h"
#include "Utils/ProcessMetricsCollector.h"
using namespace Passenger;
@@ -6,6 +12,37 @@ using namespace Passenger;
namespace tut {
struct ProcessMetricsCollectorTest {
ProcessMetricsCollector collector;
+ pid_t child;
+
+ ProcessMetricsCollectorTest() {
+ child = -1;
+ }
+
+ ~ProcessMetricsCollectorTest() {
+ if (child != -1) {
+ kill(child, SIGKILL);
+ waitpid(child, NULL, 0);
+ }
+ }
+
+ pid_t spawnChild(int memory) {
+ string memoryStr = toString(memory);
+ pid_t pid = fork();
+ if (pid == 0) {
+ execlp("support/allocate_memory",
+ "support/allocate_memory",
+ memoryStr.c_str(),
+ (char *) 0);
+
+ int e = errno;
+ fprintf(stderr, "Cannot execute support/allocate_memory: %s\n",
+ strerror(e));
+ fflush(stderr);
+ _exit(1);
+ } else {
+ return pid;
+ }
+ }
};
DEFINE_TEST_GROUP(ProcessMetricsCollectorTest);
@@ -54,4 +91,31 @@ namespace tut {
ensure(result.find(1) != result.end());
ensure(result.find(34678) == result.end());
}
+
+ TEST_METHOD(3) {
+ // Measuring real memory usage works.
+ ssize_t pss, privateDirty, swap;
+ child = spawnChild(50);
+ usleep(500000);
+ collector.measureRealMemory(child, pss, privateDirty, swap);
+ #ifdef __APPLE__
+ if (geteuid() == 0) {
+ ensure(pss > 50000 && pss < 60000);
+ ensure(privateDirty > 50000 && privateDirty < 60000);
+ ensure_equals(swap, (ssize_t) -1);
+ } else {
+ ensure_equals(pss, (ssize_t) -1);
+ ensure_equals(privateDirty, (ssize_t) -1);
+ ensure_equals(swap, (ssize_t) -1);
+ }
+ #elif defined(__linux__)
+ ensure(pss > 50000 && pss < 60000);
+ ensure(privateDirty > 50000 && privateDirty < 60000);
+ ensure(swap < 10000);
+ #else
+ ensure((pss > 50000 && pss < 60000) || pss == -1);
+ ensure((privateDirty > 50000 && privateDirty < 60000) || privateDirty == -1);
+ ensure(swap < 10000 || swap == -1);
+ #endif
+ }
}
14 test/support/allocate_memory.c
View
@@ -0,0 +1,14 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
+
+int
+main(int argc, char *argv[]) {
+ long size = atol(argv[1]) * 1024 * 1024;
+ char *memory = (char *) malloc(size);
+ memset(memory, 0, size);
+ sleep(999999999);
+ return 0;
+}
+
Please sign in to comment.
Something went wrong with that request. Please try again.