"""
This file is part of Arakoon, a distributed key-value store. Copyright
(C) 2010 Incubaid BVBA

Licensees holding a valid Incubaid license may use this file in
accordance with Incubaid's Arakoon commercial license agreement. For
more information on how to enter into this agreement, please contact
Incubaid (contact details can be found on www.arakoon.org/licensing).

Alternatively, this file may be redistributed and/or modified under
the terms of the GNU Affero General Public License version 3, as
published by the Free Software Foundation. Under this license, this
file is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.

See the GNU Affero General Public License for more details.
You should have received a copy of the
GNU Affero General Public License along with this program (file "COPYING").
If not, see <http://www.gnu.org/licenses/>.
"""

from .. import system_tests_common as Common
import nose.tools as NT
import time
import logging

@Common.with_custom_setup(Common.setup_3_nodes, Common.basic_teardown)
def test_prefered_master():
    cluster = Common.q.manage.arakoon.getCluster(Common.cluster_id)
    cluster.stop()
    pm = Common.node_names[0]
    cluster.preferredMasters([pm])
    cluster.start()
    Common.assert_running_nodes(3)
    time.sleep(4.0 * Common.lease_duration)
    logging.info("all should be well")
    client = Common.get_client()
    master = client.whoMaster()
    NT.assert_equals(pm, master)

    logging.info("master indeed is `%s`", pm)
    client.dropConnections()
    del client
    cluster.stopOne(pm)
    delay = 8.0 * Common.lease_duration

    logging.info("stopped master, waiting for things to settle (%fs)", delay)
    time.sleep(delay)
    client = Common.get_client()
    master2 = client.whoMaster()
    logging.info("master2 = %s",master2)
    NT.assert_equals(True, master2 != pm)
    logging.info("node %s took over, now restarting preferred master", master2)
    cluster.startOne(pm)
    logging.info("waiting for things to settle")
    time.sleep(4.0* Common.lease_duration)
    client = Common.get_client()
    master3 = client.whoMaster()
    logging.info("master3 = %s", master3)
    NT.assert_equals(master3,pm)
    

Add first test for 'preferred_masters'
Tests: Add first test for 'preferred_masters'

This commit adds a first test for 'preferred_masters', by copying the
existing 'system_tests_preferred' test, and changing it slightly to use
'preferredMasters' instead of 'forceMaster' with 'preferred=True'.

See: 98d74321f5d7c2f54d5faaba85f379b2a572c35c
See: d8a6072cbaf5ef66b5229035c6a60c97b3509347
See: ARAKOON-360
See: http://jira.incubaid.com/browse/ARAKOON-360
