synchronous_event_unittest.cpp 1.34 KB
Newer Older
gejun's avatar
gejun committed
1
// Copyright (c) 2014 Baidu, Inc.
gejun's avatar
gejun committed
2 3 4 5 6
//
// Author: Ge,Jun (gejun@baidu.com)
// Date: Sat Aug 30 17:13:19 CST 2014

#include <gtest/gtest.h>
7
#include "butil/synchronous_event.h"
gejun's avatar
gejun committed
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

namespace {
class SynchronousEventTest : public ::testing::Test{
protected:
    SynchronousEventTest(){
    };
    virtual ~SynchronousEventTest(){};
    virtual void SetUp() {
        srand(time(0));
    };
    virtual void TearDown() {
    };
};

struct Foo {};

24
typedef butil::SynchronousEvent<int, int*> FooEvent;
gejun's avatar
gejun committed
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

FooEvent foo_event;
std::vector<std::pair<int, int> > result;

class FooObserver : public FooEvent::Observer {
public:
    FooObserver() : another_ob(NULL) {}
    
    void on_event(int x, int* p) {
        ++*p;
        result.push_back(std::make_pair(x, *p));
        if (another_ob) {
            foo_event.subscribe(another_ob);
        }
    }
    FooObserver* another_ob;
};


TEST_F(SynchronousEventTest, sanity) {
    const size_t N = 10;
    FooObserver foo_observer;
    FooObserver foo_observer2;
    foo_observer.another_ob = &foo_observer2;
    foo_event.subscribe(&foo_observer);
    int v = 0;
    result.clear();
    for (size_t i = 0; i < N; ++i) {
        foo_event.notify(i, &v);
    }
    ASSERT_EQ(2*N, result.size());
    for (size_t i = 0; i < 2*N; ++i) {
        ASSERT_EQ((int)i/2, result[i].first);
        ASSERT_EQ((int)i+1, result[i].second);
    }
}

}