Add a connect callback.

This commit is contained in:
Ian Gulliver
2015-06-18 04:27:17 +00:00
parent c72f2ff30e
commit d0d0ed1e21
3 changed files with 62 additions and 1 deletions

View File

@@ -16,9 +16,18 @@ typedef struct {
const json_t *last_message;
bool logout_fired;
bool connect_fired;
} test_state;
void on_connect(void *passthrough) {
test_state *state = passthrough;
assert(!pthread_mutex_lock(&state->lock));
state->connect_fired = true;
assert(!pthread_cond_signal(&state->cond));
assert(!pthread_mutex_unlock(&state->lock));
}
void on_logout(void *passthrough) {
test_state *state = passthrough;
assert(!pthread_mutex_lock(&state->lock));
@@ -57,6 +66,16 @@ void wait_for_logout(test_state *state) {
assert(!pthread_mutex_unlock(&state->lock));
}
void wait_for_connect(test_state *state) {
assert(!pthread_mutex_lock(&state->lock));
while (!state->connect_fired) {
assert(!pthread_cond_wait(&state->cond, &state->lock));
}
state->connect_fired = false;
assert(!pthread_mutex_unlock(&state->lock));
}
test_state *create_test_state() {
test_state *ret = malloc(sizeof(test_state));
assert(ret);
@@ -65,6 +84,7 @@ test_state *create_test_state() {
assert(!pthread_cond_init(&ret->cond, NULL));
ret->last_message = NULL;
ret->logout_fired = false;
ret->connect_fired = false;
return ret;
}
@@ -79,6 +99,7 @@ cosmo *create_client(test_state *state) {
cosmo_uuid(client_id);
cosmo_callbacks callbacks = {
.connect = on_connect,
.logout = on_logout,
.message = on_message,
};
@@ -135,6 +156,13 @@ bool test_message_round_trip(test_state *state) {
return true;
}
bool test_connect_fires(test_state *state) {
cosmo *client = create_client(state);
wait_for_connect(state);
cosmo_shutdown(client);
return true;
}
bool test_logout_fires(test_state *state) {
cosmo *client = create_client(state);
wait_for_logout(state);
@@ -171,8 +199,9 @@ bool test_resubscribe(test_state *state) {
int main(int argc, char *argv[]) {
RUN_TEST(test_create_destroy);
RUN_TEST(test_message_round_trip);
RUN_TEST(test_connect_fires);
RUN_TEST(test_logout_fires);
RUN_TEST(test_message_round_trip);
RUN_TEST(test_resubscribe);
return 0;