Project

General

Profile

0001-test-librados-fork-test.patch

simple librados fork unit test - David Disseldorp, 05/13/2020 07:18 PM

Download (2.93 KB)

View differences:

src/test/librados/misc.cc
348 348
    threads[i].join();
349 349
  ASSERT_EQ(setrlimit(RLIMIT_NOFILE, &rold), 0);
350 350
}
351

  
352
static void forker_child_fail(int status, const char *msg)
353
{
354
	std::cout << msg << " failed: " << status << std::endl;
355
	exit(status);
356
}
357

  
358
static pid_t forker_start_proc(rados_t cluster_parent)
359
{
360
	int ret;
361
	rados_t cluster_child;
362

  
363
	pid_t pid = fork();
364
	if (pid != 0) {
365
		/* parent */
366
		return pid;
367
	}
368
	/* child - pass errors up to parent */
369
	//rados_shutdown(cluster_parent);
370

  
371
	ret = rados_create(&cluster_child, NULL);
372
	if (ret < 0) {
373
		forker_child_fail(ret, "create");
374
	}
375
	ret = rados_conf_read_file(cluster_child, NULL);
376
	if (ret < 0) {
377
		forker_child_fail(ret, "conf file read");
378
	}
379
	ret = rados_conf_parse_env(cluster_child, NULL);
380
	if (ret < 0) {
381
		forker_child_fail(ret, "conf parse");
382
	}
383
	ret = rados_connect(cluster_child);
384
	if (ret < 0) {
385
		forker_child_fail(ret, "connect");
386
	}
387
	rados_shutdown(cluster_child);
388

  
389
	exit(0);
390
}
391

  
392
static void forker_sigchld_handle(int sig)
393
{
394
    std::cout << "got signal " << sig << std::endl;
395
}
396

  
397
// Test that forked children can perform I/O
398
#define FORKER_NUM_FORKS 1
399
TEST_F(LibRadosMisc, Forker)
400
{
401
  rados_t cluster_parent;
402
  struct proc_state {
403
    pid_t pid;
404
  } procs[FORKER_NUM_FORKS];
405
  struct sigaction act;
406
  struct sigaction oldact;
407

  
408
  memset(&act, 0, sizeof(act));
409
  act.sa_handler = forker_sigchld_handle;
410
  sigemptyset(&act.sa_mask);
411
  sigaddset(&act.sa_mask, SIGCHLD);
412
  ASSERT_EQ(sigaction(SIGCHLD, &act, &oldact), 0);
413

  
414
  // connect to cluster before forking child proc
415
  ASSERT_EQ(0, rados_create(&cluster_parent, NULL));
416
  ASSERT_EQ(0, rados_conf_read_file(cluster_parent, NULL));
417
  ASSERT_EQ(0, rados_conf_parse_env(cluster_parent, NULL));
418
  ASSERT_EQ(0, rados_connect(cluster_parent));
419

  
420
  for (int i = 0; i < FORKER_NUM_FORKS; ++i) {
421
    procs[i].pid = forker_start_proc(cluster_parent);
422
  }
423

  
424
  for (int i = 0; i < FORKER_NUM_FORKS; ++i) {
425
    int status = -1;
426
    ASSERT_EQ(waitpid(procs[i].pid, &status, 0), procs[i].pid);
427
    ASSERT_EQ(status, 0);
428
  }
429

  
430
  rados_shutdown(cluster_parent);
431

  
432
  // restore previous signal context
433
  ASSERT_EQ(sigaction(SIGCHLD, &oldact, NULL), 0);
434
}
435
#undef FORKER_NUM_FORKS
351
-