We got nominated! Help us out and vote for GitHub as Best Bootstrapped Startup of 2008. (You can vote once a day.) [ hide ]

public
Description: Phusion Passenger (mod_rails)
Homepage: http://www.modrails.com/
Clone URL: git://github.com/FooBarWidget/passenger.git
Click here to lend your support to: passenger and make a donation at www.pledgie.com !
Hongli Lai (Phusion) (author)
Tue Apr 29 15:05:10 -0700 2008
passenger / test / ApplicationPoolServerTest.cpp
794939c9 » Hongli Lai (Phusion) 2008-02-02 Add a new ApplicationPoolSe... 1 #include "tut.h"
2 #include "ApplicationPoolClientServer.h"
8e10bb11 » Hongli Lai (Phusion) 2008-02-03 - Make DummyRequestHandler ... 3 #include "Utils.h"
794939c9 » Hongli Lai (Phusion) 2008-02-02 Add a new ApplicationPoolSe... 4 #include <cstring>
5 #include <unistd.h>
456b4c16 » Hongli Lai (Phusion) 2008-02-03 - Fix bugs in file descript... 6 #include <errno.h>
794939c9 » Hongli Lai (Phusion) 2008-02-02 Add a new ApplicationPoolSe... 7
8 using namespace Passenger;
9
10 namespace tut {
456b4c16 » Hongli Lai (Phusion) 2008-02-03 - Fix bugs in file descript... 11 static bool firstRun = true;
12 static unsigned int initialFileDescriptors;
674d5ba3 » Hongli Lai (Phusion) 2008-02-26 Reorganize the C++ unit tes... 13
14 static unsigned int countOpenFileDescriptors() {
15 int ret;
16 unsigned int result = 0;
17 for (long i = sysconf(_SC_OPEN_MAX) - 1; i >= 0; i--) {
18 do {
19 ret = dup2(i, i);
20 } while (ret == -1 && errno == EINTR);
21 if (ret != -1) {
22 result++;
23 }
24 }
25 return result;
26 }
456b4c16 » Hongli Lai (Phusion) 2008-02-03 - Fix bugs in file descript... 27
674d5ba3 » Hongli Lai (Phusion) 2008-02-26 Reorganize the C++ unit tes... 28 struct ApplicationPoolServerTest {
794939c9 » Hongli Lai (Phusion) 2008-02-02 Add a new ApplicationPoolSe... 29 ApplicationPoolServerPtr server;
81aaad54 » Hongli Lai (Phusion) 2008-02-26 Add more tests for the Appl... 30 ApplicationPoolPtr pool, pool2;
794939c9 » Hongli Lai (Phusion) 2008-02-02 Add a new ApplicationPoolSe... 31
674d5ba3 » Hongli Lai (Phusion) 2008-02-26 Reorganize the C++ unit tes... 32 ApplicationPoolServerTest() {
456b4c16 » Hongli Lai (Phusion) 2008-02-03 - Fix bugs in file descript... 33 if (firstRun) {
34 initialFileDescriptors = countOpenFileDescriptors();
35 firstRun = false;
36 }
674d5ba3 » Hongli Lai (Phusion) 2008-02-26 Reorganize the C++ unit tes... 37 server = ptr(new ApplicationPoolServer("stub/spawn_server.rb"));
794939c9 » Hongli Lai (Phusion) 2008-02-02 Add a new ApplicationPoolSe... 38 }
456b4c16 » Hongli Lai (Phusion) 2008-02-03 - Fix bugs in file descript... 39
674d5ba3 » Hongli Lai (Phusion) 2008-02-26 Reorganize the C++ unit tes... 40
794939c9 » Hongli Lai (Phusion) 2008-02-02 Add a new ApplicationPoolSe... 41 };
42
674d5ba3 » Hongli Lai (Phusion) 2008-02-26 Reorganize the C++ unit tes... 43 DEFINE_TEST_GROUP(ApplicationPoolServerTest);
794939c9 » Hongli Lai (Phusion) 2008-02-02 Add a new ApplicationPoolSe... 44
45 TEST_METHOD(1) {
46 // Constructor and destructor should not crash.
8e10bb11 » Hongli Lai (Phusion) 2008-02-03 - Make DummyRequestHandler ... 47 // (And yes, this test method is intended to be blank.)
794939c9 » Hongli Lai (Phusion) 2008-02-02 Add a new ApplicationPoolSe... 48 }
49
50 TEST_METHOD(2) {
51 // Connecting to the ApplicationPoolServer, as well as destroying the
52 // returned ApplicationPool object, should not crash.
911462eb » Hongli Lai (Phusion) 2008-02-23 The request handler now lis... 53 server->connect();
794939c9 » Hongli Lai (Phusion) 2008-02-02 Add a new ApplicationPoolSe... 54 }
0934dc6c » Hongli Lai (Phusion) 2008-02-20 Include necessary Boost lib... 55
794939c9 » Hongli Lai (Phusion) 2008-02-02 Add a new ApplicationPoolSe... 56 TEST_METHOD(3) {
57 // If connect() has been called, then detach() should not crash, and the
58 // ApplicationPoolServer's destructor should not crash either.
59 pid_t pid = fork();
60 if (pid == 0) {
61 server->connect();
62 server->detach();
17d927ef » Hongli Lai (Phusion) 2008-02-07 - Fix some regressions. 63 server.reset();
794939c9 » Hongli Lai (Phusion) 2008-02-02 Add a new ApplicationPoolSe... 64 _exit(0);
65 } else {
0934dc6c » Hongli Lai (Phusion) 2008-02-20 Include necessary Boost lib... 66 int status;
67
68 waitpid(pid, &status, 0);
69 if (status != 0) {
70 fail("Child process exited abnormally.");
71 }
794939c9 » Hongli Lai (Phusion) 2008-02-02 Add a new ApplicationPoolSe... 72 }
73 }
0934dc6c » Hongli Lai (Phusion) 2008-02-20 Include necessary Boost lib... 74
794939c9 » Hongli Lai (Phusion) 2008-02-02 Add a new ApplicationPoolSe... 75 TEST_METHOD(4) {
76 // If connect() has not been called, then detach() should not crash, and the
77 // ApplicationPoolServer's destructor should not crash either.
78 pid_t pid = fork();
79 if (pid == 0) {
80 server->detach();
17d927ef » Hongli Lai (Phusion) 2008-02-07 - Fix some regressions. 81 server.reset();
794939c9 » Hongli Lai (Phusion) 2008-02-02 Add a new ApplicationPoolSe... 82 _exit(0);
83 } else {
0934dc6c » Hongli Lai (Phusion) 2008-02-20 Include necessary Boost lib... 84 int status;
85
86 waitpid(pid, &status, 0);
87 if (status != 0) {
88 fail("Child process exited abnormally.");
89 }
794939c9 » Hongli Lai (Phusion) 2008-02-02 Add a new ApplicationPoolSe... 90 }
91 }
456b4c16 » Hongli Lai (Phusion) 2008-02-03 - Fix bugs in file descript... 92
93 TEST_METHOD(5) {
94 // ApplicationPoolServer should not leak file descriptors after running all
95 // of the above tests.
96 server = ApplicationPoolServerPtr();
97 ensure_equals(countOpenFileDescriptors(), initialFileDescriptors);
98 }
794939c9 » Hongli Lai (Phusion) 2008-02-02 Add a new ApplicationPoolSe... 99 }
0934dc6c » Hongli Lai (Phusion) 2008-02-20 Include necessary Boost lib... 100