/* * splinter -- swarm (local fleet) tests. * * Two instances in one directory, in one process: the first binds the socket * and becomes master, the second connects as a slave. What is asserted is the * wire cycle -- welcome, id, routed text, busy report -- not the UI. */ #include #include #include #include #include #include "swarm.h" static int failures; #define CHECK(cond) do { \ if (!(cond)) { \ fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #cond); \ failures++; \ } \ } while (0) static void nap_ms(long ms) { struct timespec ts; ts.tv_sec = ms / 1000; ts.tv_nsec = (ms % 1000) * 1000000L; nanosleep(&ts, NULL); } int main(void) { struct swarm master, slave; char dir[] = "/tmp/splinter-swarm-XXXXXX"; char buf[512], list[512], path[600]; int i, got = 0; CHECK(mkdtemp(dir) != NULL); CHECK(swarm_init(&master, dir) == 0); CHECK(master.role == SWARM_MASTER); CHECK(master.id == 0); CHECK(swarm_init(&slave, dir) == 0); CHECK(slave.role == SWARM_SLAVE); /* the master has to accept before the welcome reaches the slave */ for (i = 0; i < 50 && slave.id < 0; i++) { swarm_poll(&master); swarm_poll(&slave); if (slave.id < 0) nap_ms(10); } CHECK(slave.id == 1); /* a routed line arrives in the slave's inbox and says it came from afar */ CHECK(swarm_route(&master, -1, "hallo slave") == 1); for (i = 0; i < 50 && !got; i++) { swarm_poll(&slave); got = swarm_take_inbox(&slave, buf, sizeof(buf)); if (!got) nap_ms(10); } CHECK(got == 1); CHECK(strcmp(buf, "hallo slave") == 0); CHECK(swarm_inbox_remote(&slave) == 1); CHECK(swarm_inbox_remote(&slave) == 0); /* cleared after reading */ /* quotes, backslashes and newlines survive the wire unescaped */ got = 0; CHECK(swarm_route(&master, 1, "say \"hi\" \\ now\nline 2") == 1); for (i = 0; i < 50 && !got; i++) { swarm_poll(&slave); got = swarm_take_inbox(&slave, buf, sizeof(buf)); if (!got) nap_ms(10); } CHECK(got == 1); CHECK(strcmp(buf, "say \"hi\" \\ now\nline 2") == 0); (void)swarm_inbox_remote(&slave); /* the master can route to itself without a socket round trip */ CHECK(swarm_route(&master, 0, "an mich") == 0); CHECK(swarm_take_inbox(&master, buf, sizeof(buf)) == 1); CHECK(strcmp(buf, "an mich") == 0); CHECK(swarm_inbox_remote(&master) == 1); /* a busy report is visible in the master's list */ swarm_report_busy(&slave, 7); for (i = 0; i < 50; i++) { swarm_poll(&master); swarm_list(&master, list, sizeof(list)); if (strstr(list, "busy=7") != NULL) break; nap_ms(10); } CHECK(strstr(list, "busy=7") != NULL); CHECK(strstr(list, "slave pid=") != NULL); swarm_status(&master, buf, sizeof(buf)); CHECK(strstr(buf, "master 0") != NULL); CHECK(strstr(buf, "1 slaves") != NULL); swarm_status(&slave, buf, sizeof(buf)); CHECK(strstr(buf, "slave 1") != NULL); /* * The file lives while a member does, and not a second longer. Who bound * it is an implementation detail: when the master goes, a member that is * still there takes the fleet over and the path stays. */ snprintf(path, sizeof(path), "%s/%s", dir, SWARM_PATH); CHECK(access(path, F_OK) == 0); /* close the slave first: it is not the one that would keep the fleet */ swarm_close(&slave); CHECK(access(path, F_OK) == 0); /* the master is still there */ swarm_close(&master); if (access(path, F_OK) == 0) { fprintf(stderr, "FAIL %s:%d: socket file survived the last member\n", __FILE__, __LINE__); failures++; } CHECK(access(path, F_OK) != 0); /* * Two members, the master leaves first: the fleet stays alive under the * remaining one and the file with it. The old close took the file along * and left the survivor on a path nobody answered on. */ { struct swarm m2, s2; CHECK(swarm_init(&m2, dir) == 0); CHECK(m2.role == SWARM_MASTER); CHECK(swarm_init(&s2, dir) == 0); CHECK(s2.role == SWARM_SLAVE); for (i = 0; i < 50 && s2.id < 0; i++) { swarm_poll(&m2); swarm_poll(&s2); if (s2.id < 0) nap_ms(10); } CHECK(s2.id == 1); swarm_close(&m2); /* the master goes first */ CHECK(access(path, F_OK) == 0); /* a member is still running */ /* the survivor notices and takes the fleet over */ for (i = 0; i < 50 && s2.role != SWARM_MASTER; i++) { swarm_poll(&s2); if (s2.role != SWARM_MASTER) nap_ms(10); } CHECK(s2.role == SWARM_MASTER); CHECK(access(path, F_OK) == 0); /* and it is the last one, so its close removes the file */ swarm_close(&s2); CHECK(access(path, F_OK) != 0); } /* * A hard-killed master (EOF on the connection, no "bye") leaves a file * with no listener behind. A member that reads the EOF has to clean that * up and keep the fleet -- this is the "[swarm locked]" case. */ { struct swarm m3, s3; CHECK(swarm_init(&m3, dir) == 0); CHECK(swarm_init(&s3, dir) == 0); for (i = 0; i < 50 && s3.id < 0; i++) { swarm_poll(&m3); swarm_poll(&s3); if (s3.id < 0) nap_ms(10); } CHECK(s3.id == 1); /* the master dies the way kill -9 does: no bye, file left over */ if (m3.listen_fd >= 0) { close(m3.listen_fd); m3.listen_fd = -1; } for (i = 0; i < m3.npeers; i++) if (m3.peers[i].fd >= 0) { close(m3.peers[i].fd); m3.peers[i].fd = -1; } m3.role = SWARM_NONE; CHECK(access(path, F_OK) == 0); /* the leftover file */ for (i = 0; i < 50 && s3.role != SWARM_MASTER; i++) { swarm_poll(&s3); if (s3.role != SWARM_MASTER) nap_ms(10); } CHECK(s3.role == SWARM_MASTER); swarm_close(&s3); CHECK(access(path, F_OK) != 0); } /* * The last-resort signal cleanup must not take the fleet away from a * member that is still running. kill -TERM on the master runs * swarm_emergency_unlink(), which has no access to the peer table and * counts live peers in a sig_atomic_t instead: with a peer there, the * file stays and the survivor takes over. */ { struct swarm m4, s4; CHECK(swarm_init(&m4, dir) == 0); CHECK(swarm_init(&s4, dir) == 0); for (i = 0; i < 50 && s4.id < 0; i++) { swarm_poll(&m4); swarm_poll(&s4); if (s4.id < 0) nap_ms(10); } CHECK(s4.id == 1); /* what the SIGTERM handler would do to the master */ swarm_emergency_unlink(); if (access(path, F_OK) != 0) { fprintf(stderr, "FAIL %s:%d: signal cleanup took the file from a live member\n", __FILE__, __LINE__); failures++; } /* and then the process is gone: no bye, no close, just EOF */ if (m4.listen_fd >= 0) { close(m4.listen_fd); m4.listen_fd = -1; } for (i = 0; i < m4.npeers; i++) if (m4.peers[i].fd >= 0) { close(m4.peers[i].fd); m4.peers[i].fd = -1; } m4.role = SWARM_NONE; /* the survivor has to take over the fleet that was left to it */ for (i = 0; i < 50 && s4.role != SWARM_MASTER; i++) { swarm_poll(&s4); if (s4.role != SWARM_MASTER) nap_ms(10); } CHECK(s4.role == SWARM_MASTER); swarm_close(&s4); CHECK(access(path, F_OK) != 0); } /* a directory with no fleet is standalone, not an error */ { struct swarm solo; CHECK(swarm_init(&solo, dir) == 0); CHECK(solo.role == SWARM_MASTER); CHECK(access(path, F_OK) == 0); swarm_close(&solo); CHECK(access(path, F_OK) != 0); } /* * A file that is not ours is not deleted. Another instance may have * bound the path after us; its file must survive our close. */ { struct swarm first; FILE *foreign; CHECK(swarm_init(&first, dir) == 0); CHECK(first.role == SWARM_MASTER); /* replace the file behind our back, as a new master would */ CHECK(unlink(path) == 0); foreign = fopen(path, "w"); CHECK(foreign != NULL); if (foreign != NULL) fclose(foreign); swarm_close(&first); CHECK(access(path, F_OK) == 0); /* not ours: left alone */ (void)unlink(path); } snprintf(path, sizeof(path), "rm -rf '%s'", dir); (void)system(path); if (failures == 0) printf("test_swarm: ok\n"); return failures == 0 ? 0 : 1; }