示例#1
0
CTEST2(tube, utilities)
{
    tube *t;
    spud_tube_id *id;
    int local_data = 1337;
    int *out;
    char buf[24];
    char buf2[24];

    ASSERT_TRUE(tube_manager_running(data->mgr));
    ASSERT_EQUAL(tube_manager_size(data->mgr), 0);

    ASSERT_TRUE( tube_create(&t, &data->err));
    tube_set_data(t, &local_data);
    out = tube_get_data(t);
    ASSERT_EQUAL(*out, 1337);
    ASSERT_EQUAL(tube_get_state(t), TS_UNKNOWN);

    tube_get_id(t, &id);
    spud_id_to_string(buf, sizeof(buf), id);

    ASSERT_TRUE( tube_id_to_string(t, buf2, sizeof(buf2)) == buf2);
    ASSERT_STR(buf, buf2);

    ASSERT_TRUE(tube_manager_stop(data->mgr, &data->err));
    ASSERT_FALSE(tube_manager_running(data->mgr));
}
示例#2
0
static int
markov()
{
  struct timespec timer;
  struct timespec remaining;
  tube*           t;
  ls_err          err;
  int             i;
  int*            iptr;

  timer.tv_sec = 0;

  while ( tube_manager_running(mgr) )
  {
    timer.tv_nsec = gauss(50000000, 10000000);
    nanosleep(&timer, &remaining);
    i = random() % NUM_TUBES;
    t = tubes[i];
    if (!t)
    {
      if ( !tube_create(&t, &err) )
      {
        LS_LOG_ERR(err, "tube_create");
        return 1;
      }
      tubes[i] = t;
      iptr     = malloc( sizeof(*iptr) );
      *iptr    = i;
      tube_set_data(t, iptr);
    }
    switch ( tube_get_state(t) )
    {
    case TS_START:
      ls_log(LS_LOG_ERROR, "invalid tube state");
      return 1;
    case TS_UNKNOWN:
      if ( !tube_manager_open_tube(mgr,
                                   (const struct sockaddr*)&remoteAddr,
                                   &t,
                                   &err) )
      {
        LS_LOG_ERR(err, "tube_manager_open_tube");
        return 1;
      }
      break;
    case TS_OPENING:
      /* keep waiting by the mailbox, Charlie Brown */
      break;
    case TS_RUNNING:
      /* .1% chance of close */
      if ( (random() % 10000) < 10 )
      {
        if ( !tube_close(t, &err) )
        {
          LS_LOG_ERR(err, "tube_close");
          return 1;
        }
      }
      else
      {
        /* TODO: put something intersting in the buffer */
        if ( !tube_data(t, data, random() % sizeof(data), &err) )
        {
          LS_LOG_ERR(err, "tube_data");
          return 1;
        }
      }
    case TS_RESUMING:
      /* can't get here yet */
      break;
    }
  }
  return 0;
}