Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change-Id: Ia2b327c1d0a7bd102eeb9333905f38539fd2c472 Reviewed-on: http://review.northscale.com:8080/158 Reviewed-by: Dustin Sallings <dustin@spy.net> Tested-by: Dustin Sallings <dustin@spy.net>
- Loading branch information
Showing
2 changed files
with
80 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
#include <cassert> | ||
|
||
#include "dispatcher.hh" | ||
#include "atomic.hh" | ||
#include "locks.hh" | ||
|
||
Dispatcher dispatcher; | ||
static Atomic<int> callbacks; | ||
|
||
class Thing; | ||
|
||
class TestCallback : public DispatcherCallback { | ||
public: | ||
TestCallback(Thing *t) : thing(t) { | ||
} | ||
|
||
bool callback(Dispatcher &d, TaskId t); | ||
|
||
private: | ||
Thing *thing; | ||
}; | ||
|
||
class Thing { | ||
public: | ||
void start(void) { | ||
LockHolder lh(m); | ||
TaskId t = dispatcher.schedule(shared_ptr<TestCallback>(new TestCallback(this))); | ||
sleep(1); // simulate an artificial delay allowing another thread in | ||
tid = t; | ||
} | ||
|
||
bool doSomething(Dispatcher &d, TaskId &t) { | ||
LockHolder lh(m); | ||
assert(t == tid); | ||
assert(&d == &dispatcher); | ||
++callbacks; | ||
return true; | ||
} | ||
private: | ||
TaskId tid; | ||
Mutex m; | ||
}; | ||
|
||
bool TestCallback::callback(Dispatcher &d, TaskId t) { | ||
return thing->doSomething(d, t); | ||
} | ||
|
||
static const char* test_get_logger_name(void) { | ||
return "dispatcher_test"; | ||
} | ||
|
||
static void test_get_logger_log(EXTENSION_LOG_LEVEL severity, | ||
const void* client_cookie, | ||
const char *fmt, ...) { | ||
(void)severity; | ||
(void)client_cookie; | ||
(void)fmt; | ||
// ignore | ||
} | ||
|
||
EXTENSION_LOGGER_DESCRIPTOR* getLogger() { | ||
static EXTENSION_LOGGER_DESCRIPTOR logger; | ||
logger.get_name = test_get_logger_name; | ||
logger.log = test_get_logger_log; | ||
return &logger; | ||
} | ||
|
||
int main(int argc, char **argv) { | ||
(void)argc; (void)argv; | ||
dispatcher.start(); | ||
Thing t; | ||
t.start(); | ||
dispatcher.stop(); | ||
assert(callbacks == 1); | ||
} |