bthread_butex_unittest.cpp 11.7 KB
Newer Older
gejun's avatar
gejun committed
1
// Copyright (c) 2014 Baidu, Inc.
gejun's avatar
gejun committed
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
// Author: Ge,Jun (gejun@baidu.com)
// Date: Sun Jul 13 15:04:18 CST 2014

#include <gtest/gtest.h>
#include "base/atomicops.h"
#include "base/time.h"
#include "base/macros.h"
#include "base/logging.h"
#include "bthread/butex.h"
#include "bthread/task_control.h"
#include "bthread/task_group.h"
#include "bthread/bthread.h"
#include "bthread/unstable.h"

namespace bthread {
extern base::atomic<TaskControl*> g_task_control;
inline TaskControl* get_task_control() {
    return g_task_control.load(base::memory_order_consume);
}
} // namespace bthread

namespace {
TEST(ButexTest, wait_on_already_timedout_butex) {
    uint32_t* butex = bthread::butex_create_checked<uint32_t>();
    ASSERT_TRUE(butex);
    timespec now;
    ASSERT_EQ(0, clock_gettime(CLOCK_REALTIME, &now));
    *butex = 1;
    ASSERT_EQ(-1, bthread::butex_wait(butex, 1, &now));
    ASSERT_EQ(ETIMEDOUT, errno);
}

void* sleeper(void* arg) {
    bthread_usleep((uint64_t)arg);
    return NULL;
}

void* joiner(void* arg) {
    const long t1 = base::gettimeofday_us();
    for (bthread_t* th = (bthread_t*)arg; *th; ++th) {
        if (0 != bthread_join(*th, NULL)) {
            LOG(FATAL) << "fail to join thread_" << th - (bthread_t*)arg;
        }
        long elp = base::gettimeofday_us() - t1;
        EXPECT_LE(labs(elp - (th - (bthread_t*)arg + 1) * 100000L), 5000L)
            << "timeout when joining thread_" << th - (bthread_t*)arg;
        LOG(INFO) << "Joined thread " << *th << " at " << elp << "us ["
                  << bthread_self() << "]";
    }
    for (bthread_t* th = (bthread_t*)arg; *th; ++th) {
        EXPECT_EQ(0, bthread_join(*th, NULL));
    }
    return NULL;
}

struct A {
    uint64_t a;
    char dummy[0];
};

struct B {
    uint64_t a;
};


TEST(ButexTest, with_or_without_array_zero) {
    ASSERT_EQ(sizeof(B), sizeof(A));
}


TEST(ButexTest, join) {
    const size_t N = 6;
    const size_t M = 6;
    bthread_t th[N+1];
    bthread_t jth[M];
    pthread_t pth[M];
    for (size_t i = 0; i < N; ++i) {
        bthread_attr_t attr = (i == 0 ? BTHREAD_ATTR_PTHREAD : BTHREAD_ATTR_NORMAL);
        ASSERT_EQ(0, bthread_start_urgent(
                      &th[i], &attr, sleeper,
                      (void*)(100000L/*100ms*/ * (i + 1))));
    }
    th[N] = 0;  // joiner will join tids in `th' until seeing 0.
    for (size_t i = 0; i < M; ++i) {
        ASSERT_EQ(0, bthread_start_urgent(&jth[i], NULL, joiner, th));
    }
    for (size_t i = 0; i < M; ++i) {
        ASSERT_EQ(0, pthread_create(&pth[i], NULL, joiner, th));
    }
    
    for (size_t i = 0; i < M; ++i) {
        ASSERT_EQ(0, bthread_join(jth[i], NULL))
            << "i=" << i << " error=" << berror();
    }
    for (size_t i = 0; i < M; ++i) {
        ASSERT_EQ(0, pthread_join(pth[i], NULL));
    }
}


struct WaiterArg {
    int expected_result;
    int expected_value;
    base::atomic<int> *butex;
    const timespec *ptimeout;
};

void* waiter(void* arg) {
    WaiterArg * wa = (WaiterArg*)arg;
    const long t1 = base::gettimeofday_us();
    const int rc = bthread::butex_wait(
        wa->butex, wa->expected_value, wa->ptimeout);
    const long t2 = base::gettimeofday_us();
    if (rc == 0) {
        EXPECT_EQ(wa->expected_result, 0) << bthread_self();
    } else {
        EXPECT_EQ(wa->expected_result, errno) << bthread_self();
    }
    LOG(INFO) << "after wait, time=" << (t2-t1) << "us";
    return NULL;
}

TEST(ButexTest, sanity) {
    const size_t N = 5;
    WaiterArg args[N * 4];
    pthread_t t1, t2;
    base::atomic<int>* b1 =
        bthread::butex_create_checked<base::atomic<int> >();
    ASSERT_TRUE(b1);
    bthread::butex_destroy(b1);
    
    b1 = bthread::butex_create_checked<base::atomic<int> >();
    *b1 = 1;
    ASSERT_EQ(0, bthread::butex_wake(b1));

    WaiterArg *unmatched_arg = new WaiterArg;
    unmatched_arg->expected_value = *b1 + 1;
    unmatched_arg->expected_result = EWOULDBLOCK;
    unmatched_arg->butex = b1;
    unmatched_arg->ptimeout = NULL;
    pthread_create(&t2, NULL, waiter, unmatched_arg);
    bthread_t th;
    ASSERT_EQ(0, bthread_start_urgent(&th, NULL, waiter, unmatched_arg));

    const timespec abstime = base::seconds_from_now(1);
    for (size_t i = 0; i < 4*N; ++i) {
        args[i].expected_value = *b1;
        args[i].butex = b1;
        if ((i % 2) == 0) {
            args[i].expected_result = 0;
            args[i].ptimeout = NULL;
        } else {
            args[i].expected_result = ETIMEDOUT;
            args[i].ptimeout = &abstime;
        }
        if (i < 2*N) { 
            pthread_create(&t1, NULL, waiter, &args[i]);
        } else {
            ASSERT_EQ(0, bthread_start_urgent(&th, NULL, waiter, &args[i]));
        }
    }
    
    sleep(2);
    for (size_t i = 0; i < 2*N; ++i) {
        ASSERT_EQ(1, bthread::butex_wake(b1));
    }
    ASSERT_EQ(0, bthread::butex_wake(b1));
    sleep(1);
    bthread::butex_destroy(b1);
}


struct ButexWaitArg {
    int* butex;
    int expected_val;
    long wait_msec;
    int error_code;
};

void* wait_butex(void* void_arg) {
    ButexWaitArg* arg = static_cast<ButexWaitArg*>(void_arg);
    const timespec ts = base::milliseconds_from_now(arg->wait_msec);
    int rc = bthread::butex_wait(arg->butex, arg->expected_val, &ts);
    int saved_errno = errno;
    if (arg->error_code) {
        EXPECT_EQ(-1, rc);
        EXPECT_EQ(arg->error_code, saved_errno);
    } else {
        EXPECT_EQ(0, rc);
    }
    return NULL;
}

TEST(ButexTest, wait_without_stop) {
    int* butex = bthread::butex_create_checked<int>();
    *butex = 7;
    base::Timer tm;
    const long WAIT_MSEC = 500;
    for (int i = 0; i < 2; ++i) {
        const bthread_attr_t attr =
            (i == 0 ? BTHREAD_ATTR_PTHREAD : BTHREAD_ATTR_NORMAL);
        ButexWaitArg arg = { butex, *butex, WAIT_MSEC, ETIMEDOUT };
        bthread_t th;
        
        tm.start();
        ASSERT_EQ(0, bthread_start_urgent(&th, &attr, wait_butex, &arg));
        ASSERT_EQ(0, bthread_join(th, NULL));
        tm.stop();
        
        ASSERT_LT(abs(tm.m_elapsed() - WAIT_MSEC), 20);
    }
    bthread::butex_destroy(butex);
}

TEST(ButexTest, stop_after_running) {
    int* butex = bthread::butex_create_checked<int>();
    *butex = 7;
    base::Timer tm;
    const long WAIT_MSEC = 500;
    const long SLEEP_MSEC = 10;
    for (int i = 0; i < 2; ++i) {
        const bthread_attr_t attr =
            (i == 0 ? BTHREAD_ATTR_PTHREAD : BTHREAD_ATTR_NORMAL);
        bthread_t th;
        ButexWaitArg arg = { butex, *butex, WAIT_MSEC, ESTOP };

        tm.start();
        ASSERT_EQ(0, bthread_start_urgent(&th, &attr, wait_butex, &arg));
        ASSERT_EQ(0, bthread_usleep(SLEEP_MSEC * 1000L));
        ASSERT_EQ(0, bthread_stop(th));
        ASSERT_EQ(0, bthread_join(th, NULL));
        tm.stop();

        ASSERT_LT(abs(tm.m_elapsed() - SLEEP_MSEC), 10);
        // ASSERT_TRUE(bthread::get_task_control()->
        //             timer_thread()._idset.empty());
        ASSERT_EQ(EINVAL, bthread_stop(th));
    }    
    bthread::butex_destroy(butex);
}

TEST(ButexTest, stop_before_running) {
    int* butex = bthread::butex_create_checked<int>();
    *butex = 7;
    base::Timer tm;
    const long WAIT_MSEC = 500;

    for (int i = 0; i < 2; ++i) {
        const bthread_attr_t attr =
            (i == 0 ? BTHREAD_ATTR_PTHREAD : BTHREAD_ATTR_NORMAL) | BTHREAD_NOSIGNAL;
        bthread_t th;
        ButexWaitArg arg = { butex, *butex, WAIT_MSEC, ESTOP };
        
        tm.start();
        ASSERT_EQ(0, bthread_start_background(&th, &attr, wait_butex, &arg));
        ASSERT_EQ(0, bthread_stop(th));
        bthread_flush();
        ASSERT_EQ(0, bthread_join(th, NULL));
        tm.stop();
        
        ASSERT_LT(tm.m_elapsed(), 5);
        // ASSERT_TRUE(bthread::get_task_control()->
        //             timer_thread()._idset.empty());
        ASSERT_EQ(EINVAL, bthread_stop(th));
    }
    bthread::butex_destroy(butex);
}

void* join_the_waiter(void* arg) {
    EXPECT_EQ(ESTOP, bthread_join((bthread_t)arg, NULL));
    return NULL;
}

TEST(ButexTest, join_cant_be_wakeup) {
    const long WAIT_MSEC = 100;
    int* butex = bthread::butex_create_checked<int>();
    *butex = 7;
    base::Timer tm;
    ButexWaitArg arg = { butex, *butex, 1000, ESTOP };

    for (int i = 0; i < 2; ++i) {
        const bthread_attr_t attr =
            (i == 0 ? BTHREAD_ATTR_PTHREAD : BTHREAD_ATTR_NORMAL);
        tm.start();
        bthread_t th, th2;
        ASSERT_EQ(0, bthread_start_urgent(&th, NULL, wait_butex, &arg));
        ASSERT_EQ(0, bthread_start_urgent(&th2, &attr, join_the_waiter, (void*)th));
        ASSERT_EQ(0, bthread_stop(th2));
        ASSERT_EQ(0, bthread_usleep(WAIT_MSEC / 2 * 1000L));
        ASSERT_TRUE(bthread::TaskGroup::exists(th));
        ASSERT_TRUE(bthread::TaskGroup::exists(th2));
        ASSERT_EQ(0, bthread_usleep(WAIT_MSEC / 2 * 1000L));
        ASSERT_EQ(0, bthread_stop(th));
        ASSERT_EQ(0, bthread_join(th2, NULL));
        ASSERT_EQ(0, bthread_join(th, NULL));
        tm.stop();
        ASSERT_LT(tm.m_elapsed(), WAIT_MSEC + 10);
        ASSERT_EQ(EINVAL, bthread_stop(th));
        ASSERT_EQ(EINVAL, bthread_stop(th2));
    }
    bthread::butex_destroy(butex);
}

TEST(ButexTest, stop_after_slept) {
    base::Timer tm;
    const long SLEEP_MSEC = 100;
    const long WAIT_MSEC = 10;
    
    for (int i = 0; i < 2; ++i) {
        const bthread_attr_t attr =
            (i == 0 ? BTHREAD_ATTR_PTHREAD : BTHREAD_ATTR_NORMAL);
        tm.start();
        bthread_t th;
        ASSERT_EQ(0, bthread_start_urgent(
                      &th, &attr, sleeper, (void*)(SLEEP_MSEC*1000L)));
        ASSERT_EQ(0, bthread_usleep(WAIT_MSEC * 1000L));
        ASSERT_EQ(0, bthread_stop(th));
        ASSERT_EQ(0, bthread_join(th, NULL));
        tm.stop();
        if (attr.stack_type == BTHREAD_STACKTYPE_PTHREAD) {
            ASSERT_LT(abs(tm.m_elapsed() - SLEEP_MSEC), 15);
        } else {
            ASSERT_LT(abs(tm.m_elapsed() - WAIT_MSEC), 15);
        }
        // ASSERT_TRUE(bthread::get_task_control()->
        //             timer_thread()._idset.empty());
        ASSERT_EQ(EINVAL, bthread_stop(th));
    }
}

TEST(ButexTest, stop_just_when_sleeping) {
    base::Timer tm;
    const long SLEEP_MSEC = 100;
    
    for (int i = 0; i < 2; ++i) {
        const bthread_attr_t attr =
            (i == 0 ? BTHREAD_ATTR_PTHREAD : BTHREAD_ATTR_NORMAL);
        tm.start();
        bthread_t th;
        ASSERT_EQ(0, bthread_start_urgent(
                      &th, &attr, sleeper, (void*)(SLEEP_MSEC*1000L)));
        ASSERT_EQ(0, bthread_stop(th));
        ASSERT_EQ(0, bthread_join(th, NULL));
        tm.stop();
        if (attr.stack_type == BTHREAD_STACKTYPE_PTHREAD) {
            ASSERT_LT(abs(tm.m_elapsed() - SLEEP_MSEC), 15);
        } else {
            ASSERT_LT(tm.m_elapsed(), 15);
        }
        // ASSERT_TRUE(bthread::get_task_control()->
        //             timer_thread()._idset.empty());
        ASSERT_EQ(EINVAL, bthread_stop(th));
    }
}

TEST(ButexTest, stop_before_sleeping) {
    base::Timer tm;
    const long SLEEP_MSEC = 100;

    for (int i = 0; i < 2; ++i) {
        bthread_t th;
        const bthread_attr_t attr =
            (i == 0 ? BTHREAD_ATTR_PTHREAD : BTHREAD_ATTR_NORMAL) | BTHREAD_NOSIGNAL;
        
        tm.start();
        ASSERT_EQ(0, bthread_start_background(&th, &attr, sleeper,
                                              (void*)(SLEEP_MSEC*1000L)));
        ASSERT_EQ(0, bthread_stop(th));
        bthread_flush();
        ASSERT_EQ(0, bthread_join(th, NULL));
        tm.stop();

        if (attr.stack_type == BTHREAD_STACKTYPE_PTHREAD) {
            ASSERT_LT(abs(tm.m_elapsed() - SLEEP_MSEC), 10);
        } else {
            ASSERT_LT(tm.m_elapsed(), 10);
        }
        // ASSERT_TRUE(bthread::get_task_control()->
        //             timer_thread()._idset.empty());
        ASSERT_EQ(EINVAL, bthread_stop(th));
    }
}
} // namespace