summaryrefslogtreecommitdiff
path: root/test/pthread.c
blob: f305f97d6860b9eacd16f2c2f70ffbc3e3e5af65 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
  Copyright (c) 2012 Yoran Heling

  Permission is hereby granted, free of charge, to any person obtaining
  a copy of this software and associated documentation files (the
  "Software"), to deal in the Software without restriction, including
  without limitation the rights to use, copy, modify, merge, publish,
  distribute, sublicense, and/or sell copies of the Software, and to
  permit persons to whom the Software is furnished to do so, subject to
  the following conditions:

  The above copyright notice and this permission notice shall be included
  in all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/


#include <stdio.h>
#include <string.h>
#include <pthread.h>
#include "tanja.h"
#include "tanja_pthread.h"

#define t_assert(n) printf("%sok %d - " __FILE__ ":%d: " #n "\n", (n)?"":"not ", ++test_num, __LINE__)

static int test_num = 0;
static int r_quit = 0;
static int r_alpha = 0;
static int r_alpha_sb = 0;
static int r_reply = 0;
static int reg_alpha;
static tn_session *sa, *sb;


static void recv_alpha(tn_session *s, tn_tuple *tup, tn_returnpath *p, void *d) {
	printf("# alpha %d\n", r_alpha);
	t_assert(sa == s);
	t_assert(d == (void *)1);
	t_assert(p == NULL);
	t_assert(tup && tup->n == 2);
	t_assert(tup->e[0].type == TN_VT_STR && tup->e[0].v.s != NULL && strcmp(tup->e[0].v.s, "alpha") == 0);
	t_assert(tup->e[1].type == TN_VT_INT && tup->e[1].v.i == r_alpha);
	t_assert(r_quit < 2);
	tn_tuple_unref(tup);
	if(++r_alpha >= 5) {
		tn_session_unregister(s, reg_alpha);
		// Send quit when we're done
		tn_session_send(sa, tn_tuple_new("s", strdup("quit")), NULL, NULL);
	}
}


// Note: This function may run in parallel with recv_alpha 0, so the test
// output may interleave.
static void recv_alpha_sb(tn_session *s, tn_tuple *tup, tn_returnpath *p, void *d) {
	printf("# alpha_sb\n");
	t_assert(sb == s);
	t_assert(d == (void *)2);
	t_assert(p != NULL);
	t_assert(tup && tup->n == 2);
	t_assert(tup->e[0].type == TN_VT_STR && tup->e[0].v.s != NULL && strcmp(tup->e[0].v.s, "alpha") == 0);
	t_assert(tup->e[1].type == TN_VT_INT && tup->e[1].v.i == 0);
	t_assert(r_quit == 0);
	tn_tuple_unref(tup);

	// Reply
	tn_reply(p, tn_tuple_new("i", (uint64_t)1));
	tn_reply(p, tn_tuple_new("i", (uint64_t)2));
	tn_reply_close(p);

	// Send some more alphas
	int i;
	for(i=1; i<10; i++)
		tn_session_send(sa, tn_tuple_new("si", strdup("alpha"), (uint64_t)i), NULL, NULL);

	tn_session_close(sb);
	r_alpha_sb++;
}


static void recv_quit(tn_session *s, tn_tuple *tup, tn_returnpath *p, void *d) {
	printf("# quit %d\n", r_quit);
	t_assert(sa == s);
	t_assert(p == NULL);
	t_assert(d == NULL);
	t_assert(r_alpha >= 1);
	t_assert(tup && tup->n == 1);
	t_assert(tup->e[0].type == TN_VT_STR && tup->e[0].v.s != NULL && strcmp(tup->e[0].v.s, "quit") == 0);
	tn_tuple_unref(tup);
	if(r_quit++)
		tn_session_close(s);
}


static void recv_reply(tn_session *s, tn_tuple *tup, void *d) {
	printf("# reply %d\n", r_reply);
	t_assert(sa == s);
	t_assert(d == (void *)3);
	if(++r_reply <= 2) {
		t_assert(tup && tup->n == 1);
		t_assert(tup->e[0].type == TN_VT_INT && tup->e[0].v.i == r_reply);
		tn_tuple_unref(tup);
	} else {
		t_assert(tup == NULL);
		// Send quit when we're done
		tn_session_send(sa, tn_tuple_new("s", strdup("quit")), NULL, NULL);
	}
}


int main() {
	tn_pthread_setsig();
	tn_node *n = tn_node_new();
	t_assert(n != NULL);

	// Create session a
	pthread_t th;
	sa = tn_session_pthread(n, &th);
	t_assert(sa != NULL);

	// Register "quit" and "alpha" patterns
	tn_session_register(sa, tn_tuple_new("s", strdup("quit")), 0, recv_quit, NULL);
	reg_alpha = tn_session_register(sa, tn_tuple_new("s", strdup("alpha")), 0, recv_alpha, (void *)1);

	// Create session b
	pthread_t thb;
	sb = tn_session_pthread(n, &thb);
	t_assert(sb != NULL);

	// Register "alpha" pattern (with willReply = 1)
	tn_session_register(sb, tn_tuple_new("s", strdup("alpha")), 1, recv_alpha_sb, (void *)2);

	// Send start message (which wants a reply)
	tn_session_send(sa, tn_tuple_new("si", strdup("alpha"), (uint64_t)0), recv_reply, (void *)3);

	pthread_join(th, NULL);
	pthread_join(thb, NULL);
	tn_node_unref(n);

	puts("# done");
	t_assert(r_alpha_sb == 1);
	t_assert(r_alpha == 5);
	t_assert(r_quit == 2);
	t_assert(r_reply == 3);
	printf("1..%d\n", test_num);
	return 0;
}

// vim:noet:sw=4:ts=4