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)
Sat Apr 26 10:14:36 -0700 2008
commit  3ca31b3e37c891d3f543944fb108b52e152b99d4
tree    028d53539ab438ffe0997505b9b3e78da1313fd6
parent  59838b9704e6fa966b51f93379fed10d13a7dae9
passenger / test / ApplicationPoolServerTest.cpp
100644 101 lines (84 sloc) 2.259 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include "tut.h"
#include "ApplicationPoolClientServer.h"
#include "Utils.h"
#include <cstring>
#include <unistd.h>
#include <errno.h>
 
using namespace Passenger;
 
namespace tut {
  static bool firstRun = true;
  static unsigned int initialFileDescriptors;
  
  static unsigned int countOpenFileDescriptors() {
    int ret;
    unsigned int result = 0;
    for (long i = sysconf(_SC_OPEN_MAX) - 1; i >= 0; i--) {
      do {
        ret = dup2(i, i);
      } while (ret == -1 && errno == EINTR);
      if (ret != -1) {
        result++;
      }
    }
    return result;
  }
 
  struct ApplicationPoolServerTest {
    ApplicationPoolServerPtr server;
    ApplicationPoolPtr pool, pool2;
    
    ApplicationPoolServerTest() {
      if (firstRun) {
        initialFileDescriptors = countOpenFileDescriptors();
        firstRun = false;
      }
      server = ptr(new ApplicationPoolServer("stub/spawn_server.rb"));
    }
    
    
  };
 
  DEFINE_TEST_GROUP(ApplicationPoolServerTest);
 
  TEST_METHOD(1) {
    // Constructor and destructor should not crash.
    // (And yes, this test method is intended to be blank.)
  }
  
  TEST_METHOD(2) {
    // Connecting to the ApplicationPoolServer, as well as destroying the
    // returned ApplicationPool object, should not crash.
    server->connect();
  }
 
  TEST_METHOD(3) {
    // If connect() has been called, then detach() should not crash, and the
    // ApplicationPoolServer's destructor should not crash either.
    pid_t pid = fork();
    if (pid == 0) {
      server->connect();
      server->detach();
      server.reset();
      _exit(0);
    } else {
      int status;
      
      waitpid(pid, &status, 0);
      if (status != 0) {
        fail("Child process exited abnormally.");
      }
    }
  }
 
  TEST_METHOD(4) {
    // If connect() has not been called, then detach() should not crash, and the
    // ApplicationPoolServer's destructor should not crash either.
    pid_t pid = fork();
    if (pid == 0) {
      server->detach();
      server.reset();
      _exit(0);
    } else {
      int status;
      
      waitpid(pid, &status, 0);
      if (status != 0) {
        fail("Child process exited abnormally.");
      }
    }
  }
  
  TEST_METHOD(5) {
    // ApplicationPoolServer should not leak file descriptors after running all
    // of the above tests.
    server = ApplicationPoolServerPtr();
    ensure_equals(countOpenFileDescriptors(), initialFileDescriptors);
  }
}