From 194b78050e16431aa6b756b06476f458de540acc Mon Sep 17 00:00:00 2001 From: antirez Date: Mon, 19 May 2014 15:26:17 +0200 Subject: [PATCH] Cluster test: better failure detection test and framework improvements. --- tests/cluster/cluster.tcl | 33 ++++++++++++++++++++++++++++++ tests/cluster/tests/01-faildet.tcl | 19 +++++++++-------- 2 files changed, 43 insertions(+), 9 deletions(-) diff --git a/tests/cluster/cluster.tcl b/tests/cluster/cluster.tcl index a7d71ab4..46b4a630 100644 --- a/tests/cluster/cluster.tcl +++ b/tests/cluster/cluster.tcl @@ -73,3 +73,36 @@ proc assert_cluster_state {state} { } } } + +# Search the first node starting from ID $first that is not +# already configured as a slave. +proc cluster_find_available_slave {first} { + foreach_redis_id id { + if {$id < $first} continue + if {[instance_is_killed redis $id]} continue + set me [get_myself $id] + if {[dict get $me slaveof] eq {-}} {return $id} + } + fail "No available slaves" +} + +# Add 'slaves' slaves to a cluster composed of 'masters' masters. +# It assumes that masters are allocated sequentially from instance ID 0 +# to N-1. +proc cluster_allocate_slaves {masters slaves} { + for {set j 0} {$j < $slaves} {incr j} { + set master_id [expr {$j % $masters}] + set slave_id [cluster_find_available_slave $masters] + set master_myself [get_myself $master_id] + R $slave_id cluster replicate [dict get $master_myself id] + } +} + +# Create a cluster composed of the specified number of masters and slaves. +proc create_cluster {masters slaves} { + cluster_allocate_slots $masters + if {$slaves} { + cluster_allocate_slaves $masters $slaves + } + assert_cluster_state ok +} diff --git a/tests/cluster/tests/01-faildet.tcl b/tests/cluster/tests/01-faildet.tcl index 883548ca..a31f7eb6 100644 --- a/tests/cluster/tests/01-faildet.tcl +++ b/tests/cluster/tests/01-faildet.tcl @@ -2,16 +2,8 @@ source "../tests/includes/init-tests.tcl" -proc create_cluster {masters slaves} { - cluster_allocate_slots $masters - if {$slaves} { - cluster_allocate_slaves $masters $slaves - } - assert_cluster_state ok -} - test "Create a 5 nodes cluster" { - create_cluster 5 0 + create_cluster 5 5 } test "Killing one master node" { @@ -29,3 +21,12 @@ test "Restarting master node" { test "Cluster should be up again" { assert_cluster_state ok } + +test "Killing two slave nodes" { + kill_instance redis 5 + kill_instance redis 6 +} + +test "Cluster should be still up" { + assert_cluster_state ok +}