1#!/usr/bin/env perl
2
3use common::sense 2;
4use Test::NoWarnings;
5use Test::More tests => 35+1;
6use lib::abs "../lib";
7use AnyEvent::Memcached::Hash;
8use AnyEvent::Memcached::Hash::WithNext;
9use AnyEvent::Memcached::Buckets;
10
11my $bucks = AnyEvent::Memcached::Buckets->new( servers => [
12	"node-x",
13	"node-y",
14	"node-z",
15	"socket",
16	[ "node-z", 3 ]
17]);
18
19my $hasher = AnyEvent::Memcached::Hash::WithNext->new(
20	buckets => $bucks,
21);
22
23# Basic tests
24is_deeply $hasher->hashes('a'), { 'node-z' => ['a'], 'socket' => ['a'] }, 'hashes a';
25is_deeply $hasher->hashes('b'), { 'node-z' => ['b'], 'socket' => ['b'] }, 'hashes b';
26is_deeply $hasher->hashes('c'), { 'node-z' => ['c'], 'socket' => ['c'] }, 'hashes c';
27is_deeply $hasher->hashes('d'), { 'node-z' => ['d'], 'socket' => ['d'] }, 'hashes d';
28is_deeply $hasher->hashes('e'), { 'node-z' => ['e'], 'socket' => ['e'] }, 'hashes e';
29is_deeply $hasher->hashes('f'), { 'node-z' => ['f'], 'socket' => ['f'] }, 'hashes f';
30is_deeply $hasher->hashes('g'), { 'node-z' => ['g'], 'socket' => ['g'] }, 'hashes g';
31is_deeply $hasher->hashes('h'), { 'node-x' => ['h'], 'node-y' => ['h'] }, 'hashes h';
32is_deeply $hasher->hashes('i'), { 'node-z' => ['i'], 'socket' => ['i'] }, 'hashes i';
33is_deeply $hasher->hashes('j'), { 'node-x' => ['j'], 'node-y' => ['j'] }, 'hashes j';
34is_deeply $hasher->hashes('k'), { 'node-z' => ['k'], 'socket' => ['k'] }, 'hashes k';
35is_deeply $hasher->hashes('l'), { 'node-x' => ['l'], 'socket' => ['l'] }, 'hashes l';
36is_deeply $hasher->hashes('m'), { 'node-z' => ['m'], 'socket' => ['m'] }, 'hashes m';
37is_deeply $hasher->hashes('n'), { 'node-z' => ['n'], 'socket' => ['n'] }, 'hashes n';
38is_deeply $hasher->hashes('o'), { 'node-z' => ['o'], 'socket' => ['o'] }, 'hashes o';
39is_deeply $hasher->hashes('p'), { 'node-z' => ['p'], 'node-y' => ['p'] }, 'hashes p';
40is_deeply $hasher->hashes('q'), { 'node-z' => ['q'], 'socket' => ['q'] }, 'hashes q';
41is_deeply $hasher->hashes('r'), { 'node-x' => ['r'], 'node-y' => ['r'] }, 'hashes r';
42is_deeply $hasher->hashes('s'), { 'node-x' => ['s'], 'socket' => ['s'] }, 'hashes s';
43is_deeply $hasher->hashes('t'), { 'node-x' => ['t'], 'node-y' => ['t'] }, 'hashes t';
44is_deeply $hasher->hashes('u'), { 'node-z' => ['u'], 'socket' => ['u'] }, 'hashes u';
45is_deeply $hasher->hashes('v'), { 'node-x' => ['v'], 'socket' => ['v'] }, 'hashes v';
46is_deeply $hasher->hashes('w'), { 'node-z' => ['w'], 'node-y' => ['w'] }, 'hashes w';
47is_deeply $hasher->hashes('x'), { 'node-z' => ['x'], 'socket' => ['x'] }, 'hashes x';
48is_deeply $hasher->hashes('y'), { 'node-z' => ['y'], 'socket' => ['y'] }, 'hashes y';
49is_deeply $hasher->hashes('z'), { 'node-x' => ['z'], 'node-y' => ['z'] }, 'hashes z';
50
51# Test many keys
52is_deeply $hasher->hashes([qw(h p q v)]), {
53	'node-x' => ['h','v'],
54	'node-y' => ['h','p'],
55	'node-z' => ['p','q'],
56	'socket' => ['q','v'],
57}, 'hashes [h p q v]';
58
59# Test complex keys with predefined hash value
60is_deeply $hasher->hashes([[0 => 'a0']]), { 'node-x' => ['a0'], 'node-y' => ['a0'] }, 'hashes [[0,a0]]';
61is_deeply $hasher->hashes([[1 => 'a1']]), { 'node-y' => ['a1'], 'node-z' => ['a1'] }, 'hashes [[1,a1]]';
62is_deeply $hasher->hashes([[2 => 'a2']]), { 'node-z' => ['a2'], 'socket' => ['a2'] }, 'hashes [[2,a2]]';
63is_deeply $hasher->hashes([[3 => 'a3']]), { 'socket' => ['a3'], 'node-x' => ['a3'] }, 'hashes [[3,a3]]';
64is_deeply $hasher->hashes([[4 => 'a4']]), { 'node-z' => ['a4'], 'socket' => ['a4'] }, 'hashes [[4,a4]]';
65is_deeply $hasher->hashes([[5 => 'a5']]), { 'node-z' => ['a5'], 'socket' => ['a5'] }, 'hashes [[5,a5]]';
66is_deeply $hasher->hashes([[6 => 'a6']]), { 'node-z' => ['a6'], 'socket' => ['a6'] }, 'hashes [[6,a6]]';
67
68# Test many complex keys
69is_deeply
70	$hasher->hashes([
71		[ 0 => 'a' ], [ 1 => 'b' ], [ 2 => 'c' ], [ 3 => 'd' ]
72	]), {
73		'node-x' => ['a','d'],
74		'node-y' => ['a','b'],
75		'node-z' => ['b','c'],
76		'socket' => ['c','d'],
77	},
78	'hashes [[1],[2],[3],[4]]'
79;
80