add changelog for 1.13
[beanstalkd.git] / testjobs.c
blobb30573211ca604f505bbf6b57e9af09701446b40
1 #include "ct/ct.h"
2 #include "dat.h"
3 #include <stdint.h>
4 #include <stdlib.h>
5 #include <stdio.h>
6 #include <string.h>
7 #include <sys/time.h>
9 static Tube *default_tube;
11 void
12 cttest_job_creation()
14 Job *j;
16 TUBE_ASSIGN(default_tube, make_tube("default"));
17 j = make_job(1, 0, 1, 0, default_tube);
18 assertf(j->r.pri == 1, "priority should match");
21 void
22 cttest_job_cmp_pris()
24 Job *a, *b;
26 TUBE_ASSIGN(default_tube, make_tube("default"));
27 a = make_job(1, 0, 1, 0, default_tube);
28 b = make_job(1 << 27, 0, 1, 0, default_tube);
30 assertf(job_pri_less(a, b), "should be less");
33 void
34 cttest_job_cmp_ids()
36 Job *a, *b;
38 TUBE_ASSIGN(default_tube, make_tube("default"));
39 a = make_job(1, 0, 1, 0, default_tube);
40 b = make_job(1, 0, 1, 0, default_tube);
42 b->r.id <<= 49;
43 assertf(job_pri_less(a, b), "should be less");
46 void
47 cttest_job_large_pris()
49 Job *a, *b;
51 TUBE_ASSIGN(default_tube, make_tube("default"));
52 a = make_job(1, 0, 1, 0, default_tube);
53 b = make_job(-5, 0, 1, 0, default_tube);
55 assertf(job_pri_less(a, b), "should be less");
57 a = make_job(-5, 0, 1, 0, default_tube);
58 b = make_job(1, 0, 1, 0, default_tube);
60 assertf(!job_pri_less(a, b), "should not be less");
63 void
64 cttest_job_hash_free()
66 Job *j;
67 uint64 jid = 83;
69 TUBE_ASSIGN(default_tube, make_tube("default"));
70 j = make_job_with_id(0, 0, 1, 0, default_tube, jid);
71 job_free(j);
73 assertf(!job_find(jid), "job should be missing");
76 void
77 cttest_job_hash_free_next()
79 Job *a, *b;
80 uint64 aid = 97, bid = 12386;
82 TUBE_ASSIGN(default_tube, make_tube("default"));
83 b = make_job_with_id(0, 0, 1, 0, default_tube, bid);
84 a = make_job_with_id(0, 0, 1, 0, default_tube, aid);
86 assertf(a->ht_next == b, "b should be chained to a");
88 job_free(b);
90 assertf(a->ht_next == NULL, "job should be missing");
93 void
94 cttest_job_all_jobs_used()
96 Job *j, *x;
98 TUBE_ASSIGN(default_tube, make_tube("default"));
99 j = make_job(0, 0, 1, 0, default_tube);
100 assertf(get_all_jobs_used() == 1, "should match");
102 x = allocate_job(10);
103 assertf(get_all_jobs_used() == 1, "should match");
105 job_free(x);
106 assertf(get_all_jobs_used() == 1, "should match");
108 job_free(j);
109 assertf(get_all_jobs_used() == 0, "should match");
112 void
113 cttest_job_100_000_jobs()
115 int i;
117 TUBE_ASSIGN(default_tube, make_tube("default"));
118 for (i = 0; i < 100000; i++) {
119 make_job(0, 0, 1, 0, default_tube);
121 assertf(get_all_jobs_used() == 100000, "should match");
123 for (i = 1; i <= 100000; i++) {
124 job_free(job_find(i));
126 fprintf(stderr, "get_all_jobs_used() => %zu\n", get_all_jobs_used());
127 assertf(get_all_jobs_used() == 0, "should match");
130 void
131 ctbench_job_make(int n)
133 int i;
134 Job **j = calloc(n, sizeof *j);
135 TUBE_ASSIGN(default_tube, make_tube("default"));
137 ctresettimer();
138 for (i = 0; i < n; i++) {
139 j[i] = make_job(0, 0, 1, 0, default_tube);
141 ctstoptimer();
143 for (i = 0; i < n; i++) {
144 job_free(j[i]);
146 free(j);
149 void
150 ctbench_job_free(int n)
152 int i;
153 Job **j = calloc(n, sizeof *j);
154 TUBE_ASSIGN(default_tube, make_tube("default"));
155 for (i = 0; i < n; i++) {
156 j[i] = make_job(0, 0, 1, 0, default_tube);
159 ctresettimer();
160 for (i = 0; i < n; i++) {
161 job_free(j[i]);
163 ctstoptimer();
165 free(j);