END_TEST START_TEST(test_full_queue) { for (int i = 0; i < ALARM_QUEUE_SIZE; i++) { fail_unless(add_alarm(alarmqueue, "add", "Test alarm message."), "Failed to add alarm."); } fail_if(add_alarm(alarmqueue, "fail", "Test failing alarm."), "Should have failed to add another alarm."); }
END_TEST START_TEST(test_giant_alarm) { char *msg="This is a really large message that exceeds the 256 or " "so bytes allocated for messages to see what happens when a " "message is too big. Hopefully it's fine. " "It turns out that 256 bytes or whatever the current value is ends " "up being quite a bit of bytes. I suppose that makes sense if " "I do a bit of math, but I'm too lazy to do anything other than assert."; fail_unless(strlen(msg) > ALARM_MSG_MAXLEN, "Message string was too short to blow up."); fail_unless(add_alarm(alarmqueue, "bigass", msg), "Failed to alarm."); alarm_t in_alarm = get_alarm(alarmqueue); fail_unless(in_alarm.open, "Didn't receive a large alarm."); fail_unless(strlen(in_alarm.msg) == ALARM_MSG_MAXLEN, "Alarm message is too long."); fail_unless(strcmp("bigass", in_alarm.name) == 0, "Alarm name didn't match."); fail_unless(strncmp(msg, in_alarm.msg, ALARM_MSG_MAXLEN) == 0, "Alarm message didn't match."); }
ssize_t tcp_send(struct tcp_sock *m, const void *data, int size) { ssize_t ret = 0; switch(m->state) { case TCP_CLIENT_DISCONNECTED: /* We rate-limit the amount of connect() calls. */ if (alarm_pending(&tcp_connect_alarm)) { ret = -1; break; } add_alarm(&tcp_connect_alarm, TCP_CONNECT_TIMEOUT, 0); ret = connect(m->fd, (struct sockaddr *)&m->addr, m->sockaddr_len); if (ret == -1) { if (errno == EINPROGRESS || errno == EALREADY) { /* connection in progress or already trying. */ m->state = TCP_CLIENT_DISCONNECTED; } else if (errno == ECONNREFUSED) { /* connection refused. */ m->state = TCP_CLIENT_DISCONNECTED; m->stats.error++; } else { /* unexpected error, give up. */ m->state = TCP_CLIENT_DISCONNECTED; m->stats.error++; } break; } else { /* we got connected :) */ m->state = TCP_CLIENT_CONNECTED; } case TCP_CLIENT_CONNECTED: ret = sendto(m->fd, data, size, 0, (struct sockaddr *) &m->addr, m->sockaddr_len); if (ret == -1) { if (errno == EPIPE || errno == ECONNRESET) { close(m->fd); tcp_client_init(m, m->conf); m->state = TCP_CLIENT_DISCONNECTED; m->stats.error++; } else { m->stats.error++; return -1; } } } if (ret >= 0) { m->stats.bytes += ret; m->stats.messages++; } return ret; }
int main(int argc, char *argv[]) { QALARM *q; QTHREAD *qt; int i, *j; q = qalarm(); add_alarm(q, 15, alarmed, NULL, QALARM_RECUR); add_alarm(q, 22, force_terminate, q, QALARM_DEFAULT); new_sleep(30); /* Yeah, I'm lazy */ if (argv[1]) terminate_alarms(q); else wait_alarms(q); delete_qalarm(q); return 0; }
static enum conflate_mgmt_cb_result process_alarm_create(void *opaque, conflate_handle_t *handle, const char *cmd, bool direct, kvpair_t *form, conflate_form_result *r) { if(add_alarm(handle->alarms, "test", "This is a test alarm!")) { fprintf(stderr, "Created alarm!\n"); } else { fprintf(stderr, "Error queueing an alarm.\n"); } return RV_OK; }
END_TEST START_TEST(test_giant_name) { const char *name = "this name should exceed the max length"; fail_unless(strlen(name) > ALARM_NAME_MAXLEN, "Name wasn't too big enough."); fail_unless(add_alarm(alarmqueue, name, "some message"), "Failed to alarm."); alarm_t in_alarm = get_alarm(alarmqueue); fail_unless(in_alarm.open, "Didn't receive an alarm."); fail_unless(strlen(in_alarm.name) == ALARM_NAME_MAXLEN, "Alarm name is too long."); fail_unless(strncmp(name, in_alarm.name, ALARM_NAME_MAXLEN) == 0, "Alarm name didn't match."); fail_unless(strcmp("some message", in_alarm.msg) == 0, "Alarm message didn't match."); }
static void *inner_alarm_handler(void *ctx) { QTHREAD *qt = (QTHREAD *)ctx; time_t starttime; time_t newtime; struct timeval t; QUEUE_ITEM *item; starttime = time(NULL); newtime = qt->timeout; while (time(NULL) - starttime < newtime) { t.tv_sec = newtime < POLLSEC ? newtime : POLLSEC; t.tv_usec = 0; select(0, NULL, NULL, NULL, &t); newtime = qt->timeout - (time(NULL) - starttime); if (!Queue_Empty(qt->queue)) { item = Get_Queue_Item(qt->queue); if (!strcmp(item->action, "Terminate")) { Free_Queue_Item(item); return NULL; } Free_Queue_Item(item); } } qt->cb(qt->data); if (qt->flags & QALARM_RECUR) add_alarm(qt->parent, qt->timeout, qt->cb, qt->data, qt->flags); Add_Queue_Item(qt->parent->queue, "Destroy", &(qt->tid), sizeof(pthread_t)); return NULL; }
void set_alarm() { uint i = get_time(); add_alarm(&timer, i + (total_time * UNIT)); }