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 !
passenger / test / ApplicationPoolTest.cpp
100644 403 lines (350 sloc) 13.041 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <cstring>
#include <cstdlib>
#include <cerrno>
#include <signal.h>
 
/**
* This file is used as a template to test the different ApplicationPool implementations.
* It is #included in StandardApplicationPoolTest.cpp and ApplicationServer_ApplicationPoolTest.cpp
*/
#ifdef USE_TEMPLATE
 
  static string createRequestHeaders(const char *uri = "/foo/new") {
    string headers;
    #define ADD_HEADER(name, value) \
      headers.append(name); \
      headers.append(1, '\0'); \
      headers.append(value); \
      headers.append(1, '\0')
    ADD_HEADER("HTTP_HOST", "www.test.com");
    ADD_HEADER("QUERY_STRING", "");
    ADD_HEADER("REQUEST_URI", uri);
    ADD_HEADER("REQUEST_METHOD", "GET");
    ADD_HEADER("REMOTE_ADDR", "localhost");
    ADD_HEADER("PATH_INFO", uri);
    return headers;
  }
  
  static string readAll(int fd) {
    string result;
    char buf[1024 * 32];
    ssize_t ret;
    while (true) {
      do {
        ret = read(fd, buf, sizeof(buf));
      } while (ret == -1 && errno == EINTR);
      if (ret == 0) {
        break;
      } else if (ret == -1) {
        throw SystemException("Cannot read from socket", errno);
      } else {
        result.append(buf, ret);
      }
    }
    return result;
  }
  
  static Application::SessionPtr spawnRackApp(ApplicationPoolPtr pool, const char *appRoot) {
    SpawnOptions options;
    options.appRoot = appRoot;
    options.appType = "rack";
    return pool->get(options);
  }
  
  static Application::SessionPtr spawnWsgiApp(ApplicationPoolPtr pool, const char *appRoot) {
    SpawnOptions options;
    options.appRoot = appRoot;
    options.appType = "wsgi";
    return pool->get(options);
  }
 
  TEST_METHOD(1) {
    // Calling ApplicationPool.get() once should return a valid Session.
    Application::SessionPtr session(pool->get("stub/railsapp"));
    session->sendHeaders(createRequestHeaders());
    session->shutdownWriter();
 
    int reader = session->getStream();
    string result(readAll(reader));
    session->closeStream();
    ensure(result.find("hello world") != string::npos);
  }
  
  TEST_METHOD(2) {
    // Verify that the pool spawns a new app, and that
    // after the session is closed, the app is kept around.
    Application::SessionPtr session(spawnRackApp(pool, "stub/rack"));
    ensure_equals("Before the session was closed, the app was busy", pool->getActive(), 1u);
    ensure_equals("Before the session was closed, the app was in the pool", pool->getCount(), 1u);
    session.reset();
    ensure_equals("After the session is closed, the app is no longer busy", pool->getActive(), 0u);
    ensure_equals("After the session is closed, the app is kept around", pool->getCount(), 1u);
  }
  
  TEST_METHOD(3) {
    // If we call get() with an application root, then we close the session,
    // and then we call get() again with the same application root,
    // then the pool should not have spawned more than 1 app in total.
    Application::SessionPtr session(spawnRackApp(pool, "stub/rack"));
    session.reset();
    session = spawnRackApp(pool, "stub/rack");
    ensure_equals(pool->getCount(), 1u);
  }
  
  TEST_METHOD(4) {
    // If we call get() with an application root, then we call get() again before closing
    // the session, then the pool should have spawned 2 apps in total.
    Application::SessionPtr session(spawnRackApp(pool, "stub/rack"));
    Application::SessionPtr session2(spawnRackApp(pool, "stub/rack"));
    ensure_equals(pool->getCount(), 2u);
  }
  
  TEST_METHOD(5) {
    // If we call get() twice with different application roots,
    // then the pool should spawn two different apps.
    Application::SessionPtr session(pool->get("stub/railsapp"));
    Application::SessionPtr session2(pool2->get("stub/railsapp2"));
    ensure_equals("Before the sessions were closed, both apps were busy", pool->getActive(), 2u);
    ensure_equals("Before the sessions were closed, both apps were in the pool", pool->getCount(), 2u);
    
    session->sendHeaders(createRequestHeaders());
    string result(readAll(session->getStream()));
    ensure("Session 1 belongs to the correct app", result.find("hello world"));
    session.reset();
    
    session2->sendHeaders(createRequestHeaders());
    result = readAll(session2->getStream());
    ensure("Session 2 belongs to the correct app", result.find("this is railsapp2"));
    session2.reset();
  }
  
  TEST_METHOD(6) {
    // If we call get() twice with different application roots,
    // and we close both sessions, then both 2 apps should still
    // be in the pool.
    Application::SessionPtr session(pool->get("stub/railsapp"));
    Application::SessionPtr session2(pool->get("stub/railsapp2"));
    session.reset();
    session2.reset();
    ensure_equals(pool->getActive(), 0u);
    ensure_equals(pool->getCount(), 2u);
  }
  
  TEST_METHOD(7) {
    // If we call get() even though the pool is already full
    // (active == max), and the application root is already
    // in the pool, then the pool must wait until there's an
    // inactive application.
    pool->setMax(1);
    // TODO: How do we test this?
  }
  
  TEST_METHOD(8) {
    // If ApplicationPool spawns a new instance,
    // and we kill it, then the next get() with the
    // same application root should throw an exception.
    // But the get() thereafter should not:
    // ApplicationPool should have spawned a new instance
    // after detecting that the original one died.
    Application::SessionPtr session(pool->get("stub/railsapp"));
    kill(session->getPid(), SIGTERM);
    session.reset();
    try {
      session = pool->get("stub/railsapp");
      fail("ApplicationPool::get() is supposed to "
        "throw an exception because we killed "
        "the app instance.");
    } catch (const exception &e) {
      session = pool->get("stub/railsapp");
      // Should not throw.
    }
  }
  
  struct TestThread1 {
    ApplicationPoolPtr pool;
    Application::SessionPtr &m_session;
    bool &m_done;
    
    TestThread1(const ApplicationPoolPtr &pool,
      Application::SessionPtr &session,
      bool &done)
    : m_session(session), m_done(done) {
      this->pool = pool;
      done = false;
    }
    
    void operator()() {
      m_session = spawnWsgiApp(pool, "stub/wsgi");
      m_done = true;
    }
  };
 
  TEST_METHOD(9) {
    // If we call get() even though the pool is already full
    // (active == max), and the application root is *not* already
    // in the pool, then the pool will wait until enough sessions
    // have been closed.
    pool->setMax(2);
    Application::SessionPtr session1(spawnRackApp(pool, "stub/rack"));
    Application::SessionPtr session2(spawnRackApp(pool2, "stub/rack"));
    Application::SessionPtr session3;
    bool done;
    
    thread *thr = new thread(TestThread1(pool2, session3, done));
    usleep(500000);
    ensure("ApplicationPool is waiting", !done);
    ensure_equals(pool->getActive(), 2u);
    ensure_equals(pool->getCount(), 2u);
    
    session1.reset();
    
    // Wait at most 10 seconds.
    time_t begin = time(NULL);
    while (!done && time(NULL) - begin < 10) {
      usleep(100000);
    }
    
    ensure("Session 3 is openend", done);
    ensure_equals(pool->getActive(), 2u);
    ensure_equals(pool->getCount(), 2u);
    
    thr->join();
    delete thr;
  }
  
  TEST_METHOD(10) {
    // If we call get(), and:
    // * the pool is already full, but there are inactive apps
    // (active < count && count == max)
    // and
    // * the application root is *not* already in the pool
    // then the an inactive app should be killed in order to
    // satisfy this get() command.
    pool->setMax(2);
    Application::SessionPtr session1(pool->get("stub/railsapp"));
    Application::SessionPtr session2(pool->get("stub/railsapp"));
    session1.reset();
    session2.reset();
    
    ensure_equals(pool->getActive(), 0u);
    ensure_equals(pool->getCount(), 2u);
    session1 = pool2->get("stub/railsapp2");
    ensure_equals(pool->getActive(), 1u);
    ensure_equals(pool->getCount(), 2u);
  }
  
  TEST_METHOD(11) {
    // Test whether Session is still usable after the Application has been destroyed.
    Application::SessionPtr session(pool->get("stub/railsapp"));
    pool->clear();
    pool.reset();
    pool2.reset();
    
    session->sendHeaders(createRequestHeaders());
    session->shutdownWriter();
    
    int reader = session->getStream();
    string result(readAll(reader));
    session->closeStream();
    ensure(result.find("hello world") != string::npos);
  }
  
  TEST_METHOD(12) {
    // If tmp/restart.txt is present, then the applications under app_root
    // should be restarted.
    struct stat buf;
    Application::SessionPtr session1 = pool->get("stub/railsapp");
    Application::SessionPtr session2 = pool2->get("stub/railsapp");
    session1.reset();
    session2.reset();
    
    system("touch stub/railsapp/tmp/restart.txt");
    pool->get("stub/railsapp");
    
    ensure_equals("No apps are active", pool->getActive(), 0u);
    ensure_equals("Both apps are killed, and a new one was spawned",
      pool->getCount(), 1u);
    ensure("Restart file has been deleted",
      stat("stub/railsapp/tmp/restart.txt", &buf) == -1
      && errno == ENOENT);
  }
  
  TEST_METHOD(13) {
    // If tmp/restart.txt is present, but cannot be deleted, then
    // the applications under app_root should still be restarted.
    // However, a subsequent get() should not result in a restart.
    pid_t old_pid, pid;
    struct stat buf;
    Application::SessionPtr session1 = pool->get("stub/railsapp");
    Application::SessionPtr session2 = pool2->get("stub/railsapp");
    session1.reset();
    session2.reset();
    
    system("mkdir -p stub/railsapp/tmp/restart.txt");
    
    old_pid = pool->get("stub/railsapp")->getPid();
    try {
      ensure("Restart file has not been deleted",
        stat("stub/railsapp/tmp/restart.txt", &buf) == 0);
      system("rmdir stub/railsapp/tmp/restart.txt");
    } catch (...) {
      system("rmdir stub/railsapp/tmp/restart.txt");
      throw;
    }
    
    pid = pool->get("stub/railsapp")->getPid();
    ensure_equals("The app was not restarted", pid, old_pid);
    unlink("stub/railsapp/tmp/restart.txt");
  }
  
  TEST_METHOD(14) {
    // If tmp/restart.txt is present, but cannot be deleted, then
    // the applications under app_root should still be restarted.
    // A subsequent get() should only restart if we've changed
    // restart.txt's mtime.
    pid_t old_pid;
    Application::SessionPtr session1 = pool->get("stub/railsapp");
    Application::SessionPtr session2 = pool2->get("stub/railsapp");
    session1.reset();
    session2.reset();
    
    setenv("nextRestartTxtDeletionShouldFail", "1", 1);
    system("touch stub/railsapp/tmp/restart.txt");
    old_pid = pool->get("stub/railsapp")->getPid();
    ensure_equals(pool->getActive(), 0u);
    ensure_equals(pool->getCount(), 1u);
 
    sleep(1); // Allow the next mtime to be different.
    system("touch stub/railsapp/tmp/restart.txt");
    ensure("The app is restarted, and the last app instance was not reused",
      pool2->get("stub/railsapp")->getPid() != old_pid);
    
    unlink("stub/railsapp/tmp/restart.txt");
  }
  
  TEST_METHOD(15) {
    // Test whether restarting really results in code reload.
    system("cp -f stub/railsapp/app/controllers/bar_controller_1.rb "
      "stub/railsapp/app/controllers/bar_controller.rb");
    Application::SessionPtr session = pool->get("stub/railsapp");
    session->sendHeaders(createRequestHeaders("/bar"));
    string result = readAll(session->getStream());
    ensure(result.find("bar 1!"));
    session.reset();
    
    system("cp -f stub/railsapp/app/controllers/bar_controller_2.rb "
      "stub/railsapp/app/controllers/bar_controller.rb");
    system("touch stub/railsapp/tmp/restart.txt");
    session = pool->get("stub/railsapp");
    session->sendHeaders(createRequestHeaders("/bar"));
    result = readAll(session->getStream());
    ensure("App code has been reloaded", result.find("bar 2!"));
    unlink("stub/railsapp/app/controllers/bar_controller.rb");
  }
  
  TEST_METHOD(16) {
    // The cleaner thread should clean idle applications without crashing.
    pool->setMaxIdleTime(1);
    spawnRackApp(pool, "stub/rack");
    
    time_t begin = time(NULL);
    while (pool->getCount() == 1u && time(NULL) - begin < 10) {
      usleep(100000);
    }
    ensure_equals("App should have been cleaned up", pool->getCount(), 0u);
  }
  
  TEST_METHOD(17) {
    // MaxPerApp must be respected.
    pool->setMax(3);
    pool->setMaxPerApp(1);
    // TODO: how do we test this?
  }
  
  TEST_METHOD(18) {
    // Application instance is shutdown after 'maxRequests' requests.
    SpawnOptions options("stub/railsapp");
    int reader;
    pid_t originalPid;
    Application::SessionPtr session;
    
    options.maxRequests = 4;
    pool->setMax(1);
    session = pool->get(options);
    originalPid = session->getPid();
    session.reset();
    
    for (unsigned int i = 0; i < 4; i++) {
      session = pool->get(options);
      session->sendHeaders(createRequestHeaders());
      session->shutdownWriter();
      reader = session->getStream();
      readAll(reader);
      // Must explicitly call reset() here because we
      // want to close the session right now.
      session.reset();
      // In case of ApplicationPoolServer, we sleep here
      // for a little while to force a context switch to
      // the server, so that the session close event may
      // be processed.
      usleep(100000);
    }
    
    session = pool->get(options);
    ensure(session->getPid() != originalPid);
  }
  
  // TODO: test maxIdleTime == 0
 
#endif /* USE_TEMPLATE */