Algorithm-ConsistentHash-Ketama

 view release on metacpan or  search on metacpan

t/002_consistent_hash.t  view on Meta::CPAN

    $ketama->add_bucket( "localhost:1004", 800 );
    my $keys = add_keys(200);
    my $hashed = distribute($ketama, $keys);
    check_consistency($hashed);
}

{ # 3. remove buckets
    $ketama->remove_bucket( "localhost:1000" );
    my $keys = add_keys(200);
    my $hashed = distribute($ketama, $keys);
    check_consistency($hashed);
}

}
sub distribute {
    my ($ketama, $keys) = @_;

    local $Test::Builder::Level = $Test::Builder::Level + 1;
    my %hashed;
    while ( my ($key, $expected) = each %$keys ) {
        my $got = $ketama->hash($key);
        if (ok $got, "got hashed value for $key as $got") {
            $hashed{$got}++;
        }
        # test logic for returning hash number
        my ($str, $num) = $ketama->hash_with_hashnum($key);
        is($str, $got, "->hash_with_hashnum returns same label as ->hash");
        $str = $ketama->label_from_hashnum($num);
        is($str, $got, "->label_from_hashnum returns same label as ->hash");
    }

    foreach my $key (qw( localhost:1000 localhost:1001 localhost:1002 localhost:1003 localhost:1004 )) {
        if (! defined $hashed{$key}) {
            $hashed{$key} = -1;
        }
    }

    note( explain( \%hashed ) );
    return \%hashed;
}

sub check_consistency {
    my ($hashed) = @_;

    local $Test::Builder::Level = $Test::Builder::Level + 1;
    if ($hashed->{"localhost:1000"} > 0) {
        ok( $hashed->{"localhost:1001"} > $hashed->{"localhost:1000"}, "1001 > 1000" );
    }
    ok( $hashed->{"localhost:1002"} > $hashed->{"localhost:1001"}, "1002 > 1001" );
    ok( $hashed->{"localhost:1003"} > $hashed->{"localhost:1002"}, "1003 > 1002" );
    if ($hashed->{"localhost:1004"} > 0) {
        ok( $hashed->{"localhost:1004"} > $hashed->{"localhost:1003"}, "1004 > 1003" );
    }
}

sub add_keys {
    my ($howmany) = @_;

    my %keys;
    while ($howmany-- > 0) {
        my $key = <DATA>;
        chomp $key;
        if (! $key) {
            Carp::croak("Could not read in line");
        }
        $keys{ $key } = undef;
    }
    return \%keys;
}
__DATA__
6dc1cda6ba8caea25fb32b14851fc86d
ba3272461caec8ec41556596f7df010f
13209f88100162609aeabfa4d7da85b4
13fa74d50ec3cd71d2975e30f8f85bd8
7e0c51bf844b69e2770e4d2f85f6aa73
b30fe2d51f2ce81331667b7c63523fc3
b1ae7436ecfa94a9ffbbcb9acd6cf35a
3ee5e07aa0655623ac15d94b9d629fa9
aff7ea8210d12aa9cf3a3d9c91eb3980
29b2e192fa6cc3fea4fdbf73576aa64d
ee8a3610a0f43d7f2da4d38e51dad493
02c95a61e82cf22ec3d68bf95fa2daa9
8a73b9ac46a808de13f98863f9f5c319
7f147f3a2eaaec0c7d8accc14db60c53
da5aed33f08e0488605b6438f828d33d
b8079b34bf4abc5722c6f0afe1286d2d
55316ba8b906f3342f7913bcb00b81a2
3543af97b66aced00534c1d49f142763
a83855bb9a66ec84f65b2c04122c2447
0073ee1a9b4ba9b4feed48f26b98a092
77ab95e67d16ae5b00e1d1d45d933918
f791b63c743c1277d5704a0c9a1c0586
df06e29e5862c70184e82f580eca7bc7
c4e6a34043256f7758ef87105668d68c
40e83e64838d64e38317beacb25cb055
f73d91c112483c16ac5253cd35f09291
47ad87c460f7116b154688bc1b6bd055
efe9a2a688db6e7da1acd9d08963a2a6
2629359154e0c7b1f005d375a3a461f6
ed623b5692894f792ebe235c5c653a27
7c80af1d1d42a6246b3d47f8fe36ffbc
f1c4606776dd42833a543782de625775
59fc482306f261c61f210b140175b8da
e9ca428d24620c8696bd3a6c0c5854b5
524205160e478686a83a3e93854488c0
a47f9b030da6676b6716033b70cb3b86
7fc030509391d7019f138e268c52087c
ea82dbd8ee048dea6d016ca10b3f1e94
e61d2d2fcddc7a4d217a3ce048549329
5c4bca5772d4748ff824837d7938519c
72a80a26115e283e392a60bc57e2fc34
a2b1812f16e3db35a890300fba3c057e
51df27c338c35740a02bba62d9a30447
0c1146bf126d2275318b906a73a84cb5
02be0fb20a2d9e0bbaf8171260d3c3fa
4925d79c95187e1ec4e37f446884e8dc
37688b5001728cceb9f9baa721513857
49e8d92a3f631b3739e05a075e4b5206
65f4920d7df2945e778abd65a4a2969d
4c9e8591eec75e411879b23d5c0907f2
ffca1dcd3be8ed9c4c6926a3f9a3f012
9a05fda96ca9513751642ad9b5ed47c4
4ed1d04b8469c08eb1db913c344746c3
4a43b66a824730d5304eadce434d4356
f7dd8bdcb8d790b237228eb17d8f2ca7
d2fbba1212026e8d29d96d01d7160071
e55d1d314345dfa50c623f62a0a262c5
e77a1edbe164b82d5c7ef291144b64cb
4c504f9d7f6b33fa92da6c97f6e16bf5
8e2abb9f39ac2d079295f24d4d59c5cf



( run in 0.636 second using v1.01-cache-2.11-cpan-140bd7fdf52 )