1 #include <ccan/tal/tal.h>
2 #include <ccan/tal/tal.c>
3 #include <ccan/tap/tap.h>
5 static int alloc_count, when_to_fail, err_count;
8 static void *failing_alloc(size_t len)
10 if (alloc_count++ == when_to_fail)
12 /* once we've failed once, it shouldn't ask again (steal can though). */
13 assert(stealing || alloc_count <= when_to_fail);
18 static void nofail_on_error(const char *msg)
20 diag("ERROR: %s", msg);
24 static void destroy_p(void *p)
35 tal_set_backend(failing_alloc, NULL, NULL, nofail_on_error);
37 /* Fail at each possible point in an allocation. */
38 when_to_fail = err_count = 0;
44 ok1(alloc_count >= 1);
45 ok1(when_to_fail > 1);
46 ok1(err_count == when_to_fail - 1);
49 when_to_fail = err_count = 0;
55 ok1(alloc_count >= 1);
56 ok1(when_to_fail > 1);
57 ok1(err_count == when_to_fail - 1);
59 /* Now for second child. */
60 when_to_fail = err_count = 0;
66 ok1(alloc_count >= 1);
67 ok1(when_to_fail > 1);
68 /* Note: adding a child will fall through if group alloc fails. */
69 ok1 (err_count == when_to_fail - 1 || err_count == when_to_fail);
71 /* Now while adding a destructor. */
72 when_to_fail = err_count = 0;
75 success = tal_add_destructor(p, destroy_p);
78 ok1(alloc_count >= 1);
79 ok1(when_to_fail > 1);
80 ok1(err_count == when_to_fail - 1);
82 /* Now while adding a name. */
83 when_to_fail = err_count = 0;
85 const char name[] = "some name";
87 success = tal_set_name(p, name);
90 ok1(alloc_count >= 1);
91 ok1(when_to_fail > 1);
92 ok1(err_count == when_to_fail - 1);
94 /* Now while stealing. */
96 when_to_fail = err_count = 0;
99 success = tal_steal(c2, c1) != NULL;
102 ok1(alloc_count >= 1);
103 ok1(when_to_fail > 1);
104 ok1(err_count == when_to_fail - 1);
106 /* Now stealing with more children (more coverage). */
113 /* Now steal again. */
114 when_to_fail = err_count = 0;
117 success = tal_steal(c2, c1) != NULL;
120 ok1(alloc_count >= 1);
121 ok1(when_to_fail > 1);
122 ok1(err_count == when_to_fail - 1);
125 return exit_status();