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 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039
|
#include <TestSupport.h>
#include <ApplicationPool2/Pool.h>
#include <Utils/IOUtils.h>
#include <Utils/StrIntUtils.h>
#include <json/json.h>
#include <MessageReadersWriters.h>
#include <map>
#include <vector>
#include <cerrno>
#include <signal.h>
using namespace std;
using namespace Passenger;
using namespace Passenger::ApplicationPool2;
namespace tut {
struct ApplicationPool2_PoolTest {
ServerInstanceDirPtr serverInstanceDir;
ServerInstanceDir::GenerationPtr generation;
SpawnerConfigPtr spawnerConfig;
SpawnerFactoryPtr spawnerFactory;
PoolPtr pool;
Pool::DebugSupportPtr debug;
Ticket ticket;
GetCallback callback;
SessionPtr currentSession;
ExceptionPtr currentException;
AtomicInt number;
boost::mutex syncher;
list<SessionPtr> sessions;
bool retainSessions;
ApplicationPool2_PoolTest() {
createServerInstanceDirAndGeneration(serverInstanceDir, generation);
retainSessions = false;
spawnerConfig = boost::make_shared<SpawnerConfig>(*resourceLocator);
spawnerFactory = boost::make_shared<SpawnerFactory>(generation,
spawnerConfig);
pool = boost::make_shared<Pool>(spawnerFactory);
pool->initialize();
callback = boost::bind(&ApplicationPool2_PoolTest::_callback, this, _1, _2);
setLogLevel(LVL_ERROR); // TODO: change to LVL_WARN
setPrintAppOutputAsDebuggingMessages(true);
}
~ApplicationPool2_PoolTest() {
// Explicitly destroy these here because they can run
// additional code that depend on other fields in this
// class.
TRACE_POINT();
clearAllSessions();
UPDATE_TRACE_POINT();
pool->destroy();
UPDATE_TRACE_POINT();
pool.reset();
setLogLevel(DEFAULT_LOG_LEVEL);
setPrintAppOutputAsDebuggingMessages(false);
SystemTime::releaseAll();
}
void initPoolDebugging() {
pool->initDebugging();
debug = pool->debugSupport;
}
void clearAllSessions() {
SessionPtr myCurrentSession;
list<SessionPtr> mySessions;
{
LockGuard l(syncher);
myCurrentSession = currentSession;
mySessions = sessions;
currentSession.reset();
sessions.clear();
}
myCurrentSession.reset();
mySessions.clear();
}
Options createOptions() {
Options options;
options.spawnMethod = "dummy";
options.appRoot = "stub/rack";
options.startCommand = "ruby\t" "start.rb";
options.startupFile = "start.rb";
options.loadShellEnvvars = false;
options.user = testConfig["normal_user_1"].asCString();
options.defaultUser = testConfig["default_user"].asCString();
options.defaultGroup = testConfig["default_group"].asCString();
return options;
}
void _callback(const SessionPtr &session, const ExceptionPtr &e) {
SessionPtr oldSession;
{
LockGuard l(syncher);
oldSession = currentSession;
currentSession = session;
currentException = e;
number++;
if (retainSessions && session != NULL) {
sessions.push_back(session);
}
}
// destroy old session object outside the lock.
}
void sendHeaders(int connection, ...) {
va_list ap;
const char *arg;
vector<StaticString> args;
va_start(ap, connection);
while ((arg = va_arg(ap, const char *)) != NULL) {
args.push_back(StaticString(arg, strlen(arg) + 1));
}
va_end(ap);
shared_array<StaticString> args_array(new StaticString[args.size() + 1]);
unsigned int totalSize = 0;
for (unsigned int i = 0; i < args.size(); i++) {
args_array[i + 1] = args[i];
totalSize += args[i].size();
}
char sizeHeader[sizeof(uint32_t)];
Uint32Message::generate(sizeHeader, totalSize);
args_array[0] = StaticString(sizeHeader, sizeof(uint32_t));
gatheredWrite(connection, args_array.get(), args.size() + 1, NULL);
}
string stripHeaders(const string &str) {
string::size_type pos = str.find("\r\n\r\n");
if (pos == string::npos) {
return str;
} else {
string result = str;
result.erase(0, pos + 4);
return result;
}
}
string sendRequest(const Options &options, const char *path) {
int oldNumber = number;
pool->asyncGet(options, callback);
EVENTUALLY(5,
result = number == oldNumber + 1;
);
if (currentException != NULL) {
P_ERROR("get() exception: " << currentException->what());
abort();
}
currentSession->initiate();
sendHeaders(currentSession->fd(),
"PATH_INFO", path,
"REQUEST_METHOD", "GET",
NULL);
shutdown(currentSession->fd(), SHUT_WR);
string body = stripHeaders(readAll(currentSession->fd()));
ProcessPtr process = currentSession->getProcess();
currentSession.reset();
EVENTUALLY(5,
result = process->busyness() == 0;
);
return body;
}
// Ensure that n processes exist.
Options ensureMinProcesses(unsigned int n) {
Options options = createOptions();
options.minProcesses = n;
pool->asyncGet(options, callback);
EVENTUALLY(5,
result = number == 1;
);
EVENTUALLY(5,
result = pool->getProcessCount() == n;
);
currentSession.reset();
return options;
}
void disableProcess(ProcessPtr process, AtomicInt *result) {
*result = (int) pool->disableProcess(process->gupid);
}
};
DEFINE_TEST_GROUP_WITH_LIMIT(ApplicationPool2_PoolTest, 100);
TEST_METHOD(1) {
// Test initial state.
ensure(!pool->atFullCapacity());
}
/*********** Test asyncGet() behavior on a single SuperGroup and Group ***********/
TEST_METHOD(2) {
// asyncGet() actions on empty pools cannot be immediately satisfied.
// Instead a new process will be spawned. In the mean time get()
// actions are put on a wait list which will be processed as soon
// as the new process is done spawning.
Options options = createOptions();
ScopedLock l(pool->syncher);
pool->asyncGet(options, callback, false);
ensure_equals(number, 0);
ensure(pool->getWaitlist.empty());
ensure(!pool->superGroups.empty());
l.unlock();
EVENTUALLY(5,
result = pool->getProcessCount() == 1;
);
ensure_equals(number, 1);
ensure(currentSession != NULL);
ensure(currentException == NULL);
}
TEST_METHOD(3) {
// If one matching process already exists and it's not at full
// capacity then asyncGet() will immediately use it.
Options options = createOptions();
// Spawn a process and opens a session with it.
pool->asyncGet(options, callback);
EVENTUALLY(5,
result = number == 1;
);
// Close the session so that the process is now idle.
ProcessPtr process = currentSession->getProcess();
currentSession.reset();
ensure_equals(process->busyness(), 0);
ensure(!process->isTotallyBusy());
// Verify test assertion.
ScopedLock l(pool->syncher);
pool->asyncGet(options, callback, false);
ensure_equals("callback is immediately called", number, 2);
}
TEST_METHOD(4) {
// If one matching process already exists but it's at full capacity,
// and the limits prevent spawning of a new process,
// then asyncGet() will put the get action on the group's wait
// queue. When the process is no longer at full capacity it will
// process the request.
// Spawn a process and verify that it's at full capacity.
// Keep its session open.
Options options = createOptions();
options.appGroupName = "test";
pool->setMax(1);
pool->asyncGet(options, callback);
EVENTUALLY(5,
result = number == 1;
);
SessionPtr session1 = currentSession;
ProcessPtr process = session1->getProcess();
currentSession.reset();
ensure_equals(process->sessions, 1);
ensure(process->isTotallyBusy());
// Now call asyncGet() again.
pool->asyncGet(options, callback);
ensure_equals("callback is not yet called", number, 1);
ensure_equals("the get action has been put on the wait list",
pool->superGroups.get("test")->defaultGroup->getWaitlist.size(), 1u);
session1.reset();
ensure_equals("callback is called after the process becomes idle",
number, 2);
ensure_equals("the get wait list has been processed",
pool->superGroups.get("test")->defaultGroup->getWaitlist.size(), 0u);
ensure_equals(process->sessions, 1);
}
TEST_METHOD(5) {
// If one matching process already exists but it's at full utilization,
// and the limits and pool capacity allow spawning of a new process,
// then get() will put the get action on the group's wait
// queue while spawning a process in the background.
// Either the existing process or the newly spawned process
// will process the action, whichever becomes first available.
// Here we test the case in which the existing process becomes
// available first.
initPoolDebugging();
// Spawn a regular process and keep its session open.
Options options = createOptions();
debug->messages->send("Proceed with spawn loop iteration 1");
SessionPtr session1 = pool->get(options, &ticket);
ProcessPtr process1 = session1->getProcess();
// Now spawn a process that never finishes.
pool->asyncGet(options, callback);
// Release the session on the first process.
session1.reset();
EVENTUALLY(1,
result = number == 1;
);
ensure_equals("The first process handled the second asyncGet() request",
currentSession->getProcess(), process1);
debug->messages->send("Proceed with spawn loop iteration 2");
EVENTUALLY(5,
result = number == 1;
);
}
TEST_METHOD(6) {
// Here we test the case in which the new process becomes
// available first.
// Spawn a regular process.
Options options = createOptions();
pool->asyncGet(options, callback);
EVENTUALLY(5,
result = number == 1;
);
SessionPtr session1 = currentSession;
ProcessPtr process1 = currentSession->getProcess();
currentSession.reset();
// As long as we don't release process1 the following get
// action will be processed by the newly spawned process.
pool->asyncGet(options, callback);
EVENTUALLY(5,
result = pool->getProcessCount() == 2;
);
ensure_equals(number, 2);
ensure(currentSession->getProcess() != process1);
}
TEST_METHOD(7) {
// If multiple matching processes exist, and one of them is idle,
// then asyncGet() will use that.
// Spawn 3 processes and keep a session open with 1 of them.
Options options = createOptions();
options.minProcesses = 3;
pool->asyncGet(options, callback);
EVENTUALLY(5,
result = number == 1;
);
EVENTUALLY(5,
result = pool->getProcessCount() == 3;
);
SessionPtr session1 = currentSession;
ProcessPtr process1 = currentSession->getProcess();
currentSession.reset();
// Now open another session. It should complete immediately
// and should not use the first process.
ScopedLock l(pool->syncher);
pool->asyncGet(options, callback, false);
ensure_equals("asyncGet() completed immediately", number, 2);
SessionPtr session2 = currentSession;
ProcessPtr process2 = currentSession->getProcess();
l.unlock();
currentSession.reset();
ensure(process2 != process1);
// Now open yet another session. It should also complete immediately
// and should not use the first or the second process.
l.lock();
pool->asyncGet(options, callback, false);
ensure_equals("asyncGet() completed immediately", number, 3);
SessionPtr session3 = currentSession;
ProcessPtr process3 = currentSession->getProcess();
l.unlock();
currentSession.reset();
ensure(process3 != process1);
ensure(process3 != process2);
}
TEST_METHOD(8) {
// If multiple matching processes exist, then asyncGet() will use
// the one with the smallest utilization number.
// Spawn 2 processes, each with a concurrency of 2.
Options options = createOptions();
options.minProcesses = 2;
pool->setMax(2);
GroupPtr group = pool->findOrCreateGroup(options);
spawnerConfig->concurrency = 2;
{
LockGuard l(pool->syncher);
group->spawn();
}
EVENTUALLY(5,
result = pool->getProcessCount() == 2;
);
// asyncGet() selects some process.
pool->asyncGet(options, callback);
ensure_equals(number, 1);
SessionPtr session1 = currentSession;
ProcessPtr process1 = currentSession->getProcess();
currentSession.reset();
// The first process now has 1 session, so next asyncGet() should
// select the other process.
pool->asyncGet(options, callback);
ensure_equals(number, 2);
SessionPtr session2 = currentSession;
ProcessPtr process2 = currentSession->getProcess();
currentSession.reset();
ensure("(1)", process1 != process2);
// Both processes now have an equal number of sessions. Next asyncGet()
// can select either.
pool->asyncGet(options, callback);
ensure_equals(number, 3);
SessionPtr session3 = currentSession;
ProcessPtr process3 = currentSession->getProcess();
currentSession.reset();
// One process now has the lowest number of sessions. Next
// asyncGet() should select that one.
pool->asyncGet(options, callback);
ensure_equals(number, 4);
SessionPtr session4 = currentSession;
ProcessPtr process4 = currentSession->getProcess();
currentSession.reset();
ensure(process3 != process4);
}
TEST_METHOD(9) {
// If multiple matching processes exist, and all of them are at full capacity,
// and no more processes may be spawned,
// then asyncGet() will put the action on the group's wait queue.
// The process that first becomes not at full capacity will process the action.
// Spawn 2 processes and open 4 sessions.
Options options = createOptions();
options.appGroupName = "test";
options.minProcesses = 2;
pool->setMax(2);
spawnerConfig->concurrency = 2;
vector<SessionPtr> sessions;
int expectedNumber = 1;
for (int i = 0; i < 4; i++) {
pool->asyncGet(options, callback);
EVENTUALLY(5,
result = number == expectedNumber;
);
expectedNumber++;
sessions.push_back(currentSession);
currentSession.reset();
}
EVENTUALLY(5,
result = pool->getProcessCount() == 2;
);
SuperGroupPtr superGroup = pool->superGroups.get("test");
ensure_equals(superGroup->groups[0]->getWaitlist.size(), 0u);
ensure(pool->atFullCapacity());
// Now try to open another session.
pool->asyncGet(options, callback);
ensure_equals("The get request has been put on the wait list",
pool->superGroups.get("test")->groups[0]->getWaitlist.size(), 1u);
// Close an existing session so that one process is no
// longer at full utilization.
sessions[0].reset();
ensure_equals("The get request has been removed from the wait list",
pool->superGroups.get("test")->groups[0]->getWaitlist.size(), 0u);
ensure(pool->atFullCapacity());
}
TEST_METHOD(10) {
// If multiple matching processes exist, and all of them are at full utilization,
// and a new process may be spawned,
// then asyncGet() will put the action on the group's wait queue and spawn the
// new process.
// The process that first becomes not at full utilization
// or the newly spawned process
// will process the action, whichever is earlier.
// Here we test the case where an existing process is earlier.
// Spawn 2 processes and open 4 sessions.
Options options = createOptions();
options.minProcesses = 2;
pool->setMax(3);
GroupPtr group = pool->findOrCreateGroup(options);
spawnerConfig->concurrency = 2;
vector<SessionPtr> sessions;
int expectedNumber = 1;
for (int i = 0; i < 4; i++) {
pool->asyncGet(options, callback);
EVENTUALLY(5,
result = number == expectedNumber;
);
expectedNumber++;
sessions.push_back(currentSession);
currentSession.reset();
}
EVENTUALLY(5,
result = pool->getProcessCount() == 2;
);
// The next asyncGet() should spawn a new process and the action should be queued.
ScopedLock l(pool->syncher);
spawnerConfig->spawnTime = 5000000;
pool->asyncGet(options, callback, false);
ensure(group->spawning());
ensure_equals(group->getWaitlist.size(), 1u);
l.unlock();
// Close one of the sessions. Now it will process the action.
ProcessPtr process = sessions[0]->getProcess();
sessions[0].reset();
ensure_equals(number, 5);
ensure_equals(currentSession->getProcess(), process);
ensure_equals(group->getWaitlist.size(), 0u);
ensure_equals(pool->getProcessCount(), 2u);
}
TEST_METHOD(11) {
// Here we test the case where the newly spawned process is earlier.
// Spawn 2 processes and open 4 sessions.
Options options = createOptions();
options.minProcesses = 2;
pool->setMax(3);
GroupPtr group = pool->findOrCreateGroup(options);
spawnerConfig->concurrency = 2;
vector<SessionPtr> sessions;
int expectedNumber = 1;
for (int i = 0; i < 4; i++) {
pool->asyncGet(options, callback);
EVENTUALLY(5,
result = number == expectedNumber;
);
expectedNumber++;
sessions.push_back(currentSession);
currentSession.reset();
}
EVENTUALLY(5,
result = pool->getProcessCount() == 2;
);
// The next asyncGet() should spawn a new process. After it's done
// spawning it will process the action.
pool->asyncGet(options, callback);
EVENTUALLY(5,
result = pool->getProcessCount() == 3;
);
EVENTUALLY(5,
result = number == 5;
);
ensure_equals(currentSession->getProcess()->pid, 3);
ensure_equals(group->getWaitlist.size(), 0u);
}
TEST_METHOD(12) {
// Test shutting down.
ensureMinProcesses(2);
ensure(pool->detachSuperGroupByName("stub/rack"));
ensure_equals(pool->getSuperGroupCount(), 0u);
}
TEST_METHOD(13) {
// Test shutting down while Group is restarting.
initPoolDebugging();
debug->messages->send("Proceed with spawn loop iteration 1");
ensureMinProcesses(1);
ensure(pool->restartGroupByName("stub/rack#default"));
debug->debugger->recv("About to end restarting");
ensure(pool->detachSuperGroupByName("stub/rack"));
ensure_equals(pool->getSuperGroupCount(), 0u);
}
TEST_METHOD(14) {
// Test shutting down while Group is spawning.
initPoolDebugging();
Options options = createOptions();
pool->asyncGet(options, callback);
debug->debugger->recv("Begin spawn loop iteration 1");
ensure(pool->detachSuperGroupByName("stub/rack"));
ensure_equals(pool->getSuperGroupCount(), 0u);
}
TEST_METHOD(15) {
// Test shutting down while SuperGroup is initializing.
initPoolDebugging();
debug->spawning = false;
debug->superGroup = true;
Options options = createOptions();
pool->asyncGet(options, callback);
debug->debugger->recv("About to finish SuperGroup initialization");
ensure(pool->detachSuperGroupByName("stub/rack"));
ensure_equals(pool->getSuperGroupCount(), 0u);
}
TEST_METHOD(16) {
// Test shutting down while SuperGroup is restarting.
initPoolDebugging();
debug->spawning = false;
debug->superGroup = true;
debug->messages->send("Proceed with initializing SuperGroup");
ensureMinProcesses(1);
ensure_equals(pool->restartSuperGroupsByAppRoot("stub/rack"), 1u);
debug->debugger->recv("About to finish SuperGroup restart");
ensure(pool->detachSuperGroupByName("stub/rack"));
ensure_equals(pool->getSuperGroupCount(), 0u);
}
TEST_METHOD(17) {
// Test that restartGroupByName() spawns more processes to ensure
// that minProcesses and other constraints are met.
ensureMinProcesses(1);
ensure(pool->restartGroupByName("stub/rack#default"));
EVENTUALLY(5,
result = pool->getProcessCount() == 1;
);
}
TEST_METHOD(18) {
// Test getting from an app for which minProcesses is set to 0,
// and restart.txt already existed.
TempDirCopy dir("stub/wsgi", "tmp.wsgi");
Options options = createOptions();
options.appRoot = "tmp.wsgi";
options.appType = "wsgi";
options.startupFile = "passenger_wsgi.py";
options.spawnMethod = "direct";
options.minProcesses = 0;
initPoolDebugging();
debug->spawning = false;
touchFile("tmp.wsgi/tmp/restart.txt", 1);
pool->asyncGet(options, callback, false);
debug->debugger->recv("About to end restarting");
debug->messages->send("Finish restarting");
EVENTUALLY(5,
result = number == 1;
);
ensure_equals(pool->getProcessCount(), 1u);
}
/*********** Test asyncGet() behavior on multiple SuperGroups,
each with a single Group ***********/
TEST_METHOD(20) {
// If the pool is full, and one tries to asyncGet() from a nonexistant group,
// then it will kill the oldest idle process and spawn a new process.
Options options = createOptions();
pool->setMax(2);
// Get from /foo and close its session immediately.
options.appRoot = "/foo";
pool->asyncGet(options, callback);
EVENTUALLY(5,
result = number == 1;
);
ProcessPtr process1 = currentSession->getProcess();
GroupPtr group1 = process1->getGroup();
SuperGroupPtr superGroup1 = group1->getSuperGroup();
currentSession.reset();
// Get from /bar and keep its session open.
options.appRoot = "/bar";
pool->asyncGet(options, callback);
EVENTUALLY(5,
result = number == 2;
);
SessionPtr session2 = currentSession;
currentSession.reset();
// Get from /baz. The process for /foo should be killed now.
options.appRoot = "/baz";
pool->asyncGet(options, callback);
EVENTUALLY(5,
result = number == 3;
);
ensure_equals(pool->getProcessCount(), 2u);
ensure_equals(superGroup1->getProcessCount(), 0u);
}
TEST_METHOD(21) {
// If the pool is full, and one tries to asyncGet() from a nonexistant group,
// and all existing processes are non-idle, then it will
// kill the oldest process and spawn a new process.
Options options = createOptions();
pool->setMax(2);
// Get from /foo and close its session immediately.
options.appRoot = "/foo";
pool->asyncGet(options, callback);
EVENTUALLY(5,
result = number == 1;
);
ProcessPtr process1 = currentSession->getProcess();
GroupPtr group1 = process1->getGroup();
SuperGroupPtr superGroup1 = group1->getSuperGroup();
// Get from /bar and keep its session open.
options.appRoot = "/bar";
pool->asyncGet(options, callback);
EVENTUALLY(5,
result = number == 2;
);
SessionPtr session2 = currentSession;
currentSession.reset();
// Get from /baz. The process for /foo should be killed now.
options.appRoot = "/baz";
pool->asyncGet(options, callback);
EVENTUALLY(5,
result = number == 3;
);
ensure_equals(pool->getProcessCount(), 2u);
ensure_equals(superGroup1->getProcessCount(), 0u);
}
TEST_METHOD(22) {
// Suppose the pool is at full capacity, and one tries to asyncGet() from an
// existant group that does not have any processes. It should kill a process
// from another group, and the request should succeed.
Options options = createOptions();
SessionPtr session;
pid_t pid1, pid2;
pool->setMax(1);
// Create a group /foo.
options.appRoot = "/foo";
SystemTime::force(1);
session = pool->get(options, &ticket);
pid1 = session->getPid();
session.reset();
// Create a group /bar.
options.appRoot = "/bar";
SystemTime::force(2);
session = pool->get(options, &ticket);
pid2 = session->getPid();
session.reset();
// Sleep for a short while to give Pool a chance to shutdown
// the first process.
usleep(300000);
ensure_equals("(1)", pool->getProcessCount(), 1u);
// Get from /foo.
options.appRoot = "/foo";
SystemTime::force(3);
session = pool->get(options, &ticket);
ensure("(2)", session->getPid() != pid1);
ensure("(3)", session->getPid() != pid2);
ensure_equals("(4)", pool->getProcessCount(), 1u);
}
TEST_METHOD(23) {
// Suppose the pool is at full capacity, and one tries to asyncGet() from an
// existant group that does not have any processes, and that happens to need
// restarting. It should kill a process from another group and the request
// should succeed.
Options options1 = createOptions();
Options options2 = createOptions();
TempDirCopy dir("stub/wsgi", "tmp.wsgi");
SessionPtr session;
pid_t pid1, pid2;
pool->setMax(1);
// Create a group tmp.wsgi.
options1.appRoot = "tmp.wsgi";
options1.appType = "wsgi";
options1.startupFile = "passenger_wsgi.py";
options1.spawnMethod = "direct";
SystemTime::force(1);
session = pool->get(options1, &ticket);
pid1 = session->getPid();
session.reset();
// Create a group bar.
options2.appRoot = "bar";
SystemTime::force(2);
session = pool->get(options2, &ticket);
pid2 = session->getPid();
session.reset();
// Sleep for a short while to give Pool a chance to shutdown
// the first process.
usleep(300000);
ensure_equals("(1)", pool->getProcessCount(), 1u);
// Get from tmp.wsgi.
SystemTime::force(3);
touchFile("tmp.wsgi/tmp/restart.txt", 4);
session = pool->get(options1, &ticket);
ensure("(2)", session->getPid() != pid1);
ensure("(3)", session->getPid() != pid2);
ensure_equals("(4)", pool->getProcessCount(), 1u);
}
TEST_METHOD(24) {
// Suppose the pool is at full capacity, with two groups:
// - one that is spawning a process.
// - one with no processes.
// When one tries to asyncGet() from the second group, there should
// be no process to kill, but when the first group is done spawning
// it should throw away that process immediately to allow the second
// group to spawn.
Options options1 = createOptions();
Options options2 = createOptions();
initPoolDebugging();
debug->restarting = false;
pool->setMax(1);
// Create a group foo.
options1.appRoot = "foo";
options1.noop = true;
SystemTime::force(1);
pool->get(options1, &ticket);
// Create a group bar, but don't let it finish spawning.
options2.appRoot = "bar";
options2.noop = true;
SystemTime::force(2);
GroupPtr barGroup = pool->get(options2, &ticket)->getGroup();
{
LockGuard l(pool->syncher);
ensure_equals("(1)", barGroup->spawn(), SR_OK);
}
debug->debugger->recv("Begin spawn loop iteration 1");
// Now get from foo again and let the request be queued.
options1.noop = false;
SystemTime::force(3);
pool->asyncGet(options1, callback);
// Nothing should happen while bar is spawning.
SHOULD_NEVER_HAPPEN(100,
result = number > 0;
);
ensure_equals("(2)", pool->getProcessCount(), 0u);
// Now let bar finish spawning. Eventually there should
// only be one process: the one for foo.
debug->messages->send("Proceed with spawn loop iteration 1");
debug->debugger->recv("Spawn loop done");
debug->messages->send("Proceed with spawn loop iteration 2");
debug->debugger->recv("Spawn loop done");
EVENTUALLY(5,
LockGuard l(pool->syncher);
vector<ProcessPtr> processes = pool->getProcesses(false);
if (processes.size() == 1) {
GroupPtr group = processes[0]->getGroup();
result = group->name == "foo#default";
} else {
result = false;
}
);
}
TEST_METHOD(25) {
// Suppose the pool is at full capacity, with two groups:
// - one that is spawning a process, and has a queued request.
// - one with no processes.
// When one tries to asyncGet() from the second group, there should
// be no process to kill, but when the first group is done spawning
// it should throw away that process immediately to allow the second
// group to spawn.
Options options1 = createOptions();
Options options2 = createOptions();
initPoolDebugging();
debug->restarting = false;
pool->setMax(1);
// Create a group foo.
options1.appRoot = "foo";
options1.noop = true;
SystemTime::force(1);
pool->get(options1, &ticket);
// Create a group bar with a queued request, but don't let it finish spawning.
options2.appRoot = "bar";
SystemTime::force(2);
pool->asyncGet(options2, callback);
debug->debugger->recv("Begin spawn loop iteration 1");
// Now get from foo again and let the request be queued.
options1.noop = false;
SystemTime::force(3);
pool->asyncGet(options1, callback);
// Nothing should happen while bar is spawning.
SHOULD_NEVER_HAPPEN(100,
result = number > 0;
);
ensure_equals("(1)", pool->getProcessCount(), 0u);
// Now let bar finish spawning. The request for bar should be served.
debug->messages->send("Proceed with spawn loop iteration 1");
debug->debugger->recv("Spawn loop done");
EVENTUALLY(5,
result = number == 1;
);
ensure_equals(currentSession->getGroup()->name, "bar#default");
// When that request is done, the process for bar should be killed,
// and a process for foo should be spawned.
currentSession.reset();
debug->messages->send("Proceed with spawn loop iteration 2");
debug->debugger->recv("Spawn loop done");
EVENTUALLY(5,
LockGuard l(pool->syncher);
vector<ProcessPtr> processes = pool->getProcesses(false);
if (processes.size() == 1) {
GroupPtr group = processes[0]->getGroup();
result = group->name == "foo#default";
} else {
result = false;
}
);
EVENTUALLY(5,
result = number == 2;
);
}
/*********** Test detachProcess() ***********/
TEST_METHOD(30) {
// detachProcess() detaches the process from the group. The pool
// will restore the minimum number of processes afterwards.
Options options = createOptions();
options.appGroupName = "test";
options.minProcesses = 2;
pool->asyncGet(options, callback);
EVENTUALLY(5,
result = pool->getProcessCount() == 2;
);
EVENTUALLY(5,
result = number == 1;
);
ProcessPtr process = currentSession->getProcess();
pool->detachProcess(currentSession->getProcess());
{
LockGuard l(pool->syncher);
ensure(process->enabled == Process::DETACHED);
}
EVENTUALLY(5,
result = pool->getProcessCount() == 2;
);
currentSession.reset();
EVENTUALLY(5,
result = process->isDead();
);
}
TEST_METHOD(31) {
// If the containing group had waiters on it, and detachProcess()
// detaches the only process in the group, then a new process
// is automatically spawned to handle the waiters.
Options options = createOptions();
options.appGroupName = "test";
pool->setMax(1);
spawnerConfig->spawnTime = 1000000;
pool->asyncGet(options, callback);
EVENTUALLY(5,
result = number == 1;
);
SessionPtr session1 = currentSession;
currentSession.reset();
pool->asyncGet(options, callback);
{
LockGuard l(pool->syncher);
ensure_equals(pool->superGroups.get("test")->defaultGroup->getWaitlist.size(), 1u);
}
pool->detachProcess(session1->getProcess());
{
LockGuard l(pool->syncher);
ensure(pool->superGroups.get("test")->defaultGroup->spawning());
ensure_equals(pool->superGroups.get("test")->defaultGroup->enabledCount, 0);
ensure_equals(pool->superGroups.get("test")->defaultGroup->getWaitlist.size(), 1u);
}
EVENTUALLY(5,
result = number == 2;
);
}
TEST_METHOD(32) {
// If the pool had waiters on it then detachProcess() will
// automatically create the SuperGroups that were requested
// by the waiters.
Options options = createOptions();
options.appGroupName = "test";
options.minProcesses = 0;
pool->setMax(1);
spawnerConfig->spawnTime = 30000;
// Begin spawning a process.
pool->asyncGet(options, callback);
ensure(pool->atFullCapacity());
// asyncGet() on another group should now put it on the waiting list.
Options options2 = createOptions();
options2.appGroupName = "test2";
options2.minProcesses = 0;
spawnerConfig->spawnTime = 90000;
pool->asyncGet(options2, callback);
{
LockGuard l(pool->syncher);
ensure_equals(pool->getWaitlist.size(), 1u);
}
// Eventually the dummy process for "test" is now done spawning.
// We then detach it.
EVENTUALLY(5,
result = number == 1;
);
SessionPtr session1 = currentSession;
currentSession.reset();
pool->detachProcess(session1->getProcess());
{
LockGuard l(pool->syncher);
ensure(pool->superGroups.get("test2") != NULL);
ensure_equals(pool->getWaitlist.size(), 0u);
}
EVENTUALLY(5,
result = number == 2;
);
}
TEST_METHOD(33) {
// A SuperGroup does not become garbage collectable
// after detaching all its processes.
Options options = createOptions();
pool->asyncGet(options, callback);
EVENTUALLY(5,
result = number == 1;
);
ProcessPtr process = currentSession->getProcess();
currentSession.reset();
SuperGroupPtr superGroup = process->getSuperGroup();
pool->detachProcess(process);
LockGuard l(pool->syncher);
ensure_equals(pool->superGroups.size(), 1u);
ensure(superGroup->isAlive());
ensure(!superGroup->garbageCollectable());
}
TEST_METHOD(34) {
// When detaching a process, it waits until all sessions have
// finished before telling the process to shut down.
Options options = createOptions();
options.spawnMethod = "direct";
options.minProcesses = 0;
SessionPtr session = pool->get(options, &ticket);
ProcessPtr process = session->getProcess();
ensure(pool->detachProcess(process));
{
LockGuard l(pool->syncher);
ensure_equals(process->enabled, Process::DETACHED);
}
SHOULD_NEVER_HAPPEN(100,
LockGuard l(pool->syncher);
result = !process->isAlive()
|| !process->osProcessExists();
);
session.reset();
EVENTUALLY(1,
LockGuard l(pool->syncher);
result = process->enabled == Process::DETACHED
&& !process->osProcessExists()
&& process->isDead();
);
}
TEST_METHOD(35) {
// When detaching a process, it waits until the OS processes
// have exited before cleaning up the in-memory data structures.
Options options = createOptions();
options.spawnMethod = "direct";
options.minProcesses = 0;
ProcessPtr process = pool->get(options, &ticket)->getProcess();
ScopeGuard g(boost::bind(::kill, process->pid, SIGCONT));
kill(process->pid, SIGSTOP);
ensure(pool->detachProcess(process));
{
LockGuard l(pool->syncher);
ensure_equals(process->enabled, Process::DETACHED);
}
EVENTUALLY(1,
result = process->getLifeStatus() == Process::SHUTDOWN_TRIGGERED;
);
SHOULD_NEVER_HAPPEN(100,
LockGuard l(pool->syncher);
result = process->isDead()
|| !process->osProcessExists();
);
kill(process->pid, SIGCONT);
g.clear();
EVENTUALLY(1,
LockGuard l(pool->syncher);
result = process->enabled == Process::DETACHED
&& !process->osProcessExists()
&& process->isDead();
);
}
TEST_METHOD(36) {
// Detaching a process that is already being detached, works.
Options options = createOptions();
options.appGroupName = "test";
options.minProcesses = 0;
initPoolDebugging();
debug->restarting = false;
debug->spawning = false;
debug->detachedProcessesChecker = true;
pool->asyncGet(options, callback);
EVENTUALLY(5,
result = pool->getProcessCount() == 1;
);
EVENTUALLY(5,
result = number == 1;
);
ProcessPtr process = currentSession->getProcess();
pool->detachProcess(currentSession->getProcess());
debug->debugger->recv("About to start detached processes checker");
{
LockGuard l(pool->syncher);
ensure(process->enabled == Process::DETACHED);
}
pool->detachProcess(currentSession->getProcess());
debug->messages->send("Proceed with starting detached processes checker");
debug->messages->send("Proceed with starting detached processes checker");
EVENTUALLY(5,
result = pool->getProcessCount() == 0;
);
currentSession.reset();
EVENTUALLY(5,
result = process->isDead();
);
}
/*********** Test disabling and enabling processes ***********/
TEST_METHOD(40) {
// Disabling a process under idle conditions should succeed immediately.
ensureMinProcesses(2);
vector<ProcessPtr> processes = pool->getProcesses();
ensure_equals("Disabling succeeds",
pool->disableProcess(processes[0]->gupid), DR_SUCCESS);
LockGuard l(pool->syncher);
ensure(processes[0]->isAlive());
ensure_equals("Process is disabled",
processes[0]->enabled,
Process::DISABLED);
ensure("Other processes are not affected",
processes[1]->isAlive());
ensure_equals("Other processes are not affected",
processes[1]->enabled, Process::ENABLED);
}
TEST_METHOD(41) {
// Disabling the sole process in a group, in case the pool settings allow
// spawning another process, should trigger a new process spawn.
ensureMinProcesses(1);
Options options = createOptions();
SessionPtr session = pool->get(options, &ticket);
ensure_equals(pool->getProcessCount(), 1u);
ensure(!pool->isSpawning());
spawnerConfig->spawnTime = 60000;
AtomicInt code = -1;
TempThread thr(boost::bind(&ApplicationPool2_PoolTest::disableProcess,
this, session->getProcess(), &code));
EVENTUALLY2(100, 1,
result = pool->isSpawning();
);
EVENTUALLY(1,
result = pool->getProcessCount() == 2u;
);
ensure_equals((int) code, -1);
session.reset();
EVENTUALLY(1,
result = code == (int) DR_SUCCESS;
);
}
TEST_METHOD(42) {
// Disabling the sole process in a group, in case pool settings don't allow
// spawning another process, should fail.
pool->setMax(1);
ensureMinProcesses(1);
vector<ProcessPtr> processes = pool->getProcesses();
ensure_equals("(1)", processes.size(), 1u);
DisableResult result = pool->disableProcess(processes[0]->gupid);
ensure_equals("(2)", result, DR_ERROR);
ensure_equals("(3)", pool->getProcessCount(), 1u);
}
TEST_METHOD(43) {
// If there are no enabled processes in the group, then disabling should
// succeed after the new process has been spawned.
initPoolDebugging();
debug->messages->send("Proceed with spawn loop iteration 1");
debug->messages->send("Proceed with spawn loop iteration 2");
Options options = createOptions();
SessionPtr session1 = pool->get(options, &ticket);
SessionPtr session2 = pool->get(options, &ticket);
ensure_equals(pool->getProcessCount(), 2u);
GroupPtr group = session1->getGroup();
AtomicInt code1 = -1, code2 = -1;
TempThread thr(boost::bind(&ApplicationPool2_PoolTest::disableProcess,
this, session1->getProcess(), &code1));
TempThread thr2(boost::bind(&ApplicationPool2_PoolTest::disableProcess,
this, session2->getProcess(), &code2));
EVENTUALLY(2,
LockGuard l(pool->syncher);
result = group->enabledCount == 0
&& group->disablingCount == 2
&& group->disabledCount == 0;
);
session1.reset();
session2.reset();
SHOULD_NEVER_HAPPEN(20,
result = code1 != -1 || code2 != -1;
);
debug->messages->send("Proceed with spawn loop iteration 3");
EVENTUALLY(5,
result = code1 == DR_SUCCESS;
);
EVENTUALLY(5,
result = code2 == DR_SUCCESS;
);
{
LockGuard l(pool->syncher);
ensure_equals(group->enabledCount, 1);
ensure_equals(group->disablingCount, 0);
ensure_equals(group->disabledCount, 2);
}
}
TEST_METHOD(44) {
// Suppose that a previous disable command triggered a new process spawn,
// and the spawn fails. Then any disabling processes should become enabled
// again, and the callbacks for the previous disable commands should be called.
initPoolDebugging();
debug->messages->send("Proceed with spawn loop iteration 1");
debug->messages->send("Proceed with spawn loop iteration 2");
Options options = createOptions();
options.minProcesses = 2;
SessionPtr session1 = pool->get(options, &ticket);
SessionPtr session2 = pool->get(options, &ticket);
ensure_equals(pool->getProcessCount(), 2u);
AtomicInt code1 = -1, code2 = -1;
TempThread thr(boost::bind(&ApplicationPool2_PoolTest::disableProcess,
this, session1->getProcess(), &code1));
TempThread thr2(boost::bind(&ApplicationPool2_PoolTest::disableProcess,
this, session2->getProcess(), &code2));
EVENTUALLY(2,
GroupPtr group = session1->getGroup();
LockGuard l(pool->syncher);
result = group->enabledCount == 0
&& group->disablingCount == 2
&& group->disabledCount == 0;
);
SHOULD_NEVER_HAPPEN(20,
result = code1 != -1 || code2 != -1;
);
setLogLevel(-2);
debug->messages->send("Fail spawn loop iteration 3");
EVENTUALLY(5,
result = code1 == DR_ERROR;
);
EVENTUALLY(5,
result = code2 == DR_ERROR;
);
{
GroupPtr group = session1->getGroup();
LockGuard l(pool->syncher);
ensure_equals(group->enabledCount, 2);
ensure_equals(group->disablingCount, 0);
ensure_equals(group->disabledCount, 0);
}
}
// TODO: asyncGet() should not select a disabling process if there are enabled processes.
// TODO: asyncGet() should not select a disabling process when non-rolling restarting.
// TODO: asyncGet() should select a disabling process if there are no enabled processes
// in the group. If this happens then asyncGet() will also spawn a new process.
// TODO: asyncGet() should not select a disabled process.
// TODO: If there are no enabled processes and all disabling processes are at full
// utilization, and the process that was being spawned becomes available
// earlier than any of the disabling processes, then the newly spawned process
// should handle the request.
// TODO: A disabling process becomes disabled as soon as it's done with
// all its request.
TEST_METHOD(50) {
// Disabling a process that's already being disabled should result in the
// callback being called after disabling is done.
ensureMinProcesses(2);
Options options = createOptions();
SessionPtr session = pool->get(options, &ticket);
AtomicInt code = -1;
TempThread thr(boost::bind(&ApplicationPool2_PoolTest::disableProcess,
this, session->getProcess(), &code));
SHOULD_NEVER_HAPPEN(100,
result = code != -1;
);
session.reset();
EVENTUALLY(1,
result = code != -1;
);
ensure_equals(code, (int) DR_SUCCESS);
}
// TODO: Enabling a process that's disabled succeeds immediately.
// TODO: Enabling a process that's disabling succeeds immediately. The disable
// callbacks will be called with DR_CANCELED.
TEST_METHOD(51) {
// If the number of processes is already at maximum, then disabling
// a process will cause that process to be disabled, without spawning
// a new process.
pool->setMax(2);
ensureMinProcesses(2);
vector<ProcessPtr> processes = pool->getProcesses();
ensure_equals(processes.size(), 2u);
DisableResult result = pool->disableProcess(processes[0]->gupid);
ensure_equals(result, DR_SUCCESS);
{
ScopedLock l(pool->syncher);
GroupPtr group = processes[0]->getGroup();
ensure_equals(group->enabledCount, 1);
ensure_equals(group->disablingCount, 0);
ensure_equals(group->disabledCount, 1);
}
}
/*********** Other tests ***********/
TEST_METHOD(60) {
// The pool is considered to be at full capacity if and only
// if all SuperGroups are at full capacity.
Options options = createOptions();
Options options2 = createOptions();
options2.appGroupName = "test";
pool->setMax(2);
pool->asyncGet(options, callback);
EVENTUALLY(5,
result = number == 1;
);
pool->asyncGet(options2, callback);
EVENTUALLY(5,
result = number == 2;
);
ensure_equals(pool->getProcessCount(), 2u);
ensure(pool->atFullCapacity());
clearAllSessions();
pool->detachSuperGroupByName("test");
ensure(!pool->atFullCapacity());
}
TEST_METHOD(61) {
// If the pool is at full capacity, then increasing 'max' will cause
// new processes to be spawned. Any queued get requests are processed
// as those new processes become available or as existing processes
// become available.
Options options = createOptions();
retainSessions = true;
pool->setMax(1);
pool->asyncGet(options, callback);
pool->asyncGet(options, callback);
pool->asyncGet(options, callback);
EVENTUALLY(5,
result = number == 1;
);
pool->setMax(4);
EVENTUALLY(5,
result = number == 3;
);
ensure_equals(pool->getProcessCount(), 3u);
}
TEST_METHOD(62) {
// Each spawned process has a GUPID, which can be looked up
// through findProcessByGupid().
Options options = createOptions();
pool->asyncGet(options, callback);
EVENTUALLY(5,
result = number == 1;
);
string gupid = currentSession->getProcess()->gupid;
ensure(!gupid.empty());
ensure_equals(currentSession->getProcess(), pool->findProcessByGupid(gupid));
}
TEST_METHOD(63) {
// findProcessByGupid() returns a NULL pointer if there is
// no matching process.
ensure(pool->findProcessByGupid("none") == NULL);
}
TEST_METHOD(64) {
// Test process idle cleaning.
Options options = createOptions();
pool->setMaxIdleTime(50000);
SessionPtr session1 = pool->get(options, &ticket);
SessionPtr session2 = pool->get(options, &ticket);
ensure_equals(pool->getProcessCount(), 2u);
session2.reset();
// One of the processes still has a session open and should
// not be idle cleaned.
EVENTUALLY(2,
result = pool->getProcessCount() == 1;
);
SHOULD_NEVER_HAPPEN(150,
result = pool->getProcessCount() == 0;
);
// It shouldn't clean more processes than minInstances allows.
sessions.clear();
SHOULD_NEVER_HAPPEN(150,
result = pool->getProcessCount() == 0;
);
}
TEST_METHOD(65) {
// Test spawner idle cleaning.
Options options = createOptions();
options.appGroupName = "test1";
Options options2 = createOptions();
options2.appGroupName = "test2";
retainSessions = true;
pool->setMaxIdleTime(50000);
pool->asyncGet(options, callback);
pool->asyncGet(options2, callback);
EVENTUALLY(2,
result = number == 2;
);
ensure_equals(pool->getProcessCount(), 2u);
EVENTUALLY(2,
SpawnerPtr spawner = pool->getSuperGroup("test1")->defaultGroup->spawner;
result = static_pointer_cast<DummySpawner>(spawner)->cleanCount >= 1;
);
EVENTUALLY(2,
SpawnerPtr spawner = pool->getSuperGroup("test2")->defaultGroup->spawner;
result = static_pointer_cast<DummySpawner>(spawner)->cleanCount >= 1;
);
}
TEST_METHOD(66) {
// It should restart the app if restart.txt is created or updated.
TempDirCopy dir("stub/wsgi", "tmp.wsgi");
Options options = createOptions();
options.appRoot = "tmp.wsgi";
options.appType = "wsgi";
options.startupFile = "passenger_wsgi.py";
options.spawnMethod = "direct";
pool->setMax(1);
// Send normal request.
ensure_equals(sendRequest(options, "/"), "front page");
// Modify application; it shouldn't have effect yet.
writeFile("tmp.wsgi/passenger_wsgi.py",
"def application(env, start_response):\n"
" start_response('200 OK', [('Content-Type', 'text/html')])\n"
" return ['restarted']\n");
ensure_equals(sendRequest(options, "/"), "front page");
// Create restart.txt and send request again. The change should now be activated.
touchFile("tmp.wsgi/tmp/restart.txt", 1);
ensure_equals(sendRequest(options, "/"), "restarted");
// Modify application again; it shouldn't have effect yet.
writeFile("tmp.wsgi/passenger_wsgi.py",
"def application(env, start_response):\n"
" start_response('200 OK', [('Content-Type', 'text/html')])\n"
" return ['restarted 2']\n");
ensure_equals(sendRequest(options, "/"), "restarted");
// Touch restart.txt and send request again. The change should now be activated.
touchFile("tmp.wsgi/tmp/restart.txt", 2);
ensure_equals(sendRequest(options, "/"), "restarted 2");
}
TEST_METHOD(67) {
// Test spawn exceptions.
TempDirCopy dir("stub/wsgi", "tmp.wsgi");
Options options = createOptions();
options.appRoot = "tmp.wsgi";
options.appType = "wsgi";
options.startupFile = "passenger_wsgi.py";
options.spawnMethod = "direct";
writeFile("tmp.wsgi/passenger_wsgi.py",
"import sys\n"
"sys.stderr.write('Something went wrong!')\n"
"exit(1)\n");
setLogLevel(-2);
pool->asyncGet(options, callback);
EVENTUALLY(5,
result = number == 1;
);
ensure(currentException != NULL);
boost::shared_ptr<SpawnException> e = dynamic_pointer_cast<SpawnException>(currentException);
ensure(e->getErrorPage().find("Something went wrong!") != string::npos);
}
TEST_METHOD(68) {
// If a process fails to spawn, then it stops trying to spawn minProcesses processes.
TempDirCopy dir("stub/wsgi", "tmp.wsgi");
Options options = createOptions();
options.appRoot = "tmp.wsgi";
options.appType = "wsgi";
options.startupFile = "passenger_wsgi.py";
options.spawnMethod = "direct";
options.minProcesses = 4;
writeFile("tmp.wsgi/counter", "0");
chmod("tmp.wsgi/counter", 0666);
// Our application starts successfully the first two times,
// and fails all the other times.
writeFile("tmp.wsgi/passenger_wsgi.py",
"import sys\n"
"def application(env, start_response):\n"
" pass\n"
"counter = int(open('counter', 'r').read())\n"
"f = open('counter', 'w')\n"
"f.write(str(counter + 1))\n"
"f.close()\n"
"if counter >= 2:\n"
" sys.stderr.write('Something went wrong!')\n"
" exit(1)\n");
setLogLevel(-2);
pool->asyncGet(options, callback);
EVENTUALLY(5,
result = number == 1;
);
EVENTUALLY(5,
result = pool->getProcessCount() == 2;
);
EVENTUALLY(5,
result = !pool->isSpawning();
);
SHOULD_NEVER_HAPPEN(500,
result = pool->getProcessCount() > 2;
);
}
TEST_METHOD(69) {
// It removes the process from the pool if session->initiate() fails.
Options options = createOptions();
options.appRoot = "stub/wsgi";
options.appType = "wsgi";
options.startupFile = "passenger_wsgi.py";
options.spawnMethod = "direct";
pool->asyncGet(options, callback);
EVENTUALLY(5,
result = number == 1;
);
pid_t pid = currentSession->getPid();
kill(pid, SIGTERM);
// Wait until process is gone.
EVENTUALLY(5,
result = kill(pid, 0) == -1 && (errno == ESRCH || errno == EPERM || errno == ECHILD);
);
try {
currentSession->initiate();
fail("Initiate is supposed to fail");
} catch (const SystemException &e) {
ensure_equals(e.code(), ECONNREFUSED);
}
ensure_equals(pool->getProcessCount(), 0u);
}
TEST_METHOD(70) {
// When a process has become idle, and there are waiters on the pool,
// consider detaching it in order to satisfy a waiter.
Options options1 = createOptions();
Options options2 = createOptions();
options2.appRoot = "stub/wsgi";
retainSessions = true;
pool->setMax(2);
pool->asyncGet(options1, callback);
pool->asyncGet(options1, callback);
EVENTUALLY(3,
result = pool->getProcessCount() == 2;
);
pool->asyncGet(options2, callback);
ensure_equals(pool->getWaitlist.size(), 1u);
ensure_equals(number, 2);
currentSession.reset();
sessions.pop_front();
EVENTUALLY(3,
result = number == 3;
);
ensure_equals(pool->getProcessCount(), 2u);
SuperGroupPtr superGroup1 = pool->superGroups.get("stub/rack");
SuperGroupPtr superGroup2 = pool->superGroups.get("stub/rack");
ensure_equals(superGroup1->defaultGroup->enabledCount, 1);
ensure_equals(superGroup2->defaultGroup->enabledCount, 1);
}
TEST_METHOD(71) {
// A process is detached after processing maxRequests sessions.
Options options = createOptions();
options.minProcesses = 0;
options.maxRequests = 5;
pool->setMax(1);
SessionPtr session = pool->get(options, &ticket);
ensure_equals(pool->getProcessCount(), 1u);
pid_t origPid = session->getPid();
session.reset();
for (int i = 0; i < 3; i++) {
pool->get(options, &ticket).reset();
ensure_equals(pool->getProcessCount(), 1u);
ensure_equals(pool->getProcesses()[0]->pid, origPid);
}
pool->get(options, &ticket).reset();
EVENTUALLY(2,
result = pool->getProcessCount() == 0;
);
}
TEST_METHOD(72) {
// If we restart while spawning is in progress, and the restart
// finishes before the process is done spawning, then that
// process will not be attached and the original spawn loop will
// abort. A new spawn loop will start to ensure that resource
// constraints are met.
TempDirCopy dir("stub/wsgi", "tmp.wsgi");
initPoolDebugging();
Options options = createOptions();
options.appRoot = "tmp.wsgi";
options.minProcesses = 3;
// Trigger spawn loop and freeze it at the point where it's spawning
// the second process.
pool->asyncGet(options, callback);
debug->debugger->recv("Begin spawn loop iteration 1");
debug->messages->send("Proceed with spawn loop iteration 1");
debug->debugger->recv("Begin spawn loop iteration 2");
ensure_equals("(1)", pool->getProcessCount(), 1u);
// Trigger restart, wait until it's finished.
touchFile("tmp.wsgi/tmp/restart.txt", 1);
pool->asyncGet(options, callback);
debug->messages->send("Finish restarting");
debug->debugger->recv("Restarting done");
ensure_equals("(2)", pool->getProcessCount(), 0u);
// The restarter should have created a new spawn loop and
// instructed the old one to stop.
debug->debugger->recv("Begin spawn loop iteration 3");
// We let the old spawn loop continue, which should drop
// the second process and abort.
debug->messages->send("Proceed with spawn loop iteration 2");
debug->debugger->recv("Spawn loop done");
ensure_equals("(3)", pool->getProcessCount(), 0u);
// We let the new spawn loop continue.
debug->messages->send("Proceed with spawn loop iteration 3");
debug->messages->send("Proceed with spawn loop iteration 4");
debug->messages->send("Proceed with spawn loop iteration 5");
debug->debugger->recv("Spawn loop done");
ensure_equals("(4)", pool->getProcessCount(), 3u);
}
TEST_METHOD(73) {
// If a get() request comes in while the restart is in progress, then
// that get() request will be put into the get waiters list, which will
// be processed after spawning is done.
// Spawn 2 processes.
TempDirCopy dir("stub/wsgi", "tmp.wsgi");
Options options = createOptions();
options.appRoot = "tmp.wsgi";
options.minProcesses = 2;
pool->asyncGet(options, callback);
EVENTUALLY(2,
result = pool->getProcessCount() == 2;
);
// Trigger a restart. The creation of the new spawner should take a while.
spawnerConfig->spawnerCreationSleepTime = 20000;
touchFile("tmp.wsgi/tmp/restart.txt");
pool->asyncGet(options, callback);
GroupPtr group = pool->findOrCreateGroup(options);
ensure_equals(pool->getProcessCount(), 0u);
ensure_equals(group->getWaitlist.size(), 1u);
// Now that the restart is in progress, perform a get().
pool->asyncGet(options, callback);
ensure_equals(group->getWaitlist.size(), 2u);
EVENTUALLY(2,
result = number == 3;
);
ensure_equals("The restart function respects minProcesses",
pool->getProcessCount(), 2u);
}
TEST_METHOD(74) {
// If a process fails to spawn, it sends a SpawnException result to all get waiters.
TempDirCopy dir("stub/wsgi", "tmp.wsgi");
chmod("tmp.wsgi", 0777);
Options options = createOptions();
options.appRoot = "tmp.wsgi";
options.appType = "wsgi";
options.startupFile = "passenger_wsgi.py";
options.spawnMethod = "direct";
pool->setMax(1);
writeFile("tmp.wsgi/passenger_wsgi.py",
"import os, time, sys\n"
"\n"
"def file_exists(filename):\n"
" try:\n"
" os.stat(filename)\n"
" return True\n"
" except OSError:\n"
" return False\n"
"\n"
"f = open('spawned.txt', 'w')\n"
"f.write(str(os.getpid()))\n"
"f.close()\n"
"while not file_exists('continue.txt'):\n"
" time.sleep(0.05)\n"
"sys.stderr.write('Something went wrong!')\n"
"exit(1)\n");
retainSessions = true;
setLogLevel(-2);
pool->asyncGet(options, callback);
pool->asyncGet(options, callback);
pool->asyncGet(options, callback);
pool->asyncGet(options, callback);
EVENTUALLY(5,
result = fileExists("tmp.wsgi/spawned.txt");
);
usleep(20000);
writeFile("tmp.wsgi/passenger_wsgi.py", readAll("stub/wsgi/passenger_wsgi.py"));
pid_t pid = (pid_t) stringToLL(readAll("tmp.wsgi/spawned.txt"));
kill(pid, SIGTERM);
EVENTUALLY(5,
result = number == 4;
);
ensure_equals(pool->getProcessCount(), 0u);
ensure(sessions.empty());
}
TEST_METHOD(75) {
// If a process fails to spawn, the existing processes
// are kept alive.
TempDirCopy dir("stub/wsgi", "tmp.wsgi");
Options options = createOptions();
options.appRoot = "tmp.wsgi";
options.appType = "wsgi";
options.startupFile = "passenger_wsgi.py";
options.spawnMethod = "direct";
options.minProcesses = 2;
// Spawn 2 processes.
retainSessions = true;
pool->asyncGet(options, callback);
pool->asyncGet(options, callback);
EVENTUALLY(10,
result = number == 2;
);
ensure_equals(pool->getProcessCount(), 2u);
// Mess up the application and spawn a new one.
writeFile("tmp.wsgi/passenger_wsgi.py",
"import sys\n"
"sys.stderr.write('Something went wrong!')\n"
"exit(1)\n");
try {
setLogLevel(-2);
currentSession = pool->get(options, &ticket);
fail("SpawnException expected");
} catch (const SpawnException &) {
ensure_equals(pool->getProcessCount(), 2u);
}
}
TEST_METHOD(76) {
// No more than maxOutOfBandWorkInstances process will be performing
// out-of-band work at the same time.
TempDirCopy dir("stub/wsgi", "tmp.wsgi");
Options options = createOptions();
options.appRoot = "tmp.wsgi";
options.appType = "wsgi";
options.startupFile = "passenger_wsgi.py";
options.spawnMethod = "direct";
options.maxOutOfBandWorkInstances = 2;
initPoolDebugging();
debug->restarting = false;
debug->spawning = false;
debug->oobw = true;
// Spawn 3 processes and initiate 2 OOBW requests.
SessionPtr session1 = pool->get(options, &ticket);
SessionPtr session2 = pool->get(options, &ticket);
SessionPtr session3 = pool->get(options, &ticket);
session1->requestOOBW();
session1.reset();
session2->requestOOBW();
session2.reset();
// 2 OOBW requests eventually start.
debug->debugger->recv("OOBW request about to start");
debug->debugger->recv("OOBW request about to start");
// Request another OOBW, but this one is not initiated.
session3->requestOOBW();
session3.reset();
SHOULD_NEVER_HAPPEN(100,
result = debug->debugger->peek("OOBW request about to start") != NULL;
);
// Let one OOBW request finish. The third one should eventually
// start.
debug->messages->send("Proceed with OOBW request");
debug->debugger->recv("OOBW request about to start");
debug->messages->send("Proceed with OOBW request");
debug->messages->send("Proceed with OOBW request");
debug->debugger->recv("OOBW request finished");
debug->debugger->recv("OOBW request finished");
debug->debugger->recv("OOBW request finished");
}
TEST_METHOD(77) {
// If the getWaitlist already has maxRequestQueueSize items,
// then an exception is returned.
Options options = createOptions();
options.appGroupName = "test1";
options.maxRequestQueueSize = 3;
GroupPtr group = pool->findOrCreateGroup(options);
spawnerConfig->concurrency = 3;
initPoolDebugging();
pool->setMax(1);
for (int i = 0; i < 3; i++) {
pool->asyncGet(options, callback);
}
ensure_equals(number, 0);
{
LockGuard l(pool->syncher);
ensure_equals(group->getWaitlist.size(),
3u);
}
try {
pool->get(options, &ticket);
fail("Expected RequestQueueFullException");
} catch (const RequestQueueFullException &e) {
// OK
}
debug->messages->send("Proceed with spawn loop iteration 1");
debug->messages->send("Spawn loop done");
EVENTUALLY(5,
result = number == 3;
);
}
TEST_METHOD(78) {
// Test restarting while a previous restart was already being finalized.
// The previous finalization should abort.
Options options = createOptions();
initPoolDebugging();
debug->spawning = false;
pool->get(options, &ticket);
ensure_equals(pool->restartSuperGroupsByAppRoot(options.appRoot), 1u);
debug->debugger->recv("About to end restarting");
ensure_equals(pool->restartSuperGroupsByAppRoot(options.appRoot), 1u);
debug->debugger->recv("About to end restarting");
debug->messages->send("Finish restarting");
debug->messages->send("Finish restarting");
debug->debugger->recv("Restarting done");
debug->debugger->recv("Restarting aborted");
}
TEST_METHOD(79) {
// Test sticky sessions.
// Spawn 2 processes and get their sticky session IDs and PIDs.
ensureMinProcesses(2);
Options options = createOptions();
SessionPtr session1 = pool->get(options, &ticket);
SessionPtr session2 = pool->get(options, &ticket);
int id1 = session1->getStickySessionId();
int id2 = session2->getStickySessionId();
pid_t pid1 = session1->getPid();
pid_t pid2 = session2->getPid();
session1.reset();
session2.reset();
// Make two requests with id1 as sticky session ID. They should
// both go to process pid1.
options.stickySessionId = id1;
session1 = pool->get(options, &ticket);
ensure_equals("Request 1.1 goes to process 1", session1->getPid(), pid1);
// The second request should be queued, and should not finish until
// the first request is finished.
ensure_equals(number, 1);
pool->asyncGet(options, callback);
SHOULD_NEVER_HAPPEN(100,
result = number > 1;
);
session1.reset();
EVENTUALLY(1,
result = number == 2;
);
ensure_equals("Request 1.2 goes to process 1", currentSession->getPid(), pid1);
currentSession.reset();
// Make two requests with id2 as sticky session ID. They should
// both go to process pid2.
options.stickySessionId = id2;
session1 = pool->get(options, &ticket);
ensure_equals("Request 2.1 goes to process 2", session1->getPid(), pid2);
// The second request should be queued, and should not finish until
// the first request is finished.
pool->asyncGet(options, callback);
SHOULD_NEVER_HAPPEN(100,
result = number > 2;
);
session1.reset();
EVENTUALLY(1,
result = number == 3;
);
ensure_equals("Request 2.2 goes to process 2", currentSession->getPid(), pid2);
currentSession.reset();
}
// TODO: Persistent connections.
// TODO: If one closes the session before it has reached EOF, and process's maximum concurrency
// has already been reached, then the pool should ping the process so that it can detect
// when the session's connection has been released by the app.
/*********** Test previously discovered bugs ***********/
TEST_METHOD(85) {
// Test detaching, then restarting. This should not violate any invariants.
TempDirCopy dir("stub/wsgi", "tmp.wsgi");
Options options = createOptions();
options.appRoot = "tmp.wsgi";
options.appType = "wsgi";
options.startupFile = "passenger_wsgi.py";
options.spawnMethod = "direct";
SessionPtr session = pool->get(options, &ticket);
string gupid = session->getProcess()->gupid;
session.reset();
pool->detachProcess(gupid);
touchFile("tmp.wsgi/tmp/restart.txt", 1);
pool->get(options, &ticket).reset();
}
/*****************************/
}
|