summaryrefslogtreecommitdiff
path: root/test.pl
blob: b0f6272852d2080ecc004e3c606ded1a329e36b8 (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
#!/usr/bin/perl

use strict;
use warnings;
use Test::More;
use AnyEvent;
use AnyEvent::Handle;
use Socket;

my $DEBUG = 0;
if($DEBUG) {
	no warnings 'once';
	require Data::Dumper;
	$Data::Dumper::Indent = 0;
	$Data::Dumper::Terse = 1;
}

use_ok 'Tanja';


# Simple matching tests.
# (Doesn't test a whole lot, since the semantics aren't final yet anyway).
ok  Tanja::match([], []);
ok  Tanja::match([], [1, 2, 3]);
ok  Tanja::match([], [undef]);
ok !Tanja::match([undef], []);
ok  Tanja::match([undef], [1]);
ok  Tanja::match([1], [1]);
ok  Tanja::match([1], [1, "b"]);
ok  Tanja::match([1], [undef, 3]);
ok !Tanja::match([2], [1]);


# Simple single-session test
{
	my $node = Tanja::Node->new;
	isa_ok $node, 'Tanja::Node';
	my $ses = $node->session;
	isa_ok $ses, 'Tanja::Session';
	my $done = AnyEvent->condvar;
	my $n = 0;
	my $n2 = 0;
	$ses->reg([], sub {
		my($t, $r) = @_;
		isa_ok $r, 'Tanja::ReturnPath';
		ok $r->null;
		is_deeply $t, [$n];
		ok $n <= 5;
		if(++$n == 5) {
			$ses->close;
			$done->send;
		}
	});
	$ses->reg_once([undef], sub {
		my($t, $r) = @_;
		isa_ok $r, 'Tanja::ReturnPath';
		ok $r->null;
		is_deeply $t, [0];
		$n2++;
	});
	$ses->send([$_]) for (0..10);
	is $n, 0; # Make sure that ->send() doesn't run the callbacks. The event system should.
	is $n2, 0;
	$done->recv;
	is $n, 5;
	is $n2, 1;
}


# Simple double-session test with return-path
sub t_double {
	my($sa, $sb, $link) = @_;
	my $a = $sa->session;
	my $b = $sb->session;
	my $done = AnyEvent->condvar;
	my $msgn = 0;
	$a->reg(["msg"], sub {
		my($t, $r) = @_;
		ok !$msgn++;
		isa_ok $r, 'Tanja::ReturnPath';
		ok !$r->null;
		$r->reply(['b', 9]);
		is_deeply $t, ["msg", 'a'];
		$a->send(["b"]);
	});
	my $bn = 0;
	$b->reg(["b"], sub {
		my($t, $r) = @_;
		ok !$bn++;
		isa_ok $r, 'Tanja::ReturnPath';
		ok $r->null;
		is_deeply $t, ["b"];
		$done->send;
	});
	$link && $link->();
	my $n = 0;
	$b->send(["msg", 'a'], sub {
		!$n++ ? is_deeply $_[0], ['b', 9] : ok !@_;
	});
	$done->recv;
	is $n, 2;
}

{	# same node
	my $s = Tanja::Node->new;
	note 'Same Node';
	t_double($s, $s);
}

{	# different nodes, linked. (With various combinations of the 'sync' flag)
	for my $f (0..3) {
		note "Linked nodes, $f";
		my $sa = Tanja::Node->new;
		my $sb = Tanja::Node->new;
		t_double($sa, $sb, sub {
			socketpair my $socka, my $sockb, AF_UNIX, SOCK_STREAM, PF_UNSPEC;
			my $done = AnyEvent->condvar;
			$done->begin;
			$done->begin;
			$sa->link(
				handle => AnyEvent::Handle->new(fh => $socka),
				sync => $f&1,
				on_ready => sub { $done->end },
				$DEBUG ? (on_write => sub { note 'A: ',Data::Dumper::Dumper(\@_) }) : (),
			);
			$sb->link(
				handle => AnyEvent::Handle->new(fh => $sockb),
				sync => $f&2,
				on_ready => sub { $done->end },
				$DEBUG ? (on_write => sub { note 'B: ',Data::Dumper::Dumper(\@_) }) : (),
			);
			$done->recv;
		});
	}
}



done_testing();

# vim:noet:sw=4:ts=4