This repository is private.
All pages are served over SSL and all pushing and pulling is done over SSH.
No one may fork, clone, or view it unless they are added as a member.
Every repository with this icon (
) is private.
Every repository with this icon (
This repository is public.
Anyone may fork, clone, or view it.
Every repository with this icon (
) is public.
Every repository with this icon (
Hongli Lai (Phusion) (author)
Tue Apr 29 15:05:10 -0700 2008
| 794939c9 » | Hongli Lai (Phusion) | 2008-02-02 | 1 | #include "tut.h" | |
| 2 | #include "ApplicationPoolClientServer.h" | ||||
| 8e10bb11 » | Hongli Lai (Phusion) | 2008-02-03 | 3 | #include "Utils.h" | |
| 794939c9 » | Hongli Lai (Phusion) | 2008-02-02 | 4 | #include <cstring> | |
| 5 | #include <unistd.h> | ||||
| 456b4c16 » | Hongli Lai (Phusion) | 2008-02-03 | 6 | #include <errno.h> | |
| 794939c9 » | Hongli Lai (Phusion) | 2008-02-02 | 7 | ||
| 8 | using namespace Passenger; | ||||
| 9 | |||||
| 10 | namespace tut { | ||||
| 456b4c16 » | Hongli Lai (Phusion) | 2008-02-03 | 11 | static bool firstRun = true; | |
| 12 | static unsigned int initialFileDescriptors; | ||||
| 674d5ba3 » | Hongli Lai (Phusion) | 2008-02-26 | 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 | 27 | ||
| 674d5ba3 » | Hongli Lai (Phusion) | 2008-02-26 | 28 | struct ApplicationPoolServerTest { | |
| 794939c9 » | Hongli Lai (Phusion) | 2008-02-02 | 29 | ApplicationPoolServerPtr server; | |
| 81aaad54 » | Hongli Lai (Phusion) | 2008-02-26 | 30 | ApplicationPoolPtr pool, pool2; | |
| 794939c9 » | Hongli Lai (Phusion) | 2008-02-02 | 31 | ||
| 674d5ba3 » | Hongli Lai (Phusion) | 2008-02-26 | 32 | ApplicationPoolServerTest() { | |
| 456b4c16 » | Hongli Lai (Phusion) | 2008-02-03 | 33 | if (firstRun) { | |
| 34 | initialFileDescriptors = countOpenFileDescriptors(); | ||||
| 35 | firstRun = false; | ||||
| 36 | } | ||||
| 674d5ba3 » | Hongli Lai (Phusion) | 2008-02-26 | 37 | server = ptr(new ApplicationPoolServer("stub/spawn_server.rb")); | |
| 794939c9 » | Hongli Lai (Phusion) | 2008-02-02 | 38 | } | |
| 456b4c16 » | Hongli Lai (Phusion) | 2008-02-03 | 39 | ||
| 674d5ba3 » | Hongli Lai (Phusion) | 2008-02-26 | 40 | ||
| 794939c9 » | Hongli Lai (Phusion) | 2008-02-02 | 41 | }; | |
| 42 | |||||
| 674d5ba3 » | Hongli Lai (Phusion) | 2008-02-26 | 43 | DEFINE_TEST_GROUP(ApplicationPoolServerTest); | |
| 794939c9 » | Hongli Lai (Phusion) | 2008-02-02 | 44 | ||
| 45 | TEST_METHOD(1) { | ||||
| 46 | // Constructor and destructor should not crash. | ||||
| 8e10bb11 » | Hongli Lai (Phusion) | 2008-02-03 | 47 | // (And yes, this test method is intended to be blank.) | |
| 794939c9 » | Hongli Lai (Phusion) | 2008-02-02 | 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 | 53 | server->connect(); | |
| 794939c9 » | Hongli Lai (Phusion) | 2008-02-02 | 54 | } | |
| 0934dc6c » | Hongli Lai (Phusion) | 2008-02-20 | 55 | ||
| 794939c9 » | Hongli Lai (Phusion) | 2008-02-02 | 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 | 63 | server.reset(); | |
| 794939c9 » | Hongli Lai (Phusion) | 2008-02-02 | 64 | _exit(0); | |
| 65 | } else { | ||||
| 0934dc6c » | Hongli Lai (Phusion) | 2008-02-20 | 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 | 72 | } | |
| 73 | } | ||||
| 0934dc6c » | Hongli Lai (Phusion) | 2008-02-20 | 74 | ||
| 794939c9 » | Hongli Lai (Phusion) | 2008-02-02 | 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 | 81 | server.reset(); | |
| 794939c9 » | Hongli Lai (Phusion) | 2008-02-02 | 82 | _exit(0); | |
| 83 | } else { | ||||
| 0934dc6c » | Hongli Lai (Phusion) | 2008-02-20 | 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 | 90 | } | |
| 91 | } | ||||
| 456b4c16 » | Hongli Lai (Phusion) | 2008-02-03 | 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 | 99 | } | |
| 0934dc6c » | Hongli Lai (Phusion) | 2008-02-20 | 100 | ||






