<?xml version="1.0" encoding="utf-8"?>
<test>
<name>RT on *nix: watchdog and replaying binlog</name>

<requires>
<non-windows/>
<watchdog/>
</requires>

<config>
searchd
{
<searchd_settings/>
	workers = threads
<dynamic>
<variant>
	binlog_path =
</variant>
<variant>
	binlog_path = data
</variant>
</dynamic>
}

index crc
{
	type	= rt
	path	= data/crc
	
	rt_field = content
	rt_attr_uint = idd
	
	dict = crc
}

index kw
{
	type	= rt
	path	= data/kw
	
	rt_field = content
	rt_attr_uint = idd
	
	dict = keywords
	min_prefix_len = 1
}

source src_test
{
	type			= mysql
	<sql_settings/>
	sql_query		= select 1, 11 as idd, 'dummy'
	sql_attr_uint	= idd
}

index test
{
	source			= src_test
	path			= <data_path/>/test
    docinfo			= extern
	dict = crc
}

</config>

<custom_test><![CDATA[

$results = array();

$make_sock = create_function ( '&$results', '

	global $sd_address, $sd_sphinxql_port, $error;

	$sockStr = "$sd_address:$sd_sphinxql_port";
	if ($sd_address == "localhost")
		$sockStr = "127.0.0.1:$sd_sphinxql_port";
	$sock = @mysql_connect ( $sockStr,"","", true );
	
	if ( $sock === false )
	{
		$results[] = "error: can not connect to searchd: " . @mysql_errno ( $sock ) . " : " . @mysql_error ( $sock );
	}
	return $sock;
');

$query = create_function ( '&$results, $q, $sock', '
	
	$res = @mysql_query ( $q, $sock );
	if ($res===true)
	{
		$results[] = "query: " . $q . "; total_affected:" . mysql_affected_rows($sock);
	}
	else if ($res===false)
	{
		$results[] = "query: " . $q . "; error:" . @mysql_errno ( $sock ) . " : " . @mysql_error ( $sock );
		return;
	}
	else
	{
		$results[] = "query: " . $q . "; total_rows:" . @mysql_num_rows($res);
		while ($row = @mysql_fetch_array($res, MYSQL_ASSOC))
		{
			$foo = array();
			foreach ($row as $key => $value)
			{
				$results[] = $key . " => " . $value;
			}
		}	
	}
');

$sock = $make_sock( $results );
if ( $sock === false )
{
	return;
}

// regression
// binlog crashes on replayng commit with delete only statement
$query ( $results, "DELETE FROM kw WHERE id=1000", $sock);

$query ( $results, "INSERT INTO crc VALUES (1, 'test work', 11)", $sock);
$query ( $results, "INSERT INTO kw VALUES (100, 'test stuff', 111)", $sock);

$query ( $results, "SELECT * FROM crc,kw WHERE MATCH('test')", $sock );
$query ( $results, "SELECT * FROM crc,kw", $sock );
$query ( $results, "SELECT * FROM test", $sock );
$query ( $results, "UPDATE test SET idd=111 WHERE id=1", $sock );
$query ( $results, "SELECT * FROM test", $sock );
$client->FlushAttributes();

$results[]= "killing";
KillSearchd ('config.conf', 'searchd.pid','KILL',false);
usleep ( 150000 );
StartSearchd ( "config.conf", "error.txt", 'searchd.pid', $error, true );
usleep ( 150000 );

$results[] = "connecting again";
$sock = $make_sock( $results );
if ( $sock === false )
{
	return;
}

$query ( $results, "SELECT * FROM crc,kw WHERE MATCH('test')", $sock );
$query ( $results, "SELECT * FROM crc,kw", $sock );
$query ( $results, "SELECT * FROM test", $sock );

]]></custom_test>

</test>
