#include <test_helper.h>
#include <test_spawn.h>
#include <spawn/spawn.h>
#include <time.h>

#define PROCESS_COUNT 1

void do_test_spawn(void) {
  // spawn multiple processes

  struct spawninfo *si = malloc(PROCESS_COUNT * sizeof(struct spawninfo));
  domainid_t *pid = malloc(PROCESS_COUNT * sizeof(domainid_t));
  for(uint i = 0; i < PROCESS_COUNT; ++i) {
    debug_printf("TEST_SPAWN: spawn %u\n", i);
    CHECK_ERR(spawn_load_by_name("hello", &(si[i]), &(pid[i])));
  }

  // wait for 10 seconds but handle events in the mean time
  // struct waitset *default_ws = get_default_waitset();
  // struct timespec tt;
  // clock_gettime(CLOCK_REALTIME, &tt);
  // long start = time_to_ms(tt);
  // do {
  //   if(err_is_ok(check_for_event(default_ws))){
  //     CHECK_ERR(event_dispatch(default_ws));
  //   }
  //   clock_gettime(CLOCK_REALTIME, &tt);
  // } while(time_to_ms(tt) - start < 10000);

  // for(uint i = 0; i < PROCESS_COUNT; ++i) {
  //   debug_printf("TEST_SPAWN: Killing %u\n", i);
  //   CHECK_ERR(invoke_dispatcher_stop(si[i].dispatcher));
  // }
}
