thread_collision_warner_unittest.cc 9.46 KB
Newer Older
gejun's avatar
gejun committed
1 2 3 4
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6 7 8 9 10
#include "butil/compiler_specific.h"
#include "butil/memory/scoped_ptr.h"
#include "butil/synchronization/lock.h"
#include "butil/threading/platform_thread.h"
#include "butil/threading/simple_thread.h"
#include "butil/threading/thread_collision_warner.h"
gejun's avatar
gejun committed
11 12 13 14 15 16 17 18 19 20
#include <gtest/gtest.h>

// '' : local class member function does not have a body
MSVC_PUSH_DISABLE_WARNING(4822)


#if defined(NDEBUG)

// Would cause a memory leak otherwise.
#undef DFAKE_MUTEX
21
#define DFAKE_MUTEX(obj) scoped_ptr<butil::AsserterBase> obj
gejun's avatar
gejun committed
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38

// In Release, we expect the AsserterBase::warn() to not happen.
#define EXPECT_NDEBUG_FALSE_DEBUG_TRUE EXPECT_FALSE

#else

// In Debug, we expect the AsserterBase::warn() to happen.
#define EXPECT_NDEBUG_FALSE_DEBUG_TRUE EXPECT_TRUE

#endif


namespace {

// This is the asserter used with ThreadCollisionWarner instead of the default
// DCheckAsserter. The method fail_state is used to know if a collision took
// place.
39
class AssertReporter : public butil::AsserterBase {
gejun's avatar
gejun committed
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
 public:
  AssertReporter()
      : failed_(false) {}

  virtual void warn() OVERRIDE {
    failed_ = true;
  }

  virtual ~AssertReporter() {}

  bool fail_state() const { return failed_; }
  void reset() { failed_ = false; }

 private:
  bool failed_;
};

}  // namespace

TEST(ThreadCollisionTest, BookCriticalSection) {
  AssertReporter* local_reporter = new AssertReporter();

62
  butil::ThreadCollisionWarner warner(local_reporter);
gejun's avatar
gejun committed
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
  EXPECT_FALSE(local_reporter->fail_state());

  {  // Pin section.
    DFAKE_SCOPED_LOCK_THREAD_LOCKED(warner);
    EXPECT_FALSE(local_reporter->fail_state());
    {  // Pin section.
      DFAKE_SCOPED_LOCK_THREAD_LOCKED(warner);
      EXPECT_FALSE(local_reporter->fail_state());
    }
  }
}

TEST(ThreadCollisionTest, ScopedRecursiveBookCriticalSection) {
  AssertReporter* local_reporter = new AssertReporter();

78
  butil::ThreadCollisionWarner warner(local_reporter);
gejun's avatar
gejun committed
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
  EXPECT_FALSE(local_reporter->fail_state());

  {  // Pin section.
    DFAKE_SCOPED_RECURSIVE_LOCK(warner);
    EXPECT_FALSE(local_reporter->fail_state());
    {  // Pin section again (allowed by DFAKE_SCOPED_RECURSIVE_LOCK)
      DFAKE_SCOPED_RECURSIVE_LOCK(warner);
      EXPECT_FALSE(local_reporter->fail_state());
    }  // Unpin section.
  }  // Unpin section.

  // Check that section is not pinned
  {  // Pin section.
    DFAKE_SCOPED_LOCK(warner);
    EXPECT_FALSE(local_reporter->fail_state());
  }  // Unpin section.
}

TEST(ThreadCollisionTest, ScopedBookCriticalSection) {
  AssertReporter* local_reporter = new AssertReporter();

100
  butil::ThreadCollisionWarner warner(local_reporter);
gejun's avatar
gejun committed
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
  EXPECT_FALSE(local_reporter->fail_state());

  {  // Pin section.
    DFAKE_SCOPED_LOCK(warner);
    EXPECT_FALSE(local_reporter->fail_state());
  }  // Unpin section.

  {  // Pin section.
    DFAKE_SCOPED_LOCK(warner);
    EXPECT_FALSE(local_reporter->fail_state());
    {
      // Pin section again (not allowed by DFAKE_SCOPED_LOCK)
      DFAKE_SCOPED_LOCK(warner);
      EXPECT_NDEBUG_FALSE_DEBUG_TRUE(local_reporter->fail_state());
      // Reset the status of warner for further tests.
      local_reporter->reset();
    }  // Unpin section.
  }  // Unpin section.

  {
    // Pin section.
    DFAKE_SCOPED_LOCK(warner);
    EXPECT_FALSE(local_reporter->fail_state());
  }  // Unpin section.
}

TEST(ThreadCollisionTest, MTBookCriticalSectionTest) {
  class NonThreadSafeQueue {
   public:
130
    explicit NonThreadSafeQueue(butil::AsserterBase* asserter)
gejun's avatar
gejun committed
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
        : push_pop_(asserter) {
    }

    void push(int value) {
      DFAKE_SCOPED_LOCK_THREAD_LOCKED(push_pop_);
    }

    int pop() {
      DFAKE_SCOPED_LOCK_THREAD_LOCKED(push_pop_);
      return 0;
    }

   private:
    DFAKE_MUTEX(push_pop_);

    DISALLOW_COPY_AND_ASSIGN(NonThreadSafeQueue);
  };

149
  class QueueUser : public butil::DelegateSimpleThread::Delegate {
gejun's avatar
gejun committed
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
   public:
    explicit QueueUser(NonThreadSafeQueue& queue)
        : queue_(queue) {}

    virtual void Run() OVERRIDE {
      queue_.push(0);
      queue_.pop();
    }

   private:
    NonThreadSafeQueue& queue_;
  };

  AssertReporter* local_reporter = new AssertReporter();

  NonThreadSafeQueue queue(local_reporter);

  QueueUser queue_user_a(queue);
  QueueUser queue_user_b(queue);

170 171
  butil::DelegateSimpleThread thread_a(&queue_user_a, "queue_user_thread_a");
  butil::DelegateSimpleThread thread_b(&queue_user_b, "queue_user_thread_b");
gejun's avatar
gejun committed
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186

  thread_a.Start();
  thread_b.Start();

  thread_a.Join();
  thread_b.Join();

  EXPECT_NDEBUG_FALSE_DEBUG_TRUE(local_reporter->fail_state());
}

TEST(ThreadCollisionTest, MTScopedBookCriticalSectionTest) {
  // Queue with a 5 seconds push execution time, hopefuly the two used threads
  // in the test will enter the push at same time.
  class NonThreadSafeQueue {
   public:
187
    explicit NonThreadSafeQueue(butil::AsserterBase* asserter)
gejun's avatar
gejun committed
188 189 190 191 192
        : push_pop_(asserter) {
    }

    void push(int value) {
      DFAKE_SCOPED_LOCK(push_pop_);
193
      butil::PlatformThread::Sleep(butil::TimeDelta::FromSeconds(5));
gejun's avatar
gejun committed
194 195 196 197 198 199 200 201 202 203 204 205 206
    }

    int pop() {
      DFAKE_SCOPED_LOCK(push_pop_);
      return 0;
    }

   private:
    DFAKE_MUTEX(push_pop_);

    DISALLOW_COPY_AND_ASSIGN(NonThreadSafeQueue);
  };

207
  class QueueUser : public butil::DelegateSimpleThread::Delegate {
gejun's avatar
gejun committed
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
   public:
    explicit QueueUser(NonThreadSafeQueue& queue)
        : queue_(queue) {}

    virtual void Run() OVERRIDE {
      queue_.push(0);
      queue_.pop();
    }

   private:
    NonThreadSafeQueue& queue_;
  };

  AssertReporter* local_reporter = new AssertReporter();

  NonThreadSafeQueue queue(local_reporter);

  QueueUser queue_user_a(queue);
  QueueUser queue_user_b(queue);

228 229
  butil::DelegateSimpleThread thread_a(&queue_user_a, "queue_user_thread_a");
  butil::DelegateSimpleThread thread_b(&queue_user_b, "queue_user_thread_b");
gejun's avatar
gejun committed
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244

  thread_a.Start();
  thread_b.Start();

  thread_a.Join();
  thread_b.Join();

  EXPECT_NDEBUG_FALSE_DEBUG_TRUE(local_reporter->fail_state());
}

TEST(ThreadCollisionTest, MTSynchedScopedBookCriticalSectionTest) {
  // Queue with a 2 seconds push execution time, hopefuly the two used threads
  // in the test will enter the push at same time.
  class NonThreadSafeQueue {
   public:
245
    explicit NonThreadSafeQueue(butil::AsserterBase* asserter)
gejun's avatar
gejun committed
246 247 248 249 250
        : push_pop_(asserter) {
    }

    void push(int value) {
      DFAKE_SCOPED_LOCK(push_pop_);
251
      butil::PlatformThread::Sleep(butil::TimeDelta::FromSeconds(2));
gejun's avatar
gejun committed
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
    }

    int pop() {
      DFAKE_SCOPED_LOCK(push_pop_);
      return 0;
    }

   private:
    DFAKE_MUTEX(push_pop_);

    DISALLOW_COPY_AND_ASSIGN(NonThreadSafeQueue);
  };

  // This time the QueueUser class protects the non thread safe queue with
  // a lock.
267
  class QueueUser : public butil::DelegateSimpleThread::Delegate {
gejun's avatar
gejun committed
268
   public:
269
    QueueUser(NonThreadSafeQueue& queue, butil::Lock& lock)
gejun's avatar
gejun committed
270 271 272 273 274
        : queue_(queue),
          lock_(lock) {}

    virtual void Run() OVERRIDE {
      {
275
        butil::AutoLock auto_lock(lock_);
gejun's avatar
gejun committed
276 277 278
        queue_.push(0);
      }
      {
279
        butil::AutoLock auto_lock(lock_);
gejun's avatar
gejun committed
280 281 282 283 284
        queue_.pop();
      }
    }
   private:
    NonThreadSafeQueue& queue_;
285
    butil::Lock& lock_;
gejun's avatar
gejun committed
286 287 288 289 290 291
  };

  AssertReporter* local_reporter = new AssertReporter();

  NonThreadSafeQueue queue(local_reporter);

292
  butil::Lock lock;
gejun's avatar
gejun committed
293 294 295 296

  QueueUser queue_user_a(queue, lock);
  QueueUser queue_user_b(queue, lock);

297 298
  butil::DelegateSimpleThread thread_a(&queue_user_a, "queue_user_thread_a");
  butil::DelegateSimpleThread thread_b(&queue_user_b, "queue_user_thread_b");
gejun's avatar
gejun committed
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313

  thread_a.Start();
  thread_b.Start();

  thread_a.Join();
  thread_b.Join();

  EXPECT_FALSE(local_reporter->fail_state());
}

TEST(ThreadCollisionTest, MTSynchedScopedRecursiveBookCriticalSectionTest) {
  // Queue with a 2 seconds push execution time, hopefuly the two used threads
  // in the test will enter the push at same time.
  class NonThreadSafeQueue {
   public:
314
    explicit NonThreadSafeQueue(butil::AsserterBase* asserter)
gejun's avatar
gejun committed
315 316 317 318 319 320
        : push_pop_(asserter) {
    }

    void push(int) {
      DFAKE_SCOPED_RECURSIVE_LOCK(push_pop_);
      bar();
321
      butil::PlatformThread::Sleep(butil::TimeDelta::FromSeconds(2));
gejun's avatar
gejun committed
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
    }

    int pop() {
      DFAKE_SCOPED_RECURSIVE_LOCK(push_pop_);
      return 0;
    }

    void bar() {
      DFAKE_SCOPED_RECURSIVE_LOCK(push_pop_);
    }

   private:
    DFAKE_MUTEX(push_pop_);

    DISALLOW_COPY_AND_ASSIGN(NonThreadSafeQueue);
  };

  // This time the QueueUser class protects the non thread safe queue with
  // a lock.
341
  class QueueUser : public butil::DelegateSimpleThread::Delegate {
gejun's avatar
gejun committed
342
   public:
343
    QueueUser(NonThreadSafeQueue& queue, butil::Lock& lock)
gejun's avatar
gejun committed
344 345 346 347 348
        : queue_(queue),
          lock_(lock) {}

    virtual void Run() OVERRIDE {
      {
349
        butil::AutoLock auto_lock(lock_);
gejun's avatar
gejun committed
350 351 352
        queue_.push(0);
      }
      {
353
        butil::AutoLock auto_lock(lock_);
gejun's avatar
gejun committed
354 355 356
        queue_.bar();
      }
      {
357
        butil::AutoLock auto_lock(lock_);
gejun's avatar
gejun committed
358 359 360 361 362
        queue_.pop();
      }
    }
   private:
    NonThreadSafeQueue& queue_;
363
    butil::Lock& lock_;
gejun's avatar
gejun committed
364 365 366 367 368 369
  };

  AssertReporter* local_reporter = new AssertReporter();

  NonThreadSafeQueue queue(local_reporter);

370
  butil::Lock lock;
gejun's avatar
gejun committed
371 372 373 374

  QueueUser queue_user_a(queue, lock);
  QueueUser queue_user_b(queue, lock);

375 376
  butil::DelegateSimpleThread thread_a(&queue_user_a, "queue_user_thread_a");
  butil::DelegateSimpleThread thread_b(&queue_user_b, "queue_user_thread_b");
gejun's avatar
gejun committed
377 378 379 380 381 382 383 384 385

  thread_a.Start();
  thread_b.Start();

  thread_a.Join();
  thread_b.Join();

  EXPECT_FALSE(local_reporter->fail_state());
}