Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 966a5473ad
Fetching contributors…

Cannot retrieve contributors at this time

file 248 lines (203 sloc) 7.51 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
/**
* Copyright (C) 2012 10gen Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include <boost/thread/thread.hpp>
#include <stdexcept>
#include <string>
#include <vector>

#include "mongo/util/fail_point.h"
#include "mongo/util/time_support.h"
#include "mongo/unittest/unittest.h"

using mongo::FailPoint;

namespace mongo_test {
    TEST(FailPoint, InitialState) {
        FailPoint failPoint;
        ASSERT_FALSE(failPoint.shouldFail());
        ASSERT(failPoint.getData().isEmpty());
        ASSERT_FALSE(failPoint.shouldFail());
    }

    TEST(FailPoint, AlwaysOn) {
        FailPoint failPoint;
        failPoint.setMode(FailPoint::alwaysOn);
        ASSERT(failPoint.shouldFail());
        ASSERT(failPoint.getData().isEmpty());

        for (size_t x = 0; x < 50; x++) {
            ASSERT(failPoint.shouldFail());
        }
    }

    TEST(FailPoint, NTimes) {
        FailPoint failPoint;
        failPoint.setMode(FailPoint::nTimes, 4);
        ASSERT(failPoint.shouldFail());
        ASSERT(failPoint.shouldFail());
        ASSERT(failPoint.shouldFail());
        ASSERT(failPoint.shouldFail());

        for (size_t x = 0; x < 50; x++) {
            ASSERT_FALSE(failPoint.shouldFail());
        }
    }

    TEST(FailPoint, BlockOff) {
        FailPoint failPoint;
        bool called = false;

        MONGO_FAIL_POINT_BLOCK(failPoint) {
            called = true;
        }

        ASSERT_FALSE(called);
    }

    TEST(FailPoint, BlockAlwaysOn) {
        FailPoint failPoint;
        failPoint.setMode(FailPoint::alwaysOn);
        bool called = false;

        MONGO_FAIL_POINT_BLOCK(failPoint) {
            called = true;
        }

        ASSERT(called);
    }

    TEST(FailPoint, BlockNTimes) {
        FailPoint failPoint;
        failPoint.setMode(FailPoint::nTimes, 1);
        size_t counter = 0;

        for (size_t x = 0; x < 10; x++) {
            MONGO_FAIL_POINT_BLOCK(failPoint) {
                counter++;
            }
        }

        ASSERT_EQUALS(1U, counter);
    }

    TEST(FailPoint, BlockWithException) {
        FailPoint failPoint;
        failPoint.setMode(FailPoint::alwaysOn);
        bool threw = false;

        try {
            MONGO_FAIL_POINT_BLOCK(failPoint) {
                throw std::logic_error("BlockWithException threw");
            }
        }
        catch (const std::logic_error &) {
            threw = true;
        }

        ASSERT(threw);
        // This will get into an infinite loop if reference counter was not
        // properly decremented
        failPoint.setMode(FailPoint::off);
    }

    TEST(FailPoint, SetGetParam) {
        FailPoint failPoint;
        failPoint.setMode(FailPoint::alwaysOn, 0, BSON("x" << 20));

        MONGO_FAIL_POINT_BLOCK(failPoint) {
            ASSERT_EQUALS(20, failPoint.getData()["x"].numberInt());
        }
    }

    TEST(FailPoint, SetInvalidMode) {
        FailPoint failPoint;

        ASSERT_THROWS(failPoint.setMode(static_cast<FailPoint::Mode>(9999)),
                      mongo::UserException);
        ASSERT_FALSE(failPoint.shouldFail());

        ASSERT_THROWS(failPoint.setMode(static_cast<FailPoint::Mode>(-1)),
                      mongo::UserException);
        ASSERT_FALSE(failPoint.shouldFail());
    }

    class FailPointStress: public mongo::unittest::Test {
    public:
        FailPointStress(): _tasks(NULL) {
        }

        void setUp() {
            _fp.setMode(FailPoint::alwaysOn, 0, BSON("a" << 44));
        }

        void tearDown() {
            // Note: This can loop indefinitely if reference counter was off
            _fp.setMode(FailPoint::off, 0, BSON("a" << 66));
        }

        void startTest() {
            verify(_tasks == NULL);

            _tasks = new boost::thread_group();
            _tasks->add_thread(new boost::thread(blockTask, &_fp));
            _tasks->add_thread(new boost::thread(blockWithExceptionTask, &_fp));
            _tasks->add_thread(new boost::thread(simpleTask, &_fp));
            _tasks->add_thread(new boost::thread(flipTask, &_fp));
        }

        void stopTest() {
            _tasks->interrupt_all();
            _tasks->join_all();
            delete _tasks;
            _tasks = NULL;
        }

    private:
        static void blockTask(FailPoint* failPoint) {
            while (true) {
                MONGO_FAIL_POINT_BLOCK((*failPoint)) {
                    const mongo::BSONObj& data = failPoint->getData();

                    // Expanded ASSERT_EQUALS since the error is not being
                    // printed out properly
                    if (data["a"].numberInt() != 44) {
                        mongo::error() << "blockTask thread detected anomaly"
                                << " - data: " << data << std::endl;
                        ASSERT(false);
                    }
                }

                boost::this_thread::interruption_point();
            }
        }

        static void blockWithExceptionTask(FailPoint* failPoint) {
            while (true) {
                try {
                    MONGO_FAIL_POINT_BLOCK((*failPoint)) {
                        const mongo::BSONObj& data = failPoint->getData();

                        if (data["a"].numberInt() != 44) {
                            mongo::error() << "blockWithExceptionTask thread detected anomaly"
                                    << " - data: " << data << std::endl;
                            ASSERT(false);
                        }

                        throw std::logic_error("blockWithExceptionTask threw");
                    }
                }
                catch (const std::logic_error&) {
                }

                boost::this_thread::interruption_point();
            }
        }

        static void simpleTask(FailPoint* failPoint) {
            while (true) {
                if (MONGO_FAIL_POINT((*failPoint))) {
                    const mongo::BSONObj& data = failPoint->getData();

                    if (data["a"].numberInt() != 44) {
                        mongo::error() << "simpleTask thread detected anomaly - "
                                << " data: " << data << std::endl;
                        ASSERT(false);
                    }
                }

                boost::this_thread::interruption_point();
            }
        }

        static void flipTask(FailPoint* failPoint) {
            while (true) {
                if(failPoint->shouldFail()) {
                    failPoint->setMode(FailPoint::off, 0);
                }
                else {
                    failPoint->setMode(FailPoint::alwaysOn, 0, BSON("a" << 44));
                }

                boost::this_thread::interruption_point();
            }
        }

        FailPoint _fp;
        boost::thread_group* _tasks;
    };

    TEST_F(FailPointStress, Basic) {
        startTest();
        mongo::sleepsecs(120);
        stopTest();
    }
}
Something went wrong with that request. Please try again.